I have recently replaced my original forum post with this one. Specifically, my problem is narrowed down to Switch Case statements. I want to control 5 led frequencies using poteniometers. Each one needs to be independently controlled and running from each other.
// Which pins are connected to which LED
const byte greenLED = 12;
const byte redLED = 13;
// Time periods of blinks in milliseconds (1000 to a second).
const unsigned long greenLEDinterval = 500;
const unsigned long redLEDinterval = 1000;
// Variable holding the timer value so far. One for each "Timer"
unsigned long greenLEDtimer;
unsigned long redLEDtimer;
void setup ()
{
pinMode (greenLED, OUTPUT);
pinMode (redLED, OUTPUT);
greenLEDtimer = millis ();
redLEDtimer = millis ();
} // end of setup
void toggleGreenLED ()
{
if (digitalRead (greenLED) == LOW)
digitalWrite (greenLED, HIGH);
else
digitalWrite (greenLED, LOW);
// remember when we toggled it
greenLEDtimer = millis ();
} // end of toggleGreenLED
void toggleRedLED ()
{
if (digitalRead (redLED) == LOW)
digitalWrite (redLED, HIGH);
else
digitalWrite (redLED, LOW);
// remember when we toggled it
redLEDtimer = millis ();
} // end of toggleRedLED
void loop ()
{
// Handling the blink of one LED.
if ( (millis () - greenLEDtimer) >= greenLEDinterval)
toggleGreenLED ();
// The other LED is controlled the same way. Repeat for more LEDs
if ( (millis () - redLEDtimer) >= redLEDinterval)
toggleRedLED ();
/* Other code that needs to execute goes here.
It will be called many thousand times per second because the above code
does not wait for the LED blink interval to finish. */
} // end of loop
This is my working revised version of the code:
///Output 1
int pot1 = A1;
const int out1 = 22;
int potVal1 = 0;
int frequency1 = 0;
int enable1 = 28;
//Output 2
int pot2 = A2;
const int out2 = 23;
int potVal2 = 0;
int frequency2 = 0;
int enable2 = 29;
int pot3 = A3;
const int out3 = 24;
int potVal3 = 0;
int frequency3 = 0;
int enable3 = 30;
int pot4 = A4;
const int out4 = 25;
int potVal4 = 0;
int frequency4 = 0;
int enable4 = 31;
int pot5 = A5;
const int out5 = 26;
int potVal5 = 0;
int frequency5 = 0;
int enable5 = 32;
unsigned long output1timer;
unsigned long output2timer;
unsigned long output3timer;
unsigned long output4timer;
unsigned long output5timer;
void setup ()
{
Serial.begin(9600);
pinMode (out1, OUTPUT);
pinMode (enable1, LOW);
pinMode (out2, OUTPUT);
pinMode (enable2, LOW);
pinMode (out3, OUTPUT);
pinMode (enable3, LOW);
pinMode (out4, OUTPUT);
pinMode (enable4, LOW);
pinMode (out5, OUTPUT);
pinMode (enable5, LOW);
output1timer = millis ();
output2timer = millis ();
output3timer = millis ();
output4timer = millis ();
output5timer = millis ();
} // end of setup
void Output1 ()
{
if (digitalRead (out1) == LOW)
digitalWrite (out1, HIGH);
else
digitalWrite (out1, LOW);
output1timer = millis ();
}
void Output2 ()
{
if (digitalRead (out2) == LOW)
digitalWrite (out2, HIGH);
else
digitalWrite (out2, LOW);
output2timer = millis ();
}
void Output3 ()
{
if (digitalRead (out3) == LOW)
digitalWrite (out3, HIGH);
else
digitalWrite (out3, LOW);
output3timer = millis ();
}
void Output4 ()
{
if (digitalRead (out4) == LOW)
digitalWrite (out4, HIGH);
else
digitalWrite (out4, LOW);
output4timer = millis ();
}
void Output5 ()
{
if (digitalRead (out5) == LOW)
digitalWrite (out5, HIGH);
else
digitalWrite (out5, LOW);
output5timer = millis ();
}
void loop ()
{
potVal1 = analogRead(pot1);
frequency1 = map(potVal1, 0, 1023, 15, 500);
//Serial.println(frequency1);
const unsigned long output1interval = frequency1;
if ( (millis () - output1timer) >= output1interval)
Output1 ();
potVal2 = analogRead(pot2);
frequency2 = map(potVal2, 0, 1023, 15, 500);
//Serial.println(frequency2);
const unsigned long output2interval = frequency2;
if ( (millis () - output2timer) >= output2interval)
Output2 ();
potVal3 = analogRead(pot3);
frequency3 = map(potVal3, 0, 1023, 15, 500);
//Serial.println(frequency3);
const unsigned long output3interval = frequency3;
if ( (millis () - output3timer) >= output3interval)
Output3 ();
potVal4 = analogRead(pot4);
frequency4 = map(potVal4, 0, 1023, 15, 500);
//Serial.println(frequency4);
const unsigned long output4interval = frequency4;
if ( (millis () - output4timer) >= output4interval)
Output4 ();
potVal5 = analogRead(pot5);
frequency5 = map(potVal5, 0, 1023, 15, 500);
//Serial.println(frequency5);
const unsigned long output5interval = frequency5;
if ( (millis () - output5timer) >= output5interval)
Output5 ();
/* Other code that needs to execute goes here.
It will be called many thousand times per second because the above code
does not wait for the LED blink interval to finish. */
} // end of loop
This code also works exactly how I want it to, but I want each case to incrementally include the handling of each LED blinking. For example, for case 1, frequency 1. For case 2, frequency 1 and 2. For case 3, frequency 1, 2, 3. The code for the handling of this is below. When run, the LED's flash, but I cannot change the frequency with the potiometers like in the code above. I'm sure I'm making a silly novice mistake.
switch(attention / 10) {
case 0:
potVal1 = analogRead(pot1);
frequency1 = map(potVal1, 0, 1023, 15, 500);
//Serial.println(frequency1);
const unsigned long output1interval = frequency1;
if ( (millis () - output1timer) >= output1interval)
Output1 ();
break;
case 1:
potVal1 = analogRead(pot1);
frequency1 = map(potVal1, 0, 1023, 15, 500);
//Serial.println(frequency1);
const unsigned long output1interval = frequency1;
if ( (millis () - output1timer) >= output1interval)
Output1 ();
potVal2 = analogRead(pot2);
frequency2 = map(potVal2, 0, 1023, 15, 500);
//Serial.println(frequency2);
const unsigned long output2interval = frequency2;
if ( (millis () - output2timer) >= output2interval)
Output2 ();
break;
case 2:
potVal1 = analogRead(pot1);
frequency1 = map(potVal1, 0, 1023, 15, 500);
//Serial.println(frequency1);
const unsigned long output1interval = frequency1;
if ( (millis () - output1timer) >= output1interval)
Output1 ();
potVal2 = analogRead(pot2);
frequency2 = map(potVal2, 0, 1023, 15, 500);
//Serial.println(frequency2);
const unsigned long output2interval = frequency2;
if ( (millis () - output2timer) >= output2interval)
Output2 ();
potVal3 = analogRead(pot3);
frequency3 = map(potVal3, 0, 1023, 15, 500);
//Serial.println(frequency3);
const unsigned long output3interval = frequency3;
if ( (millis () - output3timer) >= output3interval)
Output3 ();
break;
}
These frequency functions are added incrementally through 5