And I`m back with another problem... I got the code to work and the sketch runs good, but not great...
When I power up or reset it still runs the sketch without me doing anything, but I found out that it only happens when I have a wire connected to my buttonpin (pin2), it doesn`t even have to be connected to the button or anything else, just as long as there is a wire on that pin, the sketch runs.
The same thing happens if the button is connected, but if I remove the wire from the pin on the board, it works fine...
Did you use pinMode (2,INPUT_PULLUP);, not just INPUT?
The pin seems to be floating. That means the input is undefined and reads any signal it catches through the air.
Insert
frodv:
I mean, if I run it and suddenly something goes wrong, or I just want to abort. Is there another way to stop it, or is pressing the reset button the best option?
Hitting reset is a very inelegant (that's a polite way of saying crap ) way of aborting something, even with the earlier method of then trapping the sketch in setup() after that.
You should consider the circumstances under which you may want to abort (like, what's likely to go wrong or make you have a change of heart?), and cater for those in your program.
I'd go for a state machine type approach, where in any state only certain buttons are checked say in a switch..case, and your "abort" button being pressed does an orderly exit from the current state and takes you back to (say) an idle state.
olf2012:
Did you use pinMode (2,INPUT_PULLUP);, not just INPUT?
The pin seems to be floating. That means the input is undefined and reads any signal it catches through the air.
Insert
Can the solution be as simple as that I have forgotten to use common ground on the arduino and power supply?
I have some relays that runs on 5v+ and they switches on/off some other stuff that runs on 12v+, I also use the same 12v power supply for the Arduino Uno.
frodv:
Can the solution be as simple as that I have forgotten to use common ground on the arduino and power supply?
I have some relays that runs on 5v+ and they switches on/off some other stuff that runs on 12v+, I also use the same 12v power supply for the Arduino Uno.