Hi,
I'm doing my best to read through and understand the reference page but I'm still puzzled over which is more appropriate to use in a certain context, can anyone assist in pointing me in the right direction?
My task is to create a programmable relay controller.
The visual structure is intended to run like this;
My equipment at present consists of a mega, 16key keypad and 20x4 web4robot I2C LCD.
My sketch in progress is;
#include <LCDi2cW.h>
#include <Wire.h> // i2c Library
#include <EEPROM.h> // EEPROM Library
LCDi2cW lcd = LCDi2cW(4,20,0x4C,0); // Define size of LCD and i2c address
#define r_del 20; //Repeating key delay
#define k_del 500; //1st keypress delay (delay before repeating)
int key_delay = k_del; //Delay coutner for key debounce/ repeat
char last_k = 0; //Last key pressed
unsigned long last_t = 0; //Time last keypress made
char k; //For storing keypress
//-------------------------------------------------------------------------------------------------
// Get key routine.
char get_key()
{
char k=0; // Default value, no key pressed
unsigned long t = millis(); // Time now
k=lcd.keypad();
if (k == last_k and k != 0) // Are we keeping a key pressed?
{
if (t-last_t < key_delay)
{ // If so, has required time passed?
k=0; // Not yet, so return 0 for no keypress
}
else
{
last_t = t; // Required time passed, so save the current time
key_delay = r_del; // and set delay to repeating key time
}
}
else
{
key_delay = k_del; // Not keeping a key pressed, so set delay to 1st keypress timeout
last_t = t; // and save the time
last_k = k; // and the key just pressed (or 0 if no keys down)
}
return (k); // Return the key press info or 0 for no key pressed
}
//-------------------------------------------------------------------------------------------------
byte keyPress, oldkeyPress=0, Zones, Zone1_state=HIGH, Zone2_state=LOW, Zone3_state=LOW;
boolean backlightState;
byte Zone1=24; // Digital pin assignment
byte Zone2=25; // Digital pin assignment
byte Zone3=26; // Digital pin assignment
int keyInput;
//-------------------------------------------------------------------------------------------------
void setup()
{
Wire.begin(); // Initialise the i2c bus
lcd.init(); // Initialise the LCD
lcd.clear(); // Clear the LCD
lcd.on(); // Turn on LCD backlight
backlightState=true;
pinMode(Zone1, OUTPUT);
pinMode(Zone2, OUTPUT);
pinMode(Zone3, OUTPUT);
digitalWrite(Zone1, LOW);
digitalWrite(Zone2, LOW);
digitalWrite(Zone3, LOW);
}
//-------------------------------------------------------------------------------------------------
void loop()
{
keyPress=get_key();
Zones=get_key();
//------<start Switch section>------
switch (keyPress)
{
//............................
case 13:
{
lcd.clear();
A_screen(); //print A key's screen
delay(2000); //wait 2secs for a key to be pressed, if no key pressed, return to main screen & loop
if(keyPress == 1) //if key 1 pressed, do this...
{
lcd.clear();
lcd.print("1");
delay(1000);
lcd.clear();
break;
}
if(keyPress == 2) //if key 2 pressed, do this...
{
lcd.clear();
lcd.print("2");
delay(1000);
lcd.clear();
break;
}
lcd.clear();
break;
}
//............................
case 14:
{
lcd.clear();
B_screen(); //print B key's screen
delay(2000); // wait for 2 secs for a key to be pressed, if no key pressed, return to main screen & loop
while (keyPress<200)
{
case 1:
lcd.clear();
lcd.print("1");
delay(1000);
lcd.clear();
break;
case 2:
lcd.clear();
lcd.print("2");
delay(1000);
lcd.clear();
break;
}
lcd.clear();
break;
}
//.............................
case 15:
{
lcd.clear();
C_screen(); //print C key's screen
delay(2000); //wait for 2 secs for a key to be pressed, if no key pressed, return to main screen & loop
for(keyPress = 2; keyPress<200; keyPress++)
{
lcd.clear();
lcd.print("1");
delay(1000);
lcd.clear();
break;
}
lcd.clear();
break;
}
//..............................
case 16:
{
lcd.clear();
D_screen();
delay(5000);
lcd.clear();
break;
}
//...............................
}
//------<end Switch section>------
MainScreen();
ZonesActive();
}
// End of loop function
At the moment I'm stuck on how to construct a loop within a loop or such, for example, if Key A is pressed, I want it to hold that screen for a period of time and wait for a key press, if a certain defined key press occurs, it triggers another action.
I think the correct method is using the "while" statement, but I'm not sure if I'm on the right track.
It also seems to repeat that loop and return a printed "1" even when no key is pressed, but I suspect that may be because I've confused my statements and their application.
Can someone point me in the right direction with which is more suitable, or a even to a thread or tut that may explain it a little better.
Cheers.