So I am working with this code and want to have a Larson scanner ( a 1x8 matrix of scanning LEDs) coupled with a single switch that will activate multiple pins depending on which LED is activated in the scan.
I have posted the code I am working with below and my question is as follows:
I feel like a good way to approach the switching portion would be to us a while statement
Something with the rough logic of
While leds[] = high,low,low,low,low,low,low,low
If button is pushed activate position 1
While leds[] = low,high,low,low,low,low,low
If button is pushed activate position 2
Etc...
This is to temporarily activate a button on a tv remote while the function is lit up by a certain led corresponding to a remote function. It needs to be a single switch as I am making this for a person with MS and they can only use their thumb.
I hope I have explained this clearly and am looking for some guidance on if my approach makes sense.
Thanks for any help!
const int buttonPin = 2;
const int ledPin1 = 13;
int buttonState = 0;
int leds[] = {3, 4, 6, 7, 8, 9, 10, 11};
#define NUMBER_OF_LEDS (sizeof(leds)/sizeof(int))
boolean larson[][NUMBER_OF_LEDS] = {
{ HIGH, LOW, LOW, LOW, LOW, LOW, LOW, LOW},
{ LOW, HIGH, LOW, LOW, LOW, LOW, LOW, LOW},
{ LOW, LOW, HIGH, LOW, LOW, LOW, LOW, LOW},
{ LOW, LOW, LOW, HIGH, LOW, LOW, LOW, LOW},
{ LOW, LOW, LOW, LOW, HIGH, LOW, LOW, LOW},
{ LOW, LOW, LOW, LOW, LOW, HIGH, LOW, LOW},
{ LOW, LOW, LOW, LOW, LOW, LOW, HIGH, LOW},
{ LOW, LOW, LOW, LOW, LOW, LOW, LOW, HIGH},
{ LOW, LOW, LOW, LOW, LOW, LOW, HIGH, LOW},
{ LOW, LOW, LOW, LOW, LOW, HIGH, LOW, LOW},
{ LOW, LOW, LOW, LOW, HIGH, LOW, LOW, LOW},
{ LOW, LOW, LOW, HIGH, LOW, LOW, LOW, LOW},
{ LOW, LOW, HIGH, LOW, LOW, LOW, LOW, LOW},
{ LOW, HIGH, LOW, LOW, LOW, LOW, LOW, LOW},
};
#define FRAMES (sizeof(larson)/(sizeof(larson[0])))
int sensorPin = 0;
void setup() {
pinMode(ledPin1, OUTPUT);
pinMode(buttonPin, INPUT);
for (int led=0; led<NUMBER_OF_LEDS; led++) {
pinMode(leds[led], OUTPUT);
}
}
void loop(){
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(ledPin1, HIGH);
}
else {
long time = millis();
for (int frame=0; frame<FRAMES; frame++) {
for (int led=0; led<NUMBER_OF_LEDS; led++) {
digitalWrite(leds[led], larson[frame][led]);
}
int sensorValue = map(analogRead(sensorPin), 0, 1023, 0, 1000);
while (sensorValue >= (millis() - time)) {
sensorValue = analogRead(sensorPin);
}
time = millis();
}
}
}