// Mini-project for candy dispenser (Smarties) using servo motor and Arduino
#include <SoftwareServo.h> //header file for servo motor
Servo servol;
int switchstate=0;
void setup()
{
pinMode(6,OUTPUT); // digital pin 6 as output
servol.attach(9); // digital pin 9 as output
}
void loop()
{
switchstate=digitalRead(5); //digitalRead checks the chosen pin
if(switchstate==LOW) //conditional statement
{
servol.write(1); //it commands the servo motor to set its position to degree 1
}
if(switchstate==HIGH)
{
servol.write(179); //it commands the servo motor to set its position to degree 179
}
delay(2000); //it calls a delay for 2 seconds
}
Comments