The keypad will be used to determine which lights to flash and what words to spell out. Because the CRBG code was taking up too much memory. I set that aside for a day and wrote the code that uses the toggle switch and keypad. I ran it and it works fine. Now I need to add back all of the LED lights part.
// This works like a charm thanks to some help from cattledog on the Audrino forum
// 11/16/19 Added LED display and that works too!
// This sketch gets 2 digits from the keypad and uses them to identify a color number and an item to spell out
// Now just add the toggle switch and then the LED light functions
// TO DOs::: Add a time for 30 seconds to see if a second 2 digit code is entered
// This is a safety procaution that allows me to retype the digits in if they are mistyped or miss read.
// After the 30 seconds times out, then turn off or dim the display
// 11/17/2019 Added Get2Digits functiosn to simply the code
#include <TM1637Display.h>
#define CLK 8
#define DIO 9
TM1637Display display(CLK, DIO);
#define key1 5 //connect wire 1 to pin 2
#define key2 4 //connect wire 2 to pin 3
#define key3 3 //connect wire 3 to pin 4
#define key4 2 //connect wire 4 to pin 5
#define NUM_ARRAYS 2
#define ARRAY_SIZE 5
int Array1[ARRAY_SIZE] = { 1, 2, 3, 4, 5 };
char *Array2[ARRAY_SIZE] = {"dog", "cat", "Horse","Ant", "Firetruck"};
uint8_t BlankDisplay[] = {0x0, 0x0, 0x0, 0x0}; //Used to clear out the display
int SelectPin10 = 10;
int SwitchStatusWas;
int SwitchStatusIs;
int keypressed = 0;
int keystroke_counter = 0; // counts how many key strokes have been recorded
int keypad_total = 0; // this is the total of the keypad entries added as a two digit value
int UserInput = 0; //Input from keypad
void setup() {
pinMode(key1, INPUT_PULLUP);// set pin as input
pinMode(key2, INPUT_PULLUP);// set pin as input
pinMode(key3, INPUT_PULLUP);// set pin as input
pinMode(key4, INPUT_PULLUP);// set pin as input
pinMode(SelectPin10, INPUT);
Serial.begin(9600);
display.setSegments(0); //Turn off / clear all display segments
display.setBrightness(7);
/////
// Turn on xmass lights to default setting
/////
}
void loop() {
// Look for keypad 2 digit entry & validate its correct or re-nter
static byte last_key1S = 1;
static byte last_key2S = 1;
static byte last_key3S = 1;
static byte last_key4S = 1;
byte key1S = digitalRead(key1);// read key1
byte key2S = digitalRead(key2);// read key2
byte key3S = digitalRead(key3);// read key3
byte key4S = digitalRead(key4);// read key4
//
// Look for toggle switch to go to ON. Needed to start the rest of the routine
ToggleSwitchChange();
Serial.println("Toggle Switch Change Detected ");
delay(5000); // Delay 5 seconds to walk over and unplug fake power cord
// Turn LEDs OFF (Add code later)
SwitchStatusWas = SwitchStatusIs; // Toggle has changed, Set Is/was equal to reset for another toggle test later
// Now wait for two digit item to be entered on the keypad
UserInput = Get2Digits(); // Get 2 digit entry and diplay
Serial.print("User Input was ");
Serial.println(UserInput);
delay(10000); // Display key entery for 10 seconds (need to have a loop to re-look at this later)
display.setSegments(BlankDisplay); // This clears out the dsiplay and makes it blank
// Identify color to use and item to use based off 2 digit code
// Print Color of item selected is
Serial.print("Color of item selected is ");
Serial.println(Array1[UserInput-1]);
// Print Item selected is
Serial.print("Type of item selected is ");
Serial.println(Array2[UserInput-1]);
// Serial.println(item_selected[x]);
// Wait for toggle to go back to OFF
ToggleSwitchChange();
Serial.println("Toggle Switch Change Detected ");
delay(1000); //Pause 1 seconds; make longer to allow perfromer to get ready for light show
Serial.print("Color of item selected is ");
Serial.println(Array1[UserInput-1]);
Serial.println("Color Lights brighten to on "); //Color Lights brighten to on
delay(1000); // Pause 5 seconds later make 10
Serial.println("Color Lights flash on and off 3 times "); //Color Lights bflash on and off 3 times
delay(1000); //Pause 5 seconds later make 24 seconds
Serial.println("Item selected is spelled out in the lights "); //Item selected is spelled out in the lights
delay(1000); // Pause 5 seconds later make 10
Serial.println("All Lights go OFF "); //All Lights go OFF
// Stay here until reset somehow. Currently program loops back to the top
keystroke_counter = 0; // 2 Digit keystoke received. Reset counter to zero
UserInput = 0; //// 2 Digit keystoke received. Reset total to zero
// Problem clearing out the display here. Look at later.
delay(60000); //Pause 60 seconds before re-looping. Replace this later with something else? Reset??
}
//////////////////////////// FUNTIONS //////////////////////////
int Get2Digits() {
keypressed = 0;
keystroke_counter = 0;
//Serial.println("Inside Get2Digits ");
// Look for 2 key entery on 1x4 keypad
while (keystroke_counter < 2) {
static byte last_key1S = 1;
static byte last_key2S = 1;
static byte last_key3S = 1;
static byte last_key4S = 1;
byte key1S = digitalRead(key1);// read key1
byte key2S = digitalRead(key2);// read key2
byte key3S = digitalRead(key3);// read key3
byte key4S = digitalRead(key4);// read key4
////// Cahnged keypressed values to be 0 thru 4 eeven though keys say 1-4
if (key1S != last_key1S) {
if (key1S == 0) { //went from HIGH to LOW
//Serial.println("key 1 is pressed");
keypressed = 0;
keystroke_counter = keystroke_counter + 1;
}
}
last_key1S = key1S;
if (key2S != last_key2S) {
if (key2S == 0) { //went from HIGH to LOW
//Serial.println("key 2 is pressed");
keypressed = 1;
keystroke_counter = keystroke_counter + 1;
}
}
last_key2S = key2S;
if (key3S != last_key3S) {
if (key3S == 0) { //went from HIGH to LOW
//Serial.println("key 3 is pressed");
keypressed = 2;
keystroke_counter = keystroke_counter + 1;
}
}
last_key3S = key3S;
if (key4S != last_key4S) {
if (key4S == 0) { //went from HIGH to LOW
//Serial.println("key 1 is pressed");
keypressed = 3;
keystroke_counter = keystroke_counter + 1;
}
}
last_key4S = key4S;
if (keystroke_counter == 1) {
keypad_total = keypressed * 10;
}
else if (keystroke_counter == 2) {
keypad_total = keypad_total + keypressed;
}
}
display.showNumberDec(keypad_total, false); // Expect: ___keypad_total
display.showNumberDec(keypad_total, false); // Expect: ___keypad_total
return keypad_total;
}
////////////////////////////////////////////////////
void ToggleSwitchChange() {
SwitchStatusWas = digitalRead(SelectPin10);
SwitchStatusIs = digitalRead(SelectPin10);
//Serial.print(" Switch Status WAS / IS ");
//Serial.print(SwitchStatusWas);
//Serial.print(SwitchStatusIs);
//Serial.println();
while (SwitchStatusIs == SwitchStatusWas) {
SwitchStatusIs = digitalRead(SelectPin10);
delay(100);
}
}
////////////////////////////////////////////////////