combination lock with servo

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)

Post your code within the </> tags so that people can read it.
Stick print statements in your code so that you can find out where it is going and what it is doing.

OPs image so that we don’t have to download it.
IMG_3329.jpg

Edit: How are you powering the servo? If you are powering it from the 5v pin on the arduino, don’t do that. It may work sometimes with an unloaded servo, but as soon as you put a real load on the servo (like opening a box) it will likely cause the arduino to shut down. Servos should be powered from a separate source to avoid overloading the 5v regulator on the arduino.

For debugging call Clr_Code() only if the correct code has been received. Put print statements in the code to see if 'x' and 'y' are incrementing as you are expecting when the buttons are pressed.

It would help if your code between braces '{' '}' was indented.

ardly:
Post your code within the </> tags so that people can read it.
Stick print statements in your code so that you can find out where it is going and what it is doing.

Thank you that really gave us a better overview on what was happening!

/////

vinceherman:
OPs image so that we don’t have to download it.
IMG_3329.jpg

Edit: How are you powering the servo? If you are powering it from the 5v pin on the arduino, don’t do that. It may work sometimes with an unloaded servo, but as soon as you put a real load on the servo (like opening a box) it will likely cause the arduino to shut down. Servos should be powered from a separate source to avoid overloading the 5v regulator on the arduino.

That is a good idea we will definitely do that when building the final model.

////

We actually forgot to include pinMode(LED_gruen, OUTPUT) and we also changed some things with the servo code.

This is our final code:

#include <Servo.h>

Servo myservo;

//int val=180;
//int val2=-180;
int LED_rot=4;
int LED_gruen=6;
int SW1=2;
int SW2=3;
int Buzzer=10;
int x, y, code1, code2, resetTimer=0;
int pos = 0;

void setup ()
{

Serial.begin(9600);      // open the serial port at 9600 bps:   
myservo.attach(9);
pinMode(LED_rot, OUTPUT);
pinMode(Buzzer, OUTPUT);
pinMode(LED_gruen, OUTPUT);
//pinMode(val, OUTPUT);
//pinMode(val2, 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));
}
}

Serial.println(code1);      // print as an ASCII-encoded decimal - same as “DEC”
    Serial.print("\t");    // prints a tab

// Button2 = 3 presses
if (!digitalRead(SW2))
{
delay(50);
if (!digitalRead(SW2))
{
Blink();
y++;
if(y==3)
{
code2=true;

Serial.print(code2);      // print as an ASCII-encoded decimal - same as “DEC”
    Serial.print("\t");    // prints a tab
    Serial.print(LED_rot);      // print as an ASCII-encoded decimal - same as “DEC”
    Serial.print("\t");    // prints a tab
   
}else code2=false;

do{
delay(15);
resetTimer++;

if (resetTimer>50)
{
Toggle_Flash();
Clr_Code();
break;
}
}while (!digitalRead(SW2));
resetTimer=0;
}
}

if (code1 == true && code2 == true)
{
 
digitalWrite(LED_gruen, HIGH);
for (pos = 0; pos <= 180; pos += 1){
  myservo.write(pos);
delay(15);}

Serial.print(LED_gruen);      // print as an ASCII-encoded decimal - same as “DEC”
    Serial.print("\t");    // prints a tab

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;

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(15);
}

//myservo.write(val2);
delay(1000);
}




Thanks again for your help!

That is your final code :( Add comments so that people know what you are doing. Indent the code like this;

// 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));
    }
}

Why does the code extract above have a delay(50) what is it there for? You have one 'if' with another nested inside it, why are you trying to 'debounce'?