Hello. I'm fairly new to Arduino. I made a simple project that controls the color of an RGB LED using three potentiometers and an Arduino UNO. I have three potentiometers connected to the UNO that control the intensity of the light in each field. The outputs for blue, green and red leds are biased to a 220 ohm resistor each. The circuit works and I'm planning on adding a switch that turns the circuit ON or OFF but I don't know how.
I have a tact switch available and connected the switch in this configuration. The switch is connected to a 220 ohm pull up resistor. How can I code the UNO so that when I press the switch once the circuit turns ON, retaining the color set by the potentiometers and turn OFF when pressed again? My sample code is written below. I attached a .png file for the circuit.
// Potentiometer Pins
#define bluPot A2
#define grnPot A1
#define redPot A0
// Output Pins
#define bluLED 9
#define grnLED 10
#define redLED 11
// Switch Pin
#define switchInput 8
// Temporary variables
int temp;
void setup() {
pinMode(bluLED, OUTPUT);
pinMode(grnLED, OUTPUT);
pinMode(redLED, OUTPUT);
Serial.begin(9600);
}
int bluPotControl(int bluVal) {
bluVal = analogRead(bluPot);
bluVal = map(bluVal, 0, 1023, 0, 255);
return bluVal;
}
int grnPotControl(int grnVal) {
grnVal = analogRead(grnPot);
grnVal = map(grnVal, 0, 1023, 0, 255);
return grnVal;
}
int redPotControl(int redVal) {
redVal = analogRead(redPot);
redVal = map(redVal, 0, 1023, 0, 255);
return redVal;
}
void serialDisplay(int bluVal, int grnVal, int redVal) {
Serial.print("\nbluVal = ");
Serial.print(bluVal);
Serial.print("\tgrnVal = ");
Serial.print(grnVal);
Serial.print("\tredVal = ");
Serial.print(redVal);
}
void ledDisplay(int bluVal, int grnVal, int redVal) {
analogWrite(bluLED, bluVal);
analogWrite(grnLED, grnVal);
analogWrite(redLED, redVal);
}
void loop() {
int bluVal = bluPotControl(temp);
int grnVal = grnPotControl(temp);
int redVal = redPotControl(temp);
ledDisplay(bluVal, grnVal, redVal);
serialDisplay(bluVal, grnVal, redVal);
}