Hi,
I have written a program so that my servo moves up and when I hit the button the servo moves back. I would like for it to move back after a 3 sec if the button is not pressed but I can't get it to do it. I have tried using if/else, as shown below, but that didn't work. Any suggestions?
Thanks,
Thomas
#include <Servo.h>
Servo s2;
int BUTTON = 11;
int COUNT = 1;
int buzzerPin = 8;
int counter;
unsigned long startTime;
void setup(){
Serial.begin(9600);
pinMode(11, INPUT);
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
s2.attach(5);
}
void loop(){
s2.write(6);
if(Serial.available()>0){
char data= Serial.read(); // reading the data received from the bluetooth module
switch(data)
{
{
case '1':
Serial.println(" ");
Serial.println(" Go...Go.. ");
unsigned long duration = 10000;
unsigned long startTime = millis();
unsigned long endTime = startTime + duration;
while(millis() < endTime) {
switch (COUNT);
int servo = random(5);
while(digitalRead(BUTTON) == LOW) {
noTone(buzzerPin);
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
s2.write(90);
delay(10);
}
if(digitalRead(BUTTON) == LOW){
delay(3000);
tone(buzzerPin, 600);
delay(50);
noTone(buzzerPin);
digitalWrite(13, LOW);
digitalWrite(12, HIGH);
s2.write(6);
delay(600);
COUNT ++;
Serial.print("You missed ");
Serial.println(COUNT);
}
else{
tone(buzzerPin, 600);
delay(50);
noTone(buzzerPin);
digitalWrite(13, LOW);
digitalWrite(12, HIGH);
s2.write(6);
delay(600);
COUNT ++;
Serial.print("You hit ");
Serial.println(COUNT);
}
}
}
}
Serial.println(" ");
Serial.println(" Finished: ");
Serial.println("Your result is: ");
Serial.print(COUNT);
Serial.print(" Target in 10 seconds ");
Serial.println(" ");
if(COUNT > 0){
COUNT = 0;
}
}
}