Greetings all,
I am 2 days into the world of Arduino and have learned enough to get a simple sketch running on my UNO R3. The code turns on and off 3 different relay boards in a sequence with some delays.
The issue i have in when I plug power up or reload the sketch to the UNO it energizes the relays quickly
then leaves them on...before i press the start/stop (buttonpin)..any thoughts? The relay board are active low so the energize when a logic low is on the input pin.
Shetch 1
// This sequence relays on an off
const int buttonPin = 2; // assigns digital input 2 the name buttonPin
const int ledPin1 = 11; //assigns digital output 11 the name ledPin1
const int ledPin2 = 12; //assigns digital output 12 the name ledPin2
const int ledPin3 = 13; //assigns digital output 13 the name ledPin3
int x = 1;
// variables will change:
int buttonState = 0;
int oldButtonState = LOW;
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop()
{
// Get the current state of the button
int newButtonState = digitalRead(buttonPin);
// Has the button gone high since we last read it?
if (newButtonState == HIGH && oldButtonState == LOW) {
if (x == 0) {
// Toggle on led1
digitalWrite(ledPin1, HIGH); // turn on fan
delay(5000); // delay xxxx sec
// Toggle on led2
digitalWrite(ledPin2, HIGH); // turn on pump
delay(100); // delay xxxx sec
// Toggle on led3
digitalWrite(ledPin3, LOW); // turn on ignition
delay(2000); // ignition on time xxxx sec
digitalWrite(ledPin3, HIGH); // turn off ignition
x = 1; // toggle PButton state indicator to 1
} else { // if outputs 1 2 3 on turn off
// shut down sequence
digitalWrite(ledPin2, LOW); // turn off pump
delay(5000); // cool down delay xxxx
digitalWrite(ledPin1, LOW); // turn off fan
delay(1000);
x = 0; // toggle PButton state indicator to 0
}
}
// Store the button's state so we can tell if it's changed next time round
oldButtonState = newButtonState;
}