Simeonraykov
Published © Apache-2.0

LED, touch sensor and servo motor

Touch sensor projct

BeginnerProtip8,325
LED, touch sensor and servo motor

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×1
Touth sensor
×1
Female/Female Jumper Wires
Female/Female Jumper Wires
×1
Jumper wires (generic)
Jumper wires (generic)
×1
LED (generic)
LED (generic)
×1
USB-A to B Cable
USB-A to B Cable
×1

Story

Read more

Schematics

untitled_61R7OWhSCg.png

Code

Servo, LED with touch sensor

Arduino
#include <Servo.h>

Servo myservo;

int pos = 0; 
int in = 2; 
int out = 13;  
int state = HIGH;  
int r;           
int p = LOW;    
long time = 0;       
long debounce = 200;   
void setup()
{
  myservo.attach(9); 
  pinMode(in, INPUT);
  pinMode(out, OUTPUT);
}
void loop()
{
  r = digitalRead(in);
  if (r == HIGH && p == LOW && millis() - time > debounce) {
    for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(5);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(5);                       // waits 15ms for the servo to reach the position
  }
    if (state == HIGH)
      state = LOW;
    else 
      state = HIGH;
    time = millis();    
  }
  digitalWrite(out, state);
  p = r;
}

Credits

Simeonraykov

Simeonraykov

0 projects • 2 followers

Comments