Why does this code loaded on a Nano knockoff work sometimes, if I clearly missed a section of code?
int LEDPin = 9;
int ButtonPin = 8;
int RelayPin = 2;
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LEDPin, OUTPUT);
pinMode(ButtonPin, INPUT); //Set the switch's pin to be an input with a pullup (default is HIGH)
pinMode(RelayPin, OUTPUT);
digitalWrite(RelayPin, HIGH);
}
I wrote some Arduino code to control a relay when a button is pressed. It worked on the bench at home but in the lab it didn't work. Two different breadboard circuits same Nano. The issue was I failed to pinMode the OUTPUT to the relay shown in red.
My question to the aficionados of Arduino, those individuals with much more knowledge and experience with Arduinos than moi, is why did it work? Uploading and compiling the code produced no warnings. At least none that I have become accustome to making. On one breadboard layout, the circuit worked but on another breadboard layout, it did not. I sheepishly confess it took me a few cycles to figure out what I had done wrong. This required multiple trips between the lab and home so I know it wasn't just a freak one-time event.
Any insight you can share loads up my toolkit for the next time.