Hi all,
I just discovered what Arduino can do for my Postwar (1945-1969) Lionel train layout and also my HO layout. I am now trying to learn to code basic things. Not my strong suite. I am a very hands on mechanical person.
I am creating a very basic blinking code to flash an old Lionel (incandescent bulbs) crossing signal. The bulbs will alternate on/off every 600ms and will be triggered by an IR sensor.
I actually have this working. I can block the sensor and the relay opens and closes as it should until I remove my hand from the sensor.
The only thing I am trying to figure out is how to make it so the relay starts in an off position. Meaning it is not ON to start with. The light on the board should be off and not on. I don't want the relay in an almost constant coil powered state.
My hardware is an Elegoo Uno R3, JBtek 8 ch 5v Arduino relay board, a breadboard and a GikFun IR sensor.
When the Arduino first boots up the relay is in an ON condition. If I trigger the circuit with the sensor it will then always cycle to the OFF condition once the trigger (my hand) is removed. I just cannot figure out how to get the relay to be in the OFF condition upon bootup.
I have tried different if val statements to try and get the relay to be in the OFF condition upon bootup, but I just cannot figure it out.
I have ordered 3 Elegoo Nano's and a 5 pack of 5v relay boards to actually implement this project once working plus have extra stuff for other train automations.
Any help would be greatly appreciated. I'm sure this is extremely basic for those of you who really understand the code.
Here is my code:
Again, it is working as I want except the relay always starts in the ON condition on boot and until triggered the first time and then it always goes to the OFF condition after the trigger is removed.
int sensor = A0;
int relay = 2;
void setup() {
pinMode(relay, OUTPUT);
Serial.begin(9600);
}
void loop() {
int valA1 = analogRead(sensor);
Serial.println(valA1);
if (valA1 < 500) {
digitalWrite(relay, LOW);
delay(600);
digitalWrite(relay, HIGH);
delay(600);
}
}