Hello all ![]()
I'm not a coder, so I've been piecing and modding from what I could find. Everything's working as I'd like, except I need to:
- In Case 4: Control the overall brightness of all LED's with pot 1. Also, create individual delays without blocking.
- In Case 5: Control the overall brightness of all LED's with pot 1. Also, control fade speed with pot #2.
- In both cases 4 and 5, right now, each LED finishes fading out before the next one starts fading in. I want each LED to start fading in exactly when the last one starts fading out.
I'm using an Arduino Pro Mini and here is the code:
const int buttonOne = 2;
const int potPin1 = A1;
const int potPin2 = A2;
const int motorLowerBLUE = 3;
const int motorMiddleGREEN = 5;
const int motorUpperRED = 6;
int buttonOneState;
int lastButtonOneState = 1;
unsigned long lastDebounceMillis = 0;
const unsigned long debounceDelay = 30;
int potValue1 = 0;
int potValue2 = 0;
int mode;
int prevMode = 0;
int ledState = LOW;
unsigned long previousMillis = 0;
void setup() {
Serial.begin(9600);
pinMode(buttonOne, INPUT_PULLUP);
pinMode(potPin1, INPUT);
pinMode(potPin2, INPUT);
pinMode(motorUpperRED, OUTPUT);
pinMode(motorMiddleGREEN, OUTPUT);
pinMode(motorLowerBLUE, OUTPUT);
mode = 0;
reset();
}
void loop() {
// always check on the pot values
potValue1 = analogRead(potPin1);
potValue2 = analogRead(potPin2);
// Check pushbutton?
int currentMode = checkModeButton(prevMode);
// ***** add or remove modes - also added in "mode functions" section ******
switch (currentMode) {
case 0: // all off
break;
case 1: // Bue LED, brightness controlled by pot 1
blueDimmer(potValue1);
break;
case 2: // Green LED, brightness controlled by pot 1
greenDimmer(potValue1);
break;
case 3: // Red LED, brightness controlled by pot 1
redDimmer(potValue1);
break;
case 4: // "stop light" effect, overall brightness controlled by pot 1
stopLight(potValue1);
break;
case 5: // "stop light" effect, overall brightness controlled by pot 1, speed by pot 2
stopLight2(potValue1);
break;
default: // nothing happening
break;
}
prevMode = mode;
}
// inner workings
int checkModeButton(int prevMode) {
int buttonOneReading = digitalRead(buttonOne);
if (buttonOneReading != lastButtonOneState) {
lastDebounceMillis = millis();
}
if ((millis() - lastDebounceMillis) > debounceDelay) {
if (buttonOneReading != buttonOneState) {
buttonOneState = buttonOneReading; // let's double check
if (buttonOneState == LOW) {
int lastMode = prevMode;
Serial.print("\n\nlast mode: ");
Serial.println(lastMode);
mode++;
Serial.print("button pressed: ");
Serial.print("current mode = ");
Serial.println(mode);
Serial.println();
// ******** change nuber of modes in "(mode >= x)" below when adding or removing modes *****************
}
if (mode >= 6) mode = 0;
else if (mode == 0) Serial.println("\nreset to mode 0");
reset();
}
}
lastButtonOneState = buttonOneReading;
return mode;
}
// ****************** mode functions - also add in "add or remove" mode section ********************
// Case 1 - Steady blue-only dimmer mode - brightness controlled by pot 1
void blueDimmer(int potValue1) {
int analogMotor = potValue1 / 4; /* Changing this can change the nuber of resets that happen
when turning brightness pot. Original text that was here:
"Store potValue1 / 4 to match 10-bit ADC to 8-bit DAC for LEDs"*/
analogWrite(motorLowerBLUE, analogMotor);
analogWrite(motorMiddleGREEN, LOW);
analogWrite(motorUpperRED, LOW);
}
// Case 2 - Steady green-only dimmer mode - brightness controlled by pot 1
void greenDimmer(int potValue1) {
int analogMotor = potValue1 / 4; /* Changing this can change the nuber of resets that happen
when turning brightness pot. Original text that was here:
"Store potValue1 / 4 to match 10-bit ADC to 8-bit DAC for LEDs"*/
analogWrite(motorLowerBLUE, LOW);
analogWrite(motorMiddleGREEN, analogMotor);
analogWrite(motorUpperRED, LOW);
}
// Case 3 - Steady red-only dimmer mode - brightness controlled by pot 1
void redDimmer(int potValue1) {
int analogMotor = potValue1 / 4; /* Changing this can change the nuber of resets that happen
when turning brightness pot. Original text that was here:
"Store potValue1 / 4 to match 10-bit ADC to 8-bit DAC for LEDs"*/
analogWrite(motorLowerBLUE, LOW);
analogWrite(motorMiddleGREEN, LOW);
analogWrite(motorUpperRED, analogMotor);
}
// Case 4 - Fading "stop light" mode: blue-green-red-green-repeat, brightness controlled by pot 1
// ****** In Case 4, I need to:
// - create individual delays, but in a
// non-blocking way, maybe somehow use millis?
// - limit overall brightness with pot 1.
// - Right now, each LED finishes fading out
// before the next one starts fading in.
// I want each LED to start fading in
// exactly when the last one starts
// fading out.
void stopLight(int potValue1) {
Serial.print("fading...");
//int analogMotor = potValue / 4; // Store potValue / 4 to match 10-bit ADC to 8-bit DAC for LEDs
// blue fades in
for (int brightness = 0; brightness <= 255; brightness++) {
analogWrite(motorLowerBLUE, brightness);
delay(5);
}
// blue fades out
for (int brightness = 255; brightness >= 0; brightness--) {
analogWrite(motorLowerBLUE, brightness);
delay(2);
}
// green fades in
for (int brightness = 0; brightness <= 255; brightness++) {
analogWrite(motorMiddleGREEN, brightness);
delay(5);
}
// green fades out
for (int brightness = 255; brightness >= 0; brightness--) {
analogWrite(motorMiddleGREEN, brightness);
delay(1);
}
// red fades in
for (int brightness = 0; brightness <= 255; brightness++) {
analogWrite(motorUpperRED, brightness);
delay(5);
}
// red fades out
for (int brightness = 255; brightness >= 0; brightness--) {
analogWrite(motorUpperRED, brightness);
delay(1);
}
// green fades in
for (int brightness = 0; brightness <= 255; brightness++) {
analogWrite(motorMiddleGREEN, brightness);
delay(5);
}
// green fades out
for (int brightness = 255; brightness >= 0; brightness--) {
analogWrite(motorMiddleGREEN, brightness);
delay(1);
}
}
// Case 5 - Fading "stop light" mode: blue-green-red-green-repeat - brightness by pot 1, speed by pot 2
// ****** In Case 5, I need to:
// - Create same exact delays between each LED, without
// code bocking by "delay" command.
// Pot 2 will determine those delays. Turn pot 2
// up, LED's change fast. Turn pot 2 down,
// LED's change slow.
// - limit overall brightness with pot 1.
// - Right now, each LED finishes fading out
// before the next one starts fading in.
// I want each LED to start fading in
// exactly when the last one starts
// fading out.
void stopLight2(int potValue2) {
Serial.print("fading 2...");
// blue fades in
for (int brightness = 0; brightness <= 255; brightness++) {
analogWrite(motorLowerBLUE, brightness);
//********** delay here - time determined by pot 2
}
// blue fades out
for (int brightness = 255; brightness >= 0; brightness--) {
analogWrite(motorLowerBLUE, brightness);
//********** delay here - time determined by pot 2
}
// green fades in
for (int brightness = 0; brightness <= 255; brightness++) {
analogWrite(motorMiddleGREEN, brightness);
//********** delay here - time determined by pot 2
}
// green fades out
for (int brightness = 255; brightness >= 0; brightness--) {
analogWrite(motorMiddleGREEN, brightness);
//********** delay here - time determined by pot 2
}
// red fades in
for (int brightness = 0; brightness <= 255; brightness++) {
analogWrite(motorUpperRED, brightness);
//********** delay here - time determined by pot 2
}
// red fades out
for (int brightness = 255; brightness >= 0; brightness--) {
//********** delay here - time determined by pot 2
}
// green fades in
for (int brightness = 0; brightness <= 255; brightness++) {
analogWrite(motorMiddleGREEN, brightness);
//********** delay here - time determined by pot 2
}
// green fades out
for (int brightness = 255; brightness >= 0; brightness--) {
analogWrite(motorMiddleGREEN, brightness);
//********** delay here - time determined by pot 2
}
}
// reset mode - all is off
void reset() {
digitalWrite(motorUpperRED, LOW);
digitalWrite(motorMiddleGREEN, LOW);
digitalWrite(motorLowerBLUE, LOW);
}

