Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h>
- Servo myservo;
- int LDR = 0; // light sensor connected to A0
- int prevValue = 0; // reference point
- int curValue = 0; // to check against prevValue
- void setup() {
- myservo.attach(13);
- pinMode(LDR, INPUT); //put light sensor as input
- myservo.write(140); // put servo in starting position
- Serial.begin(9600); // speed to interact with Arduino
- curValue = analogRead(LDR); //initialize curValue
- }
- void loop() {
- prevValue = curValue; // put prevValue to reference point
- curValue = analogRead(LDR); // get new value for light sensor
- Serial.println(curValue); //print sensor data (0-1023)
- if(curValue - prevValue >= 20) { // if light spikes up
- myservo.write(180); //move servo to button
- delay(400); //hold servo in current position
- myservo.write(140); //move servo back to initial position
- }
- delay(100); // wait a tenth of a second for new data
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement