Devices & Components
Arduino Uno Rev3
Female/Female Jumper Wires
USB-A to B Cable
Touth sensor
SG90 Micro-servo motor
Jumper wires (generic)
LED (generic)
Project description
Code
Servo, LED with touch sensor
arduino
1#include <Servo.h> 2 3Servo myservo; 4 5int pos = 0; 6int in = 2; 7int out = 13; 8int state = HIGH; 9int r; 10int p = LOW; 11long time = 0; 12long debounce = 200; 13void setup() 14{ 15 myservo.attach(9); 16 pinMode(in, INPUT); 17 pinMode(out, OUTPUT); 18} 19void loop() 20{ 21 r = digitalRead(in); 22 if (r == HIGH && p == LOW && millis() - time > debounce) { 23 for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees 24 // in steps of 1 degree 25 myservo.write(pos); // tell servo to go to position in variable 'pos' 26 delay(5); // waits 15ms for the servo to reach the position 27 } 28 for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees 29 myservo.write(pos); // tell servo to go to position in variable 'pos' 30 delay(5); // waits 15ms for the servo to reach the position 31 } 32 if (state == HIGH) 33 state = LOW; 34 else 35 state = HIGH; 36 time = millis(); 37 } 38 digitalWrite(out, state); 39 p = r; 40}
Downloadable files
untitled
untitled

untitled
untitled

Comments
Only logged in users can leave comments