First of all, I must say hello to all forum mebers!
I have a problem, i'm trying to make a room "weather" controller using arduino. Just testing individual components at this moment before I start final assembly. I just tested controlling some relays and it works well, then tested turning a led on pin 13 on and off using a debounced push button, and that works well too, but when merge it together and try to control the led with the button while relays are beeing switched on and off it won;t work.
Here is the code:
/*
Test code relay board switching and led + button,
r1=12,r2=7,r3=4,l1=13,b1=2.
*/
int relay1 = 12;
int relay2 = 7;
int relay3 = 4;
const int buttonPin = 2;
const int ledPin = 13;
int ledState = HIGH;
int buttonState;
int lastButtonState = LOW;
long lastDebounceTime = 0;
long debounceDelay = 50;
void setup() {
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(buttonPin,INPUT);
pinMode(ledPin,OUTPUT);
digitalWrite(ledPin, ledState);
}
void loop() {
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
ledState = !ledState;
}
}
}
digitalWrite(ledPin, ledState);
lastButtonState = reading;
// RELAYS OFF
digitalWrite(relay1, HIGH);
delay(2000);
digitalWrite(relay2, HIGH);
delay(2000);
digitalWrite(relay3, HIGH);
delay(2000);
// RELAYS ON
digitalWrite(relay1, LOW);
delay(3000);
digitalWrite(relay2, LOW);
delay(3000);
digitalWrite(relay3, LOW);
delay(3000);
}
Another question would be why the relays are on when the pin is LOW and off when pin is HIGH? I'm using a cheap 4 relay board from China.