Need help with loops

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

For an interactive and responsive program you should not use delay() because the Arduino can do nothing during a delay(). Have a look at how millis() is used to manage timing without blocking in several things at a time

Also, don’t use WHILE or FOR loops that take longer than a few microseconds to complete. Use IF and allow the loop() function to do the repeats.

Building your program as a series of small functions will make it much easier to develop and test. See Planning and Implementing a Program

…R

Hi,

Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :slight_smile:

The logic of that code is faulty.
While key is equal to six flash the LED and never look at the key value again so flash it forever.

If the key pressed is four then flash the LED for 33 minutes, nothing else can done during this time.

Is that how you want it to work?

You are also mixing up then number on the key with the number of times a key has been pressed.