state change detection of 120vac switches

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

Your schematic and code are confusing... It looks like you have 120VAC connected to a 5V Arduino. What are you trying to do? Is there a reason you're using 120VAC on the switches?

I guess those 5 boxes between the AC switches and the Arduino are 5V DC power supplies? If so, you don't need the 130K resistors.

And i guess these are existing AC switches, and that's why you are using AC switches i this project?

I think you're probably stuck in a while() loop...

i.e. How can you ever get past the red switch loop until you change the red switch?

Add some serial.Print() statements and use the serial monitor so you can "see" what your program is doing. The serial monitor is your best debugging tool.

For example, you can print a message when you enter or exit your loops and you can print switch states and previous switch states, etc... Whatever information might be useful...

Hi, Welcome to the forum.

Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Can you post a picture of your project please, so we can see your layout. Also the specs on your 5V supplies please.

Thanks .. Tom. :)

the boxes are USB chargers, the USB plug was spliced and the 5V wires are connected to the ardiuno pins via 130k resistors. The ground wires are connected to the transistor and arduino grounds. The switches are 120VAC because the end project will be using touch switches that are on the wall powered only by mains.

using simple push buttons the code works no problems attached, but with buttons there were no state changes. just if button is pushed output this mode.

DVDdoug:
I guess those 5 boxes between the AC switches and the Arduino are 5V DC power supplies? If so, you don’t need the 130K resistors.

And i guess these are existing AC switches, and that’s why you are using AC switches i this project?

I think you’re probably stuck in a while() loop…

i.e. How can you ever get past the red switch loop until you change the red switch?

Add some serial.Print() statements and use the serial monitor so you can “see” what your program is doing. The serial monitor is your best debugging tool.

For example, you can print a message when you enter or exit your loops and you can print switch states and previous switch states, etc… Whatever information might be useful…

what exactly does the serial.Print() function do (new to arduino)? as i am running my code will the arduino be connected to the computer and as a switch changes will i see output on my computer? that would be cool if so. if not someone should get on that, lol. also i was previously told on this forum i needed at least 100K resistance before the arduino to input a tolerable current.

using if() instead of while() gets the code to run once through in any order, but then another switch cant be re flipped once used. i can flip them in any order but only by changing switch status to HIGH it works. when it is on LOW nothing happens. no state change is detected. using the while loop i can go back and forth but only on HIGH.

the switches are Leviton 15A 120VAC regular SPST toggle switches.

TomGeorge:
Also the specs on your 5V supplies please.

Thanks … Tom. :slight_smile:

110-240 VAC input
5VDC 1A ouput
regular USB wall chargers.

below is a picture of my input. All the wires on the left + are the switch hot wires, each switch is directly connected to an individual USB charger + prong and the - prongs are all joined into that green wire and plugged into AC neutral. The red wires on the right breadboard are the 5V+ from the USB chargers. they pass through a 130k resistor to a respective coloured wire that goes into the arduino (RGBW) the arduino and USB chargers share a common ground.

thanks for all the help and replies.

I'm still lost... You need to explain what you are trying to do overall. It sounds like you want to flip a light switch on the wall and have an LED strip turn on and off.

its for an application in a hospital setting for a small project. there will be a constant emitted white LED until one switch is flipped then the lights will change to that switches designated colour. While that switch is on another can be pressed, changing the lights to that switches designated colour now. if i pressed the previous switch again it would change back to that colour. etc and on and on. the switches will be touch switches in the end that are 3-way switches, if that changes anything. the last button pressed will be that designated light.

Gotcha... That makes sense.

I would not use USB adapters as your input source to the Arduino. A relay with a 120VAC coil makes a better choice. Power supplies do not like to be cycled on / off, relays are designed specifically for that.

i need the wall switches to send signals to the arduino when switched, to change the output. using a relay would act as a second switch would it not?

when there is current in, when the main switch is closed, the relay switch will flip once. when there is no current in, when the main switch is open, the relay switch will flip again? Can this switch flip in the relay be read as a state change for the arduino better than a regular power supply being turned off and on or are they the same?

maybe a steady state relay since they do not turn off 100% they will still send a signal? never really used relays before i'm not sure how the output is effected when the input goes from HIGH to LOW

the current set up has it so the white light takes priority then green light, then blue then red. this meaning that when the red switch is flipped HIGH i can hit the blue switch and it will go blue, then if the green switch is flipped it will go green, then white will make it white. but to go back to green i need to turn off white, blue i need to turn off the green. and to go back to red i need to turn off the blue.

i want it so that the last switch flipped takes priority in any order. also trying to make it so i dont need to turn the switches to HIGH to change the colour, i would like change of colour to also happen when switched on LOW, the state change detection for these switches is really not working for me. but now also the prioritizing switches i dont understand why it is happening i have not changed the circuit since the last post.