Hello, we want to do a box which is opened by a servo after pushing a series of buttons. We managed to place the buttons so that they work. Every time a button is pressed the red LED lights up, however after the correct code nothing happens, although the green LED should light up and the servo should rotate.
We have not found the source of this problem yet. We thought it may be caused by the program not remembering that the codes were correct. Maybe someone of you detects a mistake we made?
Attached you will find the code and a photo.
Thank you very much!
Anna & Gesa
#include <Servo.h>
Servo myservo;
int val=180;
int val2=-180;
int LED_rot=4;
int LED_gruen=7;
int SW1=2;
int SW2=3;
int Buzzer=9;
int x, y, code1, code2, resetTimer=0;
void setup ()
{
myservo.attach(10);
pinMode(LED_rot, OUTPUT);
pinMode(Buzzer, OUTPUT);
pinMode(SW1, INPUT);
digitalWrite(SW1, HIGH);
pinMode(SW2, INPUT);
digitalWrite(SW2, HIGH);
Clr_Code();
}
void loop()
{
// Button1 = 5 presses
if (!digitalRead(SW1))
{
delay(50);
if (!digitalRead(SW1))
{
Blink();
x++;
if(x==5)
{
code1=true;
}else code1=false;
do{
}while (!digitalRead(SW1));
}
}
// Button2 = 3 presses
if (!digitalRead(SW2))
{
delay(50);
if (!digitalRead(SW2))
{
Blink();
y++;
if(y==3)
{
code2=true;
}else code2=false;
do{
delay(50);
resetTimer++;
if (resetTimer>50)
{
Toggle_Flash();
Clr_Code();
break;
}
}while (!digitalRead(SW2));
resetTimer=0;
}
}
if (code1 == true && code2 == true)
{
digitalWrite(LED_gruen, HIGH);
myservo.write(val);
//delay(15);
delay(5000);
Clr_Code();
digitalWrite(LED_gruen, LOW);
}
else
{
digitalWrite(LED_gruen, LOW);
}
}
void Blink(void)
{
digitalWrite(LED_rot, HIGH);
tone(Buzzer, 500, 150);
delay(200);
digitalWrite(LED_rot, LOW);
}
void Toggle_Flash(void)
{
int tog=0;
for (x=0; x<6; x++)
{
if (tog==0) tog=1; else tog=0;
digitalWrite(LED_rot, tog);
tone(Buzzer, 500, 250);
delay(300);
}
}
void Clr_Code (void)
{
x=0;
y=0;
code1=0;
code2=0;
resetTimer=0;
myservo.write(val2);
delay(1000);
}
combinationlock.ino (1.38 KB)
