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 -
-
The inputs taken are erratic & not in the required range for the A & B keys
-
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.
[code]
// ----------LIBRARIES--------------
#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};
byte colPins[COLS] = {A1, A0, 2, 3};
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; //
//-------- Want to Set this Interval & Duration using Keypad ---------- //
const int led_A_B_Interval = 3000; // = 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.)
const int blinkDuration_B = 1000; // = 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 - all three leds use this
const int blinkDuration_C = 1000; //
float timeA;
float timeB;
float timeAdd = 0;
int value = 0;
int key;
int isnum;
float blinkrate;
float blinktime;
byte led_A_State = LOW;
byte led_B_State = LOW;
byte led_C_State = LOW;
int val = 0;
unsigned long currentMillis = 0;
unsigned long previousLed_A_Millis = 0;
unsigned long previousLed_B_Millis = 0;
unsigned long previousLed_C_Millis = 0;
void setup() {
lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(3,0);
lcd.print("Hello");
Serial.begin(9600);
// set the Led pins as output:
pinMode(led_A_Pin, OUTPUT);
pinMode(led_B_Pin, OUTPUT);
pinMode(led_C_Pin, OUTPUT);
pinMode(ledread_A_Pin, INPUT);
}
void loop() {
currentMillis = millis(); // capture the latest value of millis()
// use the same time for all LED flashes to keep them synchronized
//**---------Problem----------**//
//Getnumber(); // - If we use this Getnumber the Numbers are taken & displayed but those values are
// not taken for the LED Interval & Duration - Here I have given fixed input values above
updateLed_A_State();
updateLed_B_State();
updateLed_C_State();
switchLeds();
}
void Getnumber(){
char key = keypad.getKey();
do
{
key = keypad.getKey();
isnum = (key >= '0' && key <= '9');
if (isnum)
{
timeAdd = timeAdd * 10 + key - '0';
}
} while (isnum || !key);
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;
}
Serial.print (timeA); // want to store the time in EEPROM for use even after turn on/off untill next change.
Serial.println(timeA);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Blnk Rate:");
lcd.setCursor(10,0);
lcd.print(timeA);
//lcd.print(" PM");
timeAdd = 0;
}
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 <= 5) { // want to input the value of Blink Duration ON time of LED - B between 0.5 to 3 seconds only.
timeAdd = (5);
timeB = timeAdd;
}
Serial.print(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 = 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 (currentMillis - previousLed_A_Millis >= led_A_B_Interval) {
led_A_State = HIGH;
previousLed_A_Millis += led_A_B_Interval;
}
}
else {
if (currentMillis - previousLed_A_Millis >= blinkDuration_A) {
led_A_State = LOW;
}
}
}
void updateLed_B_State() {
//blinkrate = 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 = 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);
}
[/code]