Keypad input & LED Blinking not working

Hello, I am a new to programming, still learning.
For my need to Blink Two LED’s A & B at same interval between 5 to 50 Blinks per minute (Turn ON simultaneously) & Different ON times ( LED A will blink on for 0.5 seconds fixed time & LED B to blink for a different ON time from 0.5 Sec to 3 Sec.

I have used Key “A” to input the Blink Interval & Key “B” to input the ON time.

I am giving the Input Rate & On Time through a 4x4 Matrix Keypad & a 16x2 LCD for display &
want to use EEPROM function so can store the Input till next change.

I have gone through different combinations & tried this code, its taking the inputs but has following problems -

  1. The inputs taken are erratic & not in the required range for the A & B keys

  2. The LEDs are Not Blinking, they just turn on continuously for certain input values. (I have tried by removing the keypad parts & if I give fixed values in program the LED’s blink properly).

Cant find out whats going wrong, looking forward for help. Attached the code.

Thanking you,

Working_OK_Ver_1.03.ino (8.26 KB)

Please read the instructions - point No. 7 - to post your code so it is readily accessible to all.


It is inappropriate to attach it as a ".ino" file unless it is clearly too long to include in the post proper. People can usually see the mistakes directly and do not want to have to actually load it in their own IDE. And even that would also assume they are using a PC and have the IDE running on that PC.

Also tidy up your blank space. Do use blank lines, but only single blanks between complete functional blocks.

Extremely Sorry for the incorrect way of posting, will surely take care in future.

The blank lines & spaces were just for my understanding, but surely will make it a point to learn lessons from mistakes… Thanks for your kind support, the code is as below -

#include <EEPROM.h>
#include <Keypad.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(13, 12, 11, 10, 9, 8);
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {A5, A4, A3, A2}; //connect to the row pinouts of the kpd
byte colPins[COLS] = {A1, A0, 2, 3}; //connect to the column pinouts of the kpd
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
const int led_A_Pin = 7;       // the pin numbers for the LEDs
const int led_B_Pin = 6;
const int ledread_A_Pin = 5; // connect externally pin 5 to pin 7 so the pin 5 will read value of pin 7
const int led_C_Pin = 4; //  
float led_A_B_Interval; // = 3000; // The Blinking interval - Want to give this input via a keypad. 
                       //( Both LED A & LED B to bling at same interval, turn ON simultaneously but turn OFF as per the Blink duration on time.)
float blinkDuration_B; // = 1500; // ON time of the LED B  - Want to give this input via a keypad.
                      //( Number in millisecs that Led - B is ON )
const int blinkDuration_A = 500; // Preset time duration for ON time of LED A. no need of keypad input
                                // number of millisecs that Led's are on.
const int blinkDuration_C = 1000; // 
int timeA;                                        // Time A
int timeB;                                        // Time B
int timeAdd = 0;                                  // for adding in keypad 
int value = 0;                                     // the number accumulator
int key;                                           // the key press
int isnum;
float blinkrate;
float blinktime;
byte led_A_State = LOW;          // used to record whether the LEDs are on or off //   LOW = off
byte led_B_State = LOW;
byte led_C_State = LOW;
int val = 0;
unsigned long currentMillis = 0;    // stores the value of millis() in each iteration of loop()
unsigned long previousLed_A_Millis = 0;  // will store last time the LED was updated
unsigned long previousLed_B_Millis = 0;
unsigned long previousLed_C_Millis = 0;

void setup() {
 lcd.begin(16, 2);// set up the LCD's number of columns and rows:
 lcd.clear();
 Serial.begin(9600);
 pinMode(led_A_Pin, OUTPUT);    // set the Led pins as output:
 pinMode(led_B_Pin, OUTPUT);
 pinMode(led_C_Pin, OUTPUT);
 pinMode(ledread_A_Pin, INPUT);
}

void loop() {
 currentMillis = millis();  
 Getnumber();
 updateLed_A_State();
 updateLed_B_State();
 updateLed_C_State();
 switchLeds();
}

void Getnumber(){
 char key = keypad.getKey();
 do
 {
   key = keypad.getKey();                          // input the key
   isnum = (key >= '0' && key <= '9');             // is it a digit?
   if (isnum)
   {
     timeAdd = timeAdd * 10 + key - '0';         // accumulate the input number
   }
 } while (isnum || !key);                          // until not a digit or while no key pressed
 if (key != NO_KEY) {
   if (key == 'A') {
     timeA = timeAdd;
     if (timeA >= 50) {  // want to input the value of led_A_B_Interval between 5 to 50 blinks per minute only
       timeAdd = (50);
       timeA = timeAdd;
     }
     if (timeA <= 5) {   // want to input the value of led_A_B_Interval between 5 to 50 blinks per minute only
       timeAdd = (5);
       timeA = timeAdd;
     }
     EEPROM.put(1,timeA);     // want to store the time in EEPROM for use even after turn on/off untill next change.
     Serial.println(timeA);
      lcd.setCursor(0,0);
      lcd.print("Rate:"); 
      lcd.setCursor(10,0);
      lcd.print(timeA);
      //lcd.print(" PM");
   }
   if (key == 'B') {
     timeB = timeAdd;
     if (timeB >= 30) {   // want to input the value of Blink Duration ON time of LED - B  between 0.5 to 3 seconds only.
       timeAdd = (30);
       timeB = timeAdd;
     }
     if (timeB <= 4) {    // want to input the value of Blink Duration ON time of LED - B  between 0.5 to 3 seconds only.
       timeAdd = (4);
       timeB = timeAdd;
     }
     EEPROM.put(2,timeB);   // want to store the time in EEPROM for use even after turn on/off untill next change.
     Serial.println(timeB);
     lcd.setCursor(0,1);
     lcd.print("On Time:");
     lcd.setCursor(9,1);
     lcd.print(timeB);
     //lcd.print(" Sec");  
 timeAdd = 0;
   }
}
}

void updateLed_A_State() {
 blinkrate = EEPROM.get(1,timeA);       // want to use the stored value of blink rate of LED A & B in EEPROM for use even after turn on/off untill next change.
 led_A_B_Interval = (60*1000)/blinkrate;  // conversion of the manual Blink Rate Per Minute input data to milliseconds for use.
   if (led_A_State == LOW) {          // if the Led is off, we must wait for the interval to expire before turning it on
   if (currentMillis - previousLed_A_Millis >= led_A_B_Interval) {  
      led_A_State = HIGH;      
      previousLed_A_Millis += led_A_B_Interval;
  }
 }
 else {  // i.e. if led_A_State is HIGH             // if the Led is on, we must wait for the duration to expire before turning it off
   if (currentMillis - previousLed_A_Millis >= blinkDuration_A) {  // time is up, so change the state to LOW
      led_A_State = LOW;      // and save the time when we made the change
 } 
 }
}

void updateLed_B_State() {
 blinkrate = EEPROM.get(1,timeA);         // want to use the stored value of blink rate of LED A & B in 
                                                              EEPROM for use even after turn on/off untill next change.
 led_A_B_Interval = (60*1000)/blinkrate;   // conversion of the manual Blink Rate Per Minute input 
                                                                   data to milliseconds for use.
 blinktime = EEPROM.get(2,timeB);          // want to use the stored value of blink time of LED B in
                                                                 EEPROM for use even after turn on/off untill next change.
 blinkDuration_B = 100*blinktime;           // conversion of the manual Blink Time in seconds input data
                                             to milliseconds for use. **Better if can input direct using decimal Point**
  if (led_B_State == LOW) {
   if (currentMillis - previousLed_B_Millis >= led_A_B_Interval) {
      led_B_State = HIGH;
      previousLed_B_Millis += led_A_B_Interval;
   }
 }
 else {
   if (currentMillis - previousLed_B_Millis >= blinkDuration_B) {
      led_B_State = LOW;
   }
 }    
}

void updateLed_C_State() {
 val = digitalRead(ledread_A_Pin); // Read the input at pin 10 which is the status of Pin 13.
 if (val == HIGH) {
   led_C_State = HIGH;
 }  
 if (val == HIGH) {
       if (currentMillis - previousLed_C_Millis >= blinkDuration_C) {
          led_C_State = LOW;
         previousLed_C_Millis += blinkDuration_C;
    }
 }
 }

void switchLeds() {
     // this is the code that actually switches the LEDs on and off
 digitalWrite(led_A_Pin, led_A_State);
 digitalWrite(led_B_Pin, led_B_State);
 digitalWrite(led_C_Pin, led_C_State);
}

You did not read point #7 about posting code in the link provided. If you had, we would e.g. not see smileys in your code. Please edit your post, put
** **[code]** **
before your code and
** **[/code]** **
after your code.