Hello,
I am very new to Arduino and this forum. I am currently working on a project for my class that requires me to click the number 5 on a 4x3 keypad and a green LED will blink twice then stay on. Also if I click 4 or 6 then a red LED will start to blink repeatedly. If the * button is pressed then the green or red light that is on will shut off. I will post my code below, and I am just wondering how I can make the red LED flicker on and off. I tried using a for loop and a while loop as shown in my code and it is not working. What am I doing wrong?
Thanks,
//MY CODE//
#include <Keypad.h>
const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 12, 11, 10, 9 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 8, 7, 6 };
// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
#define ledone 2
#define ledtwo 3
void setup()
{
pinMode(ledone,OUTPUT);
digitalWrite(ledone, LOW);
pinMode(ledtwo,OUTPUT);
digitalWrite(ledtwo, LOW);
Serial.begin(9600);
}
void loop()
{
char key = kpd.getKey();
if(key) // Check for a valid key.
{
if (key == '5') {
digitalWrite(ledone, HIGH);
delay(1000);
digitalWrite(ledone, LOW);
delay(1000);
digitalWrite(ledone, HIGH);
delay(1000);
digitalWrite(ledone, LOW);
delay(1000);
digitalWrite(ledone, HIGH);
}
if(key == '4'){
for(int i = 0; i<1000; i++){
digitalWrite(ledtwo, HIGH);
delay(1000);
digitalWrite(ledone, LOW);
delay(1000);
}
}
while (key == '6'){
digitalWrite(ledtwo, HIGH);
delay(1000);
digitalWrite(ledone, LOW);
delay(1000);
}
if (key == '*'){
digitalWrite(ledone, LOW);
digitalWrite(ledtwo, LOW);
}
}
}