Push button pressed before loop

hey,

i clearly suck at programming my Arduino as ive been trying o get this to work for weeks now.

i am trying stop my loop from running until a button is pressed, best ive got so far is stopping the script until the button is held but im more wanting one button press then the loop to run forever.

my setup is basically one line in (button) and one line out.

thanks for any help you guys can give.

(deleted)

Declare a global or static boolean variable and set it to false.
In loop(), read the button pin and when you detect that it has been pressed set the boolean to true
Put any code that is to run only after the button press inside an if test so that it only runs when the boolean is true.

No need for the while loop

void loop(){

   if (digitalRead(button)==HIGH){

       // do things
       // try it with the code from blink without delay
   }  else {

      // things for when pin is low

   }

   // do many other things which you can not do if you jyst wait in the while loop!

}

Mark

im more wanting one button press then the loop to run forever

Note that OP has specified that "the loop" should run forever following one button press. As I read that the loop should continue to run after the button is released.

Perhaps the OP could clarify the requirement.

basically thi is the idea i have which may help.

i need a system that can blink and led and upon a button being pressed the script will stop the led from blinking then will apply power to another pin for use in a relay.the relay will then allow power to pass to an alarm.

heres whats set up where.

pin 13 = alarm
pin 6 = button
pin 2 = LED

hopefully i havent just confused thing further. Both of those examples i have tried and my script seems to ignore the button press and just runs the loop as if it was never there.

hopefully i havent just confused thing further. Both of those examples i have tried and my script seems to ignore the button press and just runs the loop as if it was never there.

Post your code!!!

Mark

well its here, so far all it does it set alarm to high, delay, low and repeat. it ignores the button

int button = A2;
int alarm = 13;

void setup()
{
pinMode (alarm, OUTPUT);
}

void loop() {
while(digitalRead(button) == HIGH) {
digitalWrite (alarm, HIGH);
delay (1000);
digitalWrite (alarm, LOW);}

}

And what happens when you tie pin A2 to ground?

(good things will happen when you start using code tags)

with that current script it completely ignores the button

Did you hardwire A2 to ground?

Why don't you use a digital pin for your button?

i could do in theory, my wiring to the arduino is based on many different forums trying to get this to work. its changed a lot. When i was using a digital pin i had the same issues though. script runs as if the button was never there and no change to how it runs with the button pressed or not pressed.

i could do in theory

I'm not interested in theory here.

(deleted)

Do you have an image of your circuit?

hellinick:
i need a system that can blink and led and upon a button being pressed the script will stop the led from blinking then will apply power to another pin

My code below does what you ask for there, using BlinkWithOutDelay so it can still listen for the button press. Button is to ground, and pinMode sets INPUT_PULLUP.

You made no mention of how to turn the alarm off, so as it’s written it will blink forever until you press the button but after that there’s no way out. I imagine you need some way to silence the alarm…

//http://forum.arduino.cc/index.php?topic=456496.0

//starts with an led blinking, waits for button
//when button pressed, blinking stops and another led goes solid on

//uses blinkwithoutdelay approach
//button is pin to ground with a pullup

#define ISPRESSED LOW

byte alarmPin = 12;
byte buttonPin = 6;
byte ledPin = 2;
byte ledState = LOW;

unsigned long currentMillis;
unsigned long previousMillis = 0;
long blinkInterval = 500;

bool alarmEnabled = false;

void setup() {
  // put your setup code here, to run once:

  pinMode (alarmPin, OUTPUT);
  pinMode (ledPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (digitalRead(buttonPin) == ISPRESSED)
  {
    alarmEnabled = true;
  }

  if (alarmEnabled)
  {
    soundTheAlarm();
  }
  else
  {
    doBlink();
  }

} //loop

void doBlink()
{
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= blinkInterval)
  {
    previousMillis = currentMillis;
    ledState = !ledState;
    digitalWrite(ledPin, ledState);

  }
}//doBlink

void soundTheAlarm()
{
  digitalWrite(alarmPin, HIGH);
  ledState = LOW;
  digitalWrite(ledPin, ledState);
}//soundTheAlarm