/*
Arduino | coding-help
Adjusting brightness with potentiometer and switching between
RGB channels with one push button.
Ghost Potato OP — 11/5/24 at 9:44 PM
!THIS IS AN ASSIGNMENT! I am not looking for a direct answer, I just need a nudge in the right direction.
My assignment: "Write a program that allows the user to set the brightness of each of the RGB channels
by using the rotation dial to control brightness and a push button to select the channel. This
should be done using functions to manage the various aspects of the program."
Goal:
Use serial monitor to log all brightness levels and read the channel and locked brightness.
Starts with RGBRedPin on.
Adjust brightness with potentiometer.
Press a button to lock the brightness. (HOLD FOR 2S)
Press a button to switch to the green channel. (QUICK PRESS)
Adjust brightness of the green with potentiometer(does not change Red's locked brightness).
Press a button to lock the brightness. (HOLD FOR 2S)
Press a button to switch to blue channel. (QUICK PRESS)
Adjust blue brightness with potentiometer(does not change Red/Green brightness).
Press a button to lock the green brightness. (HOLD FOR 2s)
Loop starts again.
Add an exit to stop the loop?
*/
#include "button.h"
const char* RGB_NAME[3] = {"Red", "Green", "Blue"};
const int RGB_LED_PINS[3] = {11, 10, 9};
const int POT_PIN = A0;
int index = 0;
void setup() {
Serial.begin(9600);
for (int pin = 0; pin < 3; pin++) {
pinMode(RGB_LED_PINS[pin], OUTPUT);
}
Serial.println("Short press changes RGB channel.");
Serial.println("Long press sets channel value.\n");
Serial.print("Current channel: ");
Serial.println(RGB_NAME[index]);
}
void loop() {
int potValue = analogRead(POT_PIN);
int pwmValue = map(potValue, 0, 1023, 0, 255);
switch (button.read()) {
case ActionType::SHORT_PRESS:
//Serial.println("Short press");
index++;
if (index >= 3) index = 0;
Serial.print("Current channel: ");
Serial.println(RGB_NAME[index]);
break;
case ActionType::LONG_PRESS:
//Serial.println("Long press");
Serial.print(RGB_NAME[index]);
Serial.print(" channel PWM value: ");
Serial.println(pwmValue);
analogWrite(RGB_LED_PINS[index], pwmValue);
break;
default:
// do nothing
break;
}
}