Hello I'm new to Arduino and i was wondering how to listen for button press in Setup and after button press start loop. Can anyone help me ???
Is there any reason you can't wait for the button press in the main loop?
Yeah Because i am going to put Arduino on a model rocket so i will have to make my rocket to start a countdown a single time and then ignite engine and in the loop there is a sensors that monitor all data
Blackfin:
Is there any reason you can't wait for the button press in the main loop?
Blackfin:
Is there any reason you can't wait for the button press in the main loop?
Yeah Because i am going to put Arduino on a model rocket so i will have to make my rocket to start a countdown a single time and then ignite engine and in the loop there is a sensors that monitor all data
I'll just leave this here for you to consider.
const int pinButton = 9;
const int pinFireEngine = 10;
#define WAIT_FOR_BUTTON 0
#define COUNTDOWN 1
#define LAUNCH 2
#define READ_SENSORS 3
#define LANDING 4
void setup()
{
pinMode( pinButton, INPUT_PULLUP );
digitalWrite( pinFireEngine, LOW );
pinMode( pinFireEngine, OUTPUT );
}//setup
void loop()
{
LaunchSequence();
}//loop
void LaunchSequence( void )
{
static byte
stateLaunch = WAIT_FOR_BUTTON;
static int
nCount;
static unsigned long
timeCountdown;
unsigned long
timeNow;
switch( stateLaunch )
{
case WAIT_FOR_BUTTON:
if( digitalRead(pinButton) == LOW )
{
timeCountdown = millis();
nCount = 10;
stateLaunch = COUNTDOWN;
}//if
break;
case COUNTDOWN:
//if you want to transmit a 10-9-8...
//countdown, this is okay
//you could also simply set timCountdown
//to 10000 and wait for that to elapse
timeNow = millis();
if( (timeNow - timeCountdown) >= 1000 )
{
nCount--;
timeCountdown = timeNow;
if( nCount == 0 )
stateLaunch = LAUNCH;
}//if
break;
case LAUNCH:
//start the engine and launch
digitalWrite( pinFireEngine, HIGH );
stateLaunch = READ_SENSORS;
break;
case READ_SENSORS:
//read and log (or whatever) your sensors
if( done sensing )
stateLaunch = LANDING;
break;
case LANDING:
//do nothing. Flight is over
break;
}//switch
}//LaunchSequence
Thanks everyone
This simple code in setup() will do what you want (if I understand the requirement)
while( digitalRead( startButtonPin ) == HIGH ) { // assumes it will be LOW when pressed
}
...R
Robin2:
This simple code in setup() will do what you want (if I understand the requirement)while( digitalRead( startButtonPin ) == HIGH ) { // assumes it will be LOW when pressed
}
...R
thanks