0

I am trying to set up an Arduino Uno plus a RelayShield along with 6 push buttons(standard), and 3 LED's(red, yellow, green). Now what I have code to do is to take in a button push combination that is 6 pushes long(order only matters for what is set in the array). I have everything wired up to a breadboard and all that other jazz. My problem is, none of the LED's are working(I followed detailed instructions from instructables - minus the code), and it doesn't matter how many button pushes there are, only button 3 triggers the relay.... Now, for simplicities sake, I didn't wire up all 6 buttons, and I just shortened the array down to 3 and adjusted accordingly(I didn't want to hook up 6 buttons right now). Can someone verify this code for me? Or tell me what is wrong with it? Thanks in advance!

//button pins
const int button1 = 2;
const int button2 = 3;
const int button3 = 4;
const int button4 = 5;
const int button5 = 6;
const int button6 = 7;
const int grnLed = 9;
const int redLed = 10;
const int yellowLed = 11;
const int openRelay = 8;

// how long is our code, kinda personal
const int codelen = 3;

//pin code values must match button pin values
char PIN[codelen] = {
'1', '2', '3'
};

// attempted combo
char attempt[codelen] = {
'0', '0', '0'
};

// attempt count
int z = 0;

void setup() {
// you've been setup
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
pinMode(button5, INPUT);
pinMode(button6, INPUT);
pinMode(openRelay, OUTPUT);
// set pullup resistor for buttons
digitalWrite(button1, HIGH);
digitalWrite(button2, HIGH);
digitalWrite(button3, HIGH);
digitalWrite(button4, HIGH);
digitalWrite(button5, HIGH);
digitalWrite(button6, HIGH);
// set openRelay state to open or closed
digitalWrite(openRelay, LOW);
}

void correctPIN()
{
pulseLED(grnLed, 3000);
digitalWrite(openRelay, HIGH);
delay(2000);
digitalWrite(openRelay, LOW);
delay(2000);
z = 0;
}
void incorrectPIN()
{
pulseLED(redLed, 3000);
z = 0;
}

void checkPIN()
{
int correct = 0;
int i;
for (i = 0; i < codelen; i++)
{

    if (attempt[i] == PIN[i])
    {
        correct++;
    }
}
if (correct == codelen)
{
    correctPIN();
}
else
{
    incorrectPIN();
}

for (int zz = 0; zz < codelen; zz++)
{
    attempt[zz] = '0';
}
}

void checkButton(int button){

if (digitalRead(button) == LOW)
{   
    while (digitalRead(button) == LOW) { } // do nothing
    //convert int to string for tracking/compare
    char buttStr = button + '0';
    attempt[z] = buttStr;
    z++;
    //light up led so we know btn press worked
    pulseLED(yellowLed, 500);
}
}

void pulseLED(int ledpin, int msec) {
digitalWrite(ledpin, HIGH);
delay(msec);
digitalWrite(ledpin, LOW);
}

void loop() {
// check buttons 
checkButton(button1);
checkButton(button2);
checkButton(button3);
checkButton(button4);
checkButton(button5);
checkButton(button6);
//if number of buttons pressed, z, matches code/pin length then check
if (z >= codelen)
{
    checkPIN();     
}   
}
Skrubb
  • 531
  • 1
  • 6
  • 25
  • Did you use pull-up resistors on the buttons? If not, set the pin mode for them to INPUT_PULLUP. Then you can debug your code with some serial messages. In any case, I think that your problem are bounces. Try using the bounce2 library, and instead of using `if (digitalRead(button) == LOW) { while (digitalRead(button) == LOW) { }` use the `fell()` method (which checks for the falling edge) or `rose()` method (which checks for the rising edge) – frarugi87 Jan 13 '17 at 11:38
  • An easy de bouncer can be done by reading the input low, delaying 10 or 20ms and then checking for low again. – cyberponk Jan 14 '17 at 04:12

0 Answers0