I have a sketch running on my arduino mega that constantly checks the value being returned from 4 different potentiometers. It then checks the value being read against a minimum value I've set in order to determine the next step. To give a better idea, I'll explain a bit about what I have setup and what it is doing:
My circuit has 4 potentiometers, 4 x 5mm leds, 4 RGB LED strips, and the mega. In this setup, each potentiometer has a corresponding 5mm led, and a corresponding led strip that it controls based on the value read within the sketch. So we can say that pot A controls led A and rgb strip A, then B controls B, etc... The response time is very quick when turning "on" or "off a single potentiometer, but gets grossly dragged out when, say, all 4 are being changed rather quickly.
I understand that the arduino can only execute one thing at a time, but I would like to find a more efficient way of running my sketch so that the response time is as low as realistically possible. Another thing to clarify is that I am not a programmer. This sketch is the most code of any sort that I have written. As for the real-world applications of my setup: I am building a play kitchen for my girls, the potentiometers will be used for the cooktop controls, and the 5mm leds to indicate which "burner" is turned on (the rgb light strips).
I appreciate any and all feedback anyone has to give, and will happily take advise on how to improve my arduino programming. Please take a quick look at my sketch and let me know if there's anything I am doing wrong, what I could do better, and how I can reduce the response time between events if at all possible!
int potentiometers[4] {
0, 1, 2, 3
};
int indicatorLights[4] {
50, 51, 52, 53
};
int colorTerminals[4] {
2, 5, 8, 11
};
int brightness = 0;
void setup() {
Serial.begin(9600);
for (int i =0; i < 4; i++) {
pinMode(indicatorLights[i], OUTPUT);
pinMode(colorTerminals[i], OUTPUT);
}
}
void ReadPots() {
for (int i = 0; i < 4; i++) {
int selectedPot = potentiometers[i];
int potVal = map(analogRead(selectedPot), 0, 1023, 0, 255);
int selectedColorTerminal = colorTerminals[i];
int selectedIndicator = indicatorLights[i];
int indicatorState = digitalRead(selectedIndicator);
if (potVal < 20) {
if ( indicatorState ) {
TurnOff(selectedIndicator, selectedColorTerminal, indicatorState);
}
else if ( !indicatorState ) {
RemainOff(selectedIndicator, selectedColorTerminal, indicatorState);
}
}
else if (potVal > 20) {
if ( indicatorState ) {
RemainOn(selectedIndicator, selectedColorTerminal, indicatorState);
}
else if ( !indicatorState ) {
TurnOn(selectedIndicator, selectedColorTerminal, indicatorState);
}
}
}
}
void TurnOff (int indicator, int colorTerminal, int state) {
digitalWrite(indicator, !state);
brightness = 255;
for (int j = 0; j < 256; j++) {
analogWrite(colorTerminal, brightness);
brightness -=1;
delay(10);
}
}
void RemainOff (int indicator, int colorTerminal, int state) {
brightness = 0;
digitalWrite(indicator, state);
analogWrite(colorTerminal, brightness);
}
void RemainOn (int indicator, int colorTerminal, int state) {
brightness = 255;
digitalWrite(indicator, state);
analogWrite(colorTerminal, brightness);
}
void TurnOn (int indicator, int colorTerminal, int state) {
digitalWrite(indicator, !state);
brightness = 0;
for (int j = 0; j < 256; j++) {
analogWrite(colorTerminal, brightness);
brightness +=1;
delay(10);
}
}
void loop() {
ReadPots();
}