Stop sketch on power up/reset

Hi, I'm pretty new at this arduino thing, and I have a couple of questions. Is it so that the sketch runs automatically when the arduino is powered on or/and if I push the reset button?

If so, is it a way to prevent this?
I have a sketch, but I don't want to run until I push a button.

The sketch works fine, and the button works, but I don't want it to run before I tell it to.

The sketch only runs one time, and then I have to push the button for it to run again.

Absolutely true about the sketch running every time the Arduino is powered up, or reset.

To get the sketch to not run until you press the button, you will have to change your sketch.

Paul

Only run your code when a flag variable is 'true'.

Set the flag variable to 'true' when your switch changes state eg: from not pressed to pressed .

When the code has run, set the flag variable to 'false'.

.

As I mentioned, I'm new at this. Is there a simple code to do this, or is it complicated?

// define your variables here . . . . 

void setup()
{
  // do some stuff
}

void loop()
{
 currentState = digitalRead(switch);
 
 if( lastState != currentState)
  {
     lastState = currentState;
     if(currentState == LOW)  //switch has gone from not pressed to pressed?
     {
        runFlag = true;
     }
  }

  if( runFlag == true)
  {
    //code to run
  
    runFlag = false;
  }

} // END of    l o o p (  )

Thanks, I guess. I'm not sure I understand it, but I'll try it and see what happens :slight_smile:

I would try do..while
as first few lines of void loop() to halt until a button is pressed.

Disclaimer - written from memory, might contain errors. :smiling_imp:

void setup(){
pinMode (2,INPUT_PULLUP); //Connect your button between digital pin2 and ground.
//Your other setup code here
}

void loop(){
do
{
  //empty loop 
} while (digitalRead(2)==HIGH);
//remaining code here
}

I didt get it to work, becaus I dont understand it.

Here is my code, can you see if something can be done with it?

Nevermind the writing, it`s some norwegian and some english :slight_smile:

// constants won't change. They're used here to
// 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() {
  // 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);

  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() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:

  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(;;);
}
}

why not take a programming course? some easy computer language just to learn how to create algorithms

Not sure why everyone is putting pause code in loop(). One line in setup will pause the program at the beginning.

void setup(){
pinMode (2,INPUT_PULLUP); //Connect your button between digital pin2 and ground.
while (digitalRead(2)==HIGH);
//Your other setup code here
}

Such a simple thing, really.

I tried this.... it runs the sketch at power up.

My bad... I tried it with the wiring as it was, with the resistor and 5v+. When I wired the button to ground as you said it worked... thanks.

Another question comes to mind as I`m trying to learn a bit about this.

Will it damage the arduino board if I frequently press the reset button? or is it another way to stop the sketch mid-run?

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?

aarg:
Not sure why everyone is putting pause code in loop(). One line in setup will pause the program at the beginning.

void setup(){

pinMode (2,INPUT_PULLUP); //Connect your button between digital pin2 and ground.
while (digitalRead(2)==HIGH);
//Your other setup code here
}




Such a simple thing, really.

Not very effective to pause in setup when you want to pause between every iteration of loop...

Seems perfectly OK to me. The goal was to prevent the sketch from starting right away at power up or reset

frodv:
Another question comes to mind as I`m trying to learn a bit about this.

Will it damage the arduino board if I frequently press the reset button? or is it another way to stop the sketch mid-run?

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?

This is not a PC. There is no hard drive and no file system which can be corrupted/damaged. Just press reset whenever you like. There is no other way to stop the controller except removing power or putting it into sleep mode

OK thanks.... I Know it's not a PC, but it is an electrical component. Even a lightbulb that is designes to be switched on and off will break sooner or later.

aarg:
Not sure why everyone is putting pause code in loop(). One line in setup will pause the program at the beginning.

void setup(){

pinMode (2,INPUT_PULLUP); //Connect your button between digital pin2 and ground.
while (digitalRead(2)==HIGH);
//Your other setup code here
}




Such a simple thing, really.

Because you just know the next thing will be: how do I add code to time a soft boiled egg and then code to control the watering of my lawn also . . .

.

larryd:
Because you just know the next thing will be: how do I add code to time a soft boiled egg and then code to control the watering of my lawn also . . .

.

Indeed. I think the requirements seem to have already drifted away from the initial first sentence.

frodv:
OK thanks.... I Know it's not a PC, but it is an electrical component. Even a lightbulb that is designes to be switched on and off will break sooner or later.

Good point. But a lightbulb produces heat, and heating and cooling is what kills it sooner or later. A mere reset is not even an interruption of power. It just tells the processor to jump to adress 0