hey guys, I currently am working on a project where 4 switches need to cycle through 4 colours on an LED strip, i have it currently so that each switch powers one colour when the switch is on HIGH only.
my code is :
#define OFF 0
#define RED 1
#define BLUE 2
#define GREEN 3
//The pins with the switches
const int offSwitch = 2; //pin 2
const int redSwitch = 3;
const int blueSwitch = 4;
const int greenSwitch = 5;
// the pins with the LEDs
const int redLeds = 9; //PWM pin 9
const int blueLeds = 10;
const int greenLeds = 11;
// the current and last states of all the switches
int blueSwitchState;
int redSwitchState;
int greenSwitchState;
int offSwitchState;
int lastBlueSwitchState = LOW;
int lastGreenSwitchState = LOW;
int lastRedSwitchState = LOW;
int lastOffSwitchState = LOW;
//base mode to start on
int mode = 0;
void setup()
{
pinMode(offSwitch, INPUT);
pinMode(redSwitch, INPUT);
pinMode(blueSwitch, INPUT);
pinMode(greenSwitch, INPUT);
pinMode(redLeds, OUTPUT);
pinMode(blueLeds, OUTPUT);
pinMode(greenLeds, OUTPUT);
Serial.begin(9600);
}
void loop()
{
while (digitalRead(redSwitch)) {
redSwitchState = digitalRead(redSwitch);
if (redSwitchState != lastRedSwitchState) { // compare the switchState to its previous state
mode = 1;
delay(50);
}
}
while (digitalRead(blueSwitch)) {
blueSwitchState = digitalRead(blueSwitch);
if (blueSwitchState != lastBlueSwitchState) {
mode = 2;
delay(50);
}
}
while (digitalRead(greenSwitch)) {
greenSwitchState = digitalRead(greenSwitch);
if (greenSwitchState != lastGreenSwitchState) {
mode = 3;
delay(50);
}
}
while (digitalRead(offSwitch)) {
offSwitchState = digitalRead(offSwitch);
if (offSwitchState != lastOffSwitchState) {
mode = 0;
delay(50);
}
}
lastOffSwitchState = offSwitchState;
lastGreenSwitchState = greenSwitchState;
lastBlueSwitchState = blueSwitchState;
lastRedSwitchState = redSwitchState;
switch (mode) {
case 0:
digitalWrite(redLeds, HIGH);
digitalWrite(blueLeds, HIGH);
digitalWrite(greenLeds, HIGH);
break;
case 1:
digitalWrite(redLeds, HIGH);
digitalWrite(blueLeds, LOW);
digitalWrite(greenLeds, LOW);
break;
case 2:
digitalWrite(redLeds, LOW);
digitalWrite(blueLeds, HIGH);
digitalWrite(greenLeds, LOW);
break;
case 3:
digitalWrite(redLeds, LOW);
digitalWrite(blueLeds, LOW);
digitalWrite(greenLeds, HIGH);
break;
}
}
i would like the colours of the LED strip to change even when the switch goes to LOW and not just on HIGH. my circuit is also attached