First time posting here! To make a long story short, I'm working on a project with four switches and four lights (light bulbs connected to relays). Individually, they all work. However, I can not get them all to turn on at the same time. Only the first two turn on if I flip all 4 switches, and sometimes, only one light will come on at a time. I've attached my code below, is there something wrong there or with my hardware?
const int switchOne = 22;
const int switchTwo = 28;
const int switchThr = 26;
const int switchFou = 30;
const int lightOne = 3;
const int lightTwo = 5;
const int lightThr = 2;
const int lightFou = 4;
const int bell = 6;
void setup() {
pinMode(switchOne, INPUT_PULLUP); // Set switchPin as input with internal pull-up resistor
pinMode(lightOne, OUTPUT); // Set relayPin as output
pinMode(switchTwo, INPUT_PULLUP); // Set switchPin as input with internal pull-up resistor
pinMode(lightTwo, OUTPUT); // Set relayPin as output
pinMode(switchThr, INPUT_PULLUP); // Set switchPin as input with internal pull-up resistor
pinMode(lightThr, OUTPUT); // Set relayPin as output
pinMode(switchFou, INPUT_PULLUP); // Set switchPin as input with internal pull-up resistor
pinMode(lightFou, OUTPUT); // Set relayPin as output
}
void loop() {
int stateOne = digitalRead(switchOne); // Read the state of the switch
int stateTwo = digitalRead(switchTwo); // Read the state of the switch
int stateThr = digitalRead(switchThr); // Read the state of the switch
int stateFou = digitalRead(switchFou); // Read the state of the switch
if (stateOne == LOW) {
digitalWrite(lightOne, HIGH); // Turn on the relay (and the light)
delay(100); // Add a small delay to stabilize the relay (if necessary)
} else {
digitalWrite(lightOne, LOW); // Turn off the relay (and the light)
}
if (stateOne == LOW) {
digitalWrite(lightOne, HIGH); // Turn on the relay (and the light)
delay(100); // Add a small delay to stabilize the relay (if necessary)
} else {
digitalWrite(lightOne, LOW); // Turn off the relay (and the light)
}
if (stateOne == LOW) {
digitalWrite(lightOne, HIGH); // Turn on the relay (and the light)
delay(100); // Add a small delay to stabilize the relay (if necessary)
} else {
digitalWrite(lightOne, LOW); // Turn off the relay (and the light)
}
if (stateOne == LOW) {
digitalWrite(lightOne, HIGH); // Turn on the relay (and the light)
delay(100); // Add a small delay to stabilize the relay (if necessary)
} else {
digitalWrite(lightOne, LOW); // Turn off the relay (and the light)
}
}
All in all, you are trying to programme a button manager.
In general, a button manager must perform the following tasks:
read and debounce buttons
recognise state changes
perform action on state change
These tasks must be performed "simultaneously" if you have more than one button.
My suggestion - my design recipe
In general, arrays and structs are your friends.
Do not duplicate code in your sketch. Write code once - use it multiple times.
You should not use magic numbers. I/O pins love to have a real functional name.
Do you have experience with programming in C++?
The task can easily be realised with an object.
A structured array contains all the information, such as the pin addresses for the I/O devices, as well as the information for timing.
A single service takes care of this information and triggers the desired action.
The structured array makes the sketch scalable until all I/O pins are used up without having to adjust the code for the service.
That's cool, isn't it?
const int switchOne = 22;
const int switchTwo = 28;
const int switchThr = 26;
const int switchFou = 30;
const int lightOne = 3;
const int lightTwo = 5;
const int lightThr = 2;
const int lightFou = 4;
const int bell = 6;
byte lg[] = {lightOne, lightTwo, lightThr, lightFou};
byte sw[] = {switchOne, switchTwo, switchThr, switchFou};
//----------------------------------------------------------------------
void setup() {
for (int i = 0; i < 4; i++)
{
pinMode(sw[i], INPUT_PULLUP); // Set switchPin as input with internal pull-up resistor
pinMode(lg[i], OUTPUT); // Set relayPin as output
}
pinMode(bell, OUTPUT);
}
//----------------------------------------------------------------------
void loop() {
for (int i = 0; i < 4; i++)
{
if (digitalRead(sw[i]) == LOW) {
digitalWrite(lg[i], HIGH); // Turn on the relay (and the light)
delay(100); // Add a small delay to stabilize the relay (if necessary)
} else {
digitalWrite(lg[i], LOW); // Turn off the relay (and the light)
}
}
}
I don't think you posted the code you are talking about.
Your posted code would turn on, or off, only lightOne.
Or you've made some creative mistakes wiring your switches and LEDs.
Please post schematic of your project. Hand drawn is fastest and easiest. Show all the parts and how you've wired them, with attention to sources of power and how power is routed to the parts that need power.