Go Down

Topic: Cant stop code from being run after reset (Read 2 times) previous topic - next topic

Webca

Hey,

My project:
Make a simple timer that starts at the click of a button. My sketch works, but it starts immediately after reset.
I want the sketch to stop and wait for a button press.

If anyone could help, i would be very grateful:)

Here is my code:

Code: [Select]

#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);


#define ledPin  13                  // LED connected to digital pin 13
#define buttonPin 8                 // button on pin 4

int value = LOW;                    // previous value of the LED
int buttonState;                    // variable to store button state
int lastButtonState;                // variable to store last button state
//int blinking;                       // condition for blinking - timer is timing
long interval = 100;                // blink interval - change to suit
long previousMillis = 0;            // variable to store last time LED was updated
long startTime ;                    // start time for stop watch
long elapsedTime ;                  // elapsed time for stop watch
int fractional;                     // variable used to store fractional part of time



void setup()
{
    Serial.begin(9600);
   
      lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Test");

    pinMode(ledPin, OUTPUT);       

    pinMode(buttonPin, INPUT);       
    digitalWrite(buttonPin, HIGH);   

}

void loop()
{
     
      buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) {     
   
    activate();
  }
  else {
 
    digitalWrite(13, HIGH);
  }

}





void activate()
{
     

    buttonState = digitalRead(buttonPin);               

    if (buttonPin == HIGH){   
       // if true then found a new button press while clock is not running - start the clock

       startTime = millis();                                   // store the start time
       delay(5);                                               // short delay to debounce switch
       lastButtonState = buttonState;                          // store buttonState in lastButtonState, to compare next time

    }

    else if (lastButtonState == HIGH){     
       // if true then found a new button press while clock is running - stop the clock and report

         elapsedTime =   millis(); //- startTime;              // store elapsed time
         lastButtonState = buttonState;                     // store buttonState in lastButtonState, to compare next time

  lcd.setCursor(0,1);

  lcd.print("   ");
        // routine to report elapsed time
         lcd.print( (int)(elapsedTime / 1000L));         // divide by 1000 to convert to seconds - then cast to an int to print

         lcd.print(".");                             // print decimal point

         
        fractional = (int)(elapsedTime % 1000L);

        if (fractional == 0)
           lcd.print("000");     
        else if (fractional < 10)   
           lcd.print("00");       
        else if (fractional < 100)
           lcd.print("0");       

        lcd.println(fractional);  // print fractional part of time

    }

    else{
       lastButtonState = buttonState;                         // store buttonState in lastButtonState, to compare next time

HazardsMind

#1
Jan 31, 2013, 09:19 pm Last Edit: Jan 31, 2013, 09:21 pm by HazardsMind Reason: 1
Does your button have a pull down resistor? That is not the proper way to debounce a button, look at the debounce example. Also you never turn the LED off.
Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

Webca

Yes it has a pulldown resistor.

The led part of the sketch is obsolete, and will be removed.

It might not be the right way to debounce, but the code in the activate() function works.

Its the part in the void loop() that wont stop the activate() function that is bothering me...

HazardsMind

Well if it keeps going to the activate function, then it is seeing that the button is high, otherwise it will not do that if statement. Check your wiring again just to be sure.
Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

Webca

You where correct. The butten is HIGH when not pressed and LOW when pressed.

I changed the code to:

Code: [Select]

void loop()
{
     
      buttonState = digitalRead(buttonPin);

  if (buttonState == LOW) {     
   
   
    activate();
  }
  else {
 
    digitalWrite(13, HIGH);
  }

}


But it still wont stop the code from being run...

Go Up