Skip to content

Instantly share code, notes, and snippets.

@ronniej2014
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ronniej2014/5fb605b8b5a9fd0b7b45 to your computer and use it in GitHub Desktop.
Save ronniej2014/5fb605b8b5a9fd0b7b45 to your computer and use it in GitHub Desktop.
//<<=== refer to the example of servo > sweep ===>>
#include <Servo.h>
Servo myServo;
int lightReceiver = A0; //pin: photocell
int led = 6; //pin: LED
int lightDef = 240; //define the value of brightness
int servoDegr = 40; //define the original degree of the servo
int rotationSpeed = 25; //define the speed of servo motion
void setup()
{
pinMode(lightReceiver, INPUT);
pinMode(buzzle, OUTPUT);
pinMode(led, OUTPUT);
myServo.attach(9); //pin: servo
Serial.begin(9600);
}
void loop()
{
int lightValue = analogRead(lightReceiver); //detect the brightness
Serial.println(lightValue);
if(lightValue < lightDef) //activate actuators
{
delay(30);
digitalWrite(led, HIGH); //turn on the LED
servoDegr -= rotationSpeed; //spin the servo
Serial.println(servoDegr);
if(servoDegr >30 | servoDegr < 20) //keep servo knock the box
{
rotationSpeed *= -1;
}
myServo.write(servoDegr);
delay(550); //keep the active statement for 550ms
}
else
{
delay(100); //
digitalWrite(led, LOW); //turn off the LED
delay(300); //keep the shaking statement for 300ms
servoDegr = 40; //recover the original position of servo
myServo.write(servoDegr);
delay(15);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment