i tried this on the arduino form and got no explanation.
this is my first attempt at anything arduino, so i dont know much.
i downloaded the sketch from the net, for a keypad lock circuit. heres my main problem with it. the code is set to 1234. it opens on "4". if you enter xxxxxxx1xxxxxxx2xxxxxx3xxxxxxxx4, it opens. what i want to do is have a code that must be entered exactly, so hitting 12354 would not open it, but as it is, a correct press advances the program 1, while an incorrect presss is just ignored.
heres what i got:
#include <Keypad.h>
char* secretCode = "1234";
int position = 0;
const byte rows = 4;
const byte cols = 3;
char keys[rows][cols] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[rows] = {8, 7, 6, 5};
byte colPins[cols] = {4, 3, 2};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);
int redPin = 9;
int greenPin = 10;
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
setLocked(true);
}
void loop()
{
char key = keypad.getKey();
if (key == '*' || key == '#')
{
position = 0;
setLocked(true);
}
if (key == secretCode[position])
{
position ++;
}
if (position == 4)
{
setLocked(false);
}
}
void setLocked(int locked)
{
if (locked)
{
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
}
else
{
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
}
}
now, when ever i try to put any bit of code anywhere in there, it just bricks it out.
i add if key != secretCode[position] position = 0, ive tried else position = 0, anything i try just bricks it out, the correct input does not cause it to go to an open state.
today, i tried to add a few lines to see whats going on and tried this:
#include <Keypad.h>
char* secretCode = "1234";
int position = 0;
const byte rows = 4;
const byte cols = 3;
char keys[rows][cols] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[rows] = {8, 7, 6, 5};
byte colPins[cols] = {4, 3, 2};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);
int redPin = 9;
int greenPin = 10;
void setup()
{ Serial.begin(9600);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
setLocked(true);
}
void loop()
{
char key = keypad.getKey();
Serial.println(key);
Serial.println(position);
delay(500);
if (key == '*' || key == '#' )
{
position = 0;
setLocked(true);
}
if (key != secretCode[position])
{
position = 0;
}
if (key == secretCode[position])
{
position ++;
}
if (position == 4)
{
setLocked(false);
}
}
void setLocked(int locked)
{
if (locked)
{
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
}
else
{
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
}
}
every half second it tells me what button is being pressed, and what the value for position is. without the key != command, it works as it did before (incorrect key strokes ignored) but with that line in, i can see that when i press the 1, it goes to position 1 for one count (1/2 sec) and then back to 0, which is why it is not doing anything, it is never advancing to "4"
my first thought was that its counting no key press as a != and is resetting it to 0, but i was reassured that the char key = keypad.getKey(); command was waiting for a key press to advance, so a "non-press" wold not be counted.
any ideas on what im missing/dong wrong here?