Stop sketch on power up/reset

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...

Anye ideas?

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

  Serial.println((digitalRead(2) == HIGH) ? "1" : "0");

into your loop and observe the serial monitor as you connect/disconnect the cable or button

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 :wink: ) 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

  Serial.println((digitalRead(2) == HIGH) ? "1" : "0");

into your loop and observe the serial monitor as you connect/disconnect the cable or button

It looks like you`re correct.... watching the serial monitor it sometimes is a "0" and sometimes a "1".

any way to fix this?

Do you definitely have the pinMode as INPUT_PULLUP?

here is the code

// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int rele1 = 3;         // rele til flaskefyller
const int rele2 = 4;         // ventil for gass til purging
const int rele3 = 5;         // ventil til øl
const int rele4 = 6;         // rele til sideforskyvning
const int rele5 = 7;         // rele til korker
const int rele6 = 8;         // ikke i bruk

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
void setup() 
{

  Serial.begin(9600);
  
  // initialize the LED pin as an output:
  pinMode(rele1, OUTPUT);
  pinMode(rele2, OUTPUT);
  pinMode(rele3, OUTPUT);
  pinMode(rele4, OUTPUT);
  pinMode(rele5, OUTPUT);
  pinMode(rele6, OUTPUT);
  
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT_PULLUP);

  digitalWrite(rele1, LOW);   // holder flaskefyller oppe
  digitalWrite(rele2, LOW);   // gass stengt
  digitalWrite(rele3, LOW);   // øl stengt
  digitalWrite(rele4, LOW);   // holder stempel for flaskefyller tilbake
  digitalWrite(rele5, LOW);   // holder korker oppe
  
} 
void(*resetFunk)(void) = 0; 

void loop() {

  do
{
  //empty loop
} while (digitalRead(buttonPin)==HIGH);

Serial.println((digitalRead(2) == HIGH) ? "1" : "0");

  delay(1000);
  digitalWrite(rele1, HIGH);   // senker flaskefyller
  delay(3000);
  digitalWrite(rele2, HIGH);   // åpner purging
  delay(5000);                  
  digitalWrite(rele2, LOW);    // stenger purging
  delay(2000);
  digitalWrite(rele3, HIGH);   // åpner fylling
  delay(5000);                  
  digitalWrite(rele3, LOW);    // stenger fylling
  delay(2000);
  digitalWrite(rele1, LOW);    // hever flaskefyller
  delay(3000);  
  digitalWrite(rele4, HIGH);   // flytter flasker til korker
  delay(5000);
  digitalWrite(rele4, LOW);   // flytter stempel tilbake
  delay(5000);
  digitalWrite(rele5, HIGH);   // kjører korker ned
  delay(5000);
  digitalWrite(rele5, LOW);    // kjører korker opp
  delay(2000);
 // digitalWrite(rele6, HIGH);   // - ikke i bruk -
 //delay(500);
 // digitalWrite(rele6, LOW);    // - ikke i bruk -
 //delay(1000);

  resetFunk();
  delay(1000);

  for(;;);
}

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.

Yes

.

I have the very same question to ask with my sketch. Should i post a new topic here with the same title or should i just add it here.
Thanks

Make you own post.

.