Arduino Performs Undesired Tasks at Reset or Start-Up

Greetings,

Overview of Project:
I am working on a project to create an electronic, 4-button, pass-coded lock. Essentially, I have a servo that would actuate the locking mechanism itself, 5 buttons, a speaker, and 3 LEDs. My code is comprised of a series of “if” statements. Basically, if a button is pressed in the correct position in the code sequence, then 1 is added to a value counter. If, after 4 buttons have been pressed, the value is equal to 4, then the code was entered correctly and the lock opens with the servo. Otherwise, the servo does not open the lock. Additionally, the LEDs and speaker output certain things for different cases to make the system more engaging. Pressing a fifth button moves the servo into the locked position.

Problem:
The problem is that when I plug in or reset the Arduino, it does some weird nonsense. What I mean is that if I were to hit the reset button, first the servo would rotate to some neutral position, then the Arduino would assume that all 5 buttons were pressed, and would end with the servo rotating to the locked position, despite no buttons having actually been pressed. This is a big problem because I want to implement a physical switch between the Arduino and its power source so that nothing is using power when the lock is not being manipulated. And if the Arduino does this every time the switch is flipped on, the lock would not be very effective.

Possible Solution:
I have found that the Arduino does not make the button assumptions if I remove the debounce feature from my code but the servo still rotates to the “neutral” position.

My current code is attached below along with a poorly drawn schematic of my circuit.
Any help is greatly appreciated. Thank you! :slight_smile:

sketch_nov05b.ino (9.62 KB)

For the servo start position, try:

myservo.write(xxx); // put desired start position here
myservo.attach(10); // attaches the servo on pin 10 to the servo object

And initialize all the lBs variables to 1.

int bS1 = LOW;       // current state of the button
int lBS1 = 0;        // previous state of the button
int bS2 = LOW;   
int lBS2 = 0; 
int bS3 = LOW;  
int lBS3 = 0; 
int bS4 = LOW;       
int lBS4 = 0; 
int bS5 = LOW;
int lBS5 = 0;

It is hard to hear in here, with this code just screaming for arrays.

PaulS:
It is hard to hear in here, with this code just screaming for arrays.

:smiley: