So..... I have various components of a program that work independently, Keypad, LED light patterns, but for the life of me, being rather new to all this I can't get them to play together nicely.
The DisplayPattern routine works perfectly outside the switch, but inside it only does the pattern one change at a time for each keypress.
press 9 LED1 goes high
press 9 LED2 goes high
press 9 LED1 goes low
press 9 LED1 goes low
press 9 LED5 goes high
and so forth.
What I am trying to achieve is that the sequence continues itself, until either the same key is pressed again, or the D key is pressed, where all LEDs are set low, and the system simply waits for a sequence to be called again
Keys 1 thru 6, are already toggling relays well, one press for on, one press for off again so I snipped the code down to the not working part. I must admit, I am not sure I understand the DisplayPattern function as it is not my own work, but simply did what I wanted it to.
void loop() {
char key = kpd.getKey();
if(key)
{
switch (key)
{
case '7':
DisplayPattern(led_pattern1, sizeof(led_pattern1));
delay(SPEED_MS);
break;
case '8':
DisplayPattern(led_pattern2, sizeof(led_pattern2));
delay(SPEED_MS);
break;
case '7':
DisplayPattern(led_pattern3, sizeof(led_pattern3));
delay(SPEED_MS);
break;
case'D'
//All outputs low, all sequences halted.
default:
Serial.println(key);
}
}
}
{
static int pattern_num = 0; // keeps count of patterns
unsigned char mask = 1; // for testing each bit in pattern
// do for LEDs on pin 2 to pin 7
for (int i = 2; i <= 7; i++) {
// check if bit in pattern is set or not and switch LED accordingly
if (pattern[pattern_num] & mask) {
digitalWrite(i, HIGH);
}
else {
digitalWrite(i, LOW);
}
mask <<= 1; // adjust mask for checking next bit in pattern
}
pattern_num++; // move to next pattern for next function call
// keep pattern within limits of pattern array
if (pattern_num >= num_patterns) {
pattern_num = 0;
}
}