Pages: [1] 2 3   Go Down
Author Topic: Cant stop code from being run after reset  (Read 2040 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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
Logged

Queens, New York
Online Online
Faraday Member
**
Karma: 84
Posts: 3422
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: January 31, 2013, 03:21:42 pm by HazardsMind » Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Queens, New York
Online Online
Faraday Member
**
Karma: 84
Posts: 3422
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I changed the code to:

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

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

}

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

Lost Wages
Offline Offline
Full Member
***
Karma: 11
Posts: 103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Did you change the button logic in activate() as well?

Please re-pot your entire code.
Logged

Queens, New York
Online Online
Faraday Member
**
Karma: 84
Posts: 3422
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I noticed that your not using a latch. It is possible that if you press the button the first time, your not giving it time to go back HIGH, so it is carring over to the activate function.
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How can i implement a latch?
Logged

Queens, New York
Online Online
Faraday Member
**
Karma: 84
Posts: 3422
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Code:
byte lastbuttonState = 0; // new global variable

void loop() {
buttonState = digitalRead(buttonPin);
// normal debounce code here

  if(buttonState != lastbuttonState) {
    activate();
    lastbuttonState = buttonState;
   }
}

Alternative:
Code:
byte lastbuttonState = 0; // new global variable
byte buttonState2 = 0;
void loop() {

buttonState1 = digitalRead(buttonPin);
delay(20);
buttonState2 = digitalRead(buttonPin);

  if(buttonState1 == buttonState2) {
    if(buttonState2 != lastbuttonState) {
     activate();
     lastbuttonState = buttonState2;
    }
  }
}

dont forget the setup()
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12551
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I want the sketch to stop and wait for a button press.

The simplest way to do that is to put a loop in setup() that reads the switch state repeatedly until the button press is detected.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

HazardsMind

I ill try that tomorrow, and i will let you know how it goes..

And thanks for all your help:)
Logged

Lost Wages
Offline Offline
Full Member
***
Karma: 11
Posts: 103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your new initialization code:

Code:
byte lastbuttonState = 0; // new global variable

initializes to the button being pressed in the new scheme, yes?

Please post your entire code...
Logged

Queens, New York
Online Online
Faraday Member
**
Karma: 84
Posts: 3422
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your new initialization code:

Code:
byte lastbuttonState = 0; // new global variable

initializes to the button being pressed in the new scheme, yes?

Please post your entire code...

My code? It's just a snippet from his code that I made. Just replace his loop function with one of mine.
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Lost Wages
Offline Offline
Full Member
***
Karma: 11
Posts: 103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No, I meant Webca.

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I didnt get it to work smiley-sad

Here is my code:

Code:
#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 9                 // 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
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

byte lastbuttonState2 = 0; // new global variable
byte buttonState1 = 0;
byte buttonState2 = 0;

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

    pinMode(ledPin, OUTPUT);         // sets the digital pin as output

    pinMode(buttonPin, INPUT);       // not really necessary, pins default to INPUT anyway
    digitalWrite(buttonPin, HIGH);   // turn on pullup resistors. Wire button so that press shorts pin to ground.

}

void loop()
{
 
   
buttonState1 = digitalRead(buttonPin);
delay(20);
buttonState2 = digitalRead(buttonPin);

  if(buttonState1 == buttonState2) {
    if(buttonState2 != lastButtonState) {
     activate();
     lastButtonState = buttonState2;
    }
  }

   

}


void activate()
{
     
    buttonState = digitalRead(buttonPin);                   // read the button state and store

    if (buttonState == 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 (buttonState == LOW){     
       // 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(".");                             


        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
    }


}
Logged

Pages: [1] 2 3   Go Up
Jump to: