Hello,
I am not electronics professional but rather enthusiast, so do not judge if the problem is so obvious
I have an arduino UNO that has has a push up button and a led, and the design is simple;
when I push the button, it will change the state of the led, meaning if the led is off is goes on, and vice versa. So far so good, I am quite happy.
To power the arduino I am using an adapter that changes the AC current of 240V to 12V DC, and the source of the power comes from the electrical switch that I use to power ON/OFF an electrical light (this is because I have no source of power nearby - no power socket, and I did not wanted to use batteries to power the arduino), but of course I am using the source of electricity before the switch, have a look at the picture below:
the code:
bool pressed = 0;
void setup() {
pinMode(2, INPUT_PULLUP); //button
pinMode(5, OUTPUT); //led
}
void loop() {
int buttonValue = digitalRead(2);
if (buttonValue == LOW){
pressed = !pressed;
digitalWrite(5, pressed);
delay(500);
}
}
To conclude, my problem is that the button is triggered automatically sometimes (only sometimes - not always) when I switch the light ON/OFF, which it should not, since I am not directly depending on the electrical switch.
How come the switch triggers the button on arduino, when it is not directly linked, and I am also using the pull_up resistor macro for the button?
Can someone say something about this issue, or maybe not