Go Down

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

Webca

I still cant get it to work...

Here is the code:

Code: [Select]

#include <Wire.h>
#include <LiquidCrystal.h>

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

boolean test = false;
boolean start = false;
byte lastbuttonState2 = 0;          // new global variable
byte buttonState1 = 0;
byte buttonState2 = 0;

void setup()
{
  Serial.begin(9600);

  lcd.begin(16, 4);
  // 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 == HIGH){
      start = true;
      startTime = millis();
      lastButtonState = buttonState2;
    }
 
  if(start == true){
  elapsedTime =   millis() - startTime;              // store elapsed time
    lastButtonState = buttonState;                     // store buttonState in lastButtonState, to compare next time

    lcd.setCursor(0,2);

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

    lcd.print(".");                             

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

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

    lcd.print(fractional);  // print fractional part of time
  }
  if(test == true){
    if(buttonState1 == HIGH)
    start == true;
    else;{}
    }
  }
}

HazardsMind


You've got a delay(20) in there. Your minimum response is over 20 ms....

It'd be smoother to loop while ( now - startPress > 10 ) true after 10 ms continuous contact, Arduino can check that button 8000 times in 10 ms so why have it do nothing for 20 ms?



I ment it to be 10 but I pressed 2 instead, and just never bothered to change it. It's a crude way to do it, I know, but it works as a temporary debounce.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

HazardsMind

#32
Feb 02, 2013, 12:11 am Last Edit: Feb 02, 2013, 01:09 am by HazardsMind Reason: 1

I still cant get it to work...

Here is the code:

Code: [Select]

#include <Wire.h>
#include <LiquidCrystal.h>

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

boolean test = false;
boolean start = false;
byte lastbuttonState2 = 0;          // new global variable
byte buttonState1 = 0;
byte buttonState2 = 0;

void setup()
{
  Serial.begin(9600);

  lcd.begin(16, 4);
  // 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 == HIGH){
      start = true;
      startTime = millis();
      lastButtonState = buttonState2;
    }
 
  if(start == true){
  elapsedTime =   millis() - startTime;              // store elapsed time
    lastButtonState = buttonState;                     // store buttonState in lastButtonState, to compare next time

    lcd.setCursor(0,2);

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

    lcd.print(".");                             

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

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

    lcd.print(fractional);  // print fractional part of time
  }
  if(test == true){
    if(buttonState1 == HIGH)
    start == true;
    else;{}
    }
  }
}


I guess my hint wasn't easy enough to understand. It's only 3 new lines you need to add, along with any brackets.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

Webca

I am new at this so the learning curve is pretty steep.

couldnt you add the 3 lines in the code for me?

GoForSmoke

#34
Feb 02, 2013, 02:44 am Last Edit: Feb 02, 2013, 02:48 am by GoForSmoke Reason: 1
Time debounced button presses, start to start not release to start.  
LED 13 lights when the button is pressed. TX flashes when debounce is done.

Tested with UNO 3, serial monitor and jumper from pin 9 to gnd as button.


Code: [Select]

//  Time debounced button presses, start to start not release to start.  

//#include <Wire.h>
//#include <LiquidCrystal.h>

// LiquidCrystal lcd(12, 11, 5, 4, 3, 2);


#define LEDPIN  13                  // LED connected to digital pin 13
#define BPIN 9                 // button on pin 4

byte buttonState;                    // variable to store button state
unsigned long debounce = 0UL; // variable to store last time LED was updated
unsigned long Timer ;         // start/elapsed time for stop watch

void setup()
{
 Serial.begin(9600);

 //lcd.begin(16, 4);
 // Print a message to the LCD.
 //lcd.print("Test");

 Serial.println("Test");

 pinMode( LEDPIN, OUTPUT );         // sets the digital pin as output
 digitalWrite( LEDPIN, LOW );   // turn on pullup resistors. Wire button so that press shorts pin to ground.

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

 Timer = millis(); // Timer now is start millis
}

void loop()
{
 digitalWrite( LEDPIN, LOW );

 while ( digitalRead( BPIN )); // button is not pressed

 digitalWrite( LEDPIN, HIGH );

 debounce = millis();
 while (( millis() - debounce < 10UL ) &&
   ( buttonState = !digitalRead( BPIN ))); // button pressed && time not up

 if ( buttonState )  // success if contact holds till time is up,
 {
   Timer = millis() - Timer; // Timer now is elapsed millis

   /*
     lcd.setCursor(0,2);
   
    lcd.print( " " );
    // routine to report elapsed time
    lcd.print( Timer / 1000UL ); // convert to seconds and print    
    lcd.print( "." );                            
   
    Timer = Timer % 1000UL; // Timer now is remaining millis < 1 sec
   
    if ( Timer < 100UL )
    {
    lcd.print( "0" );    
    if ( Timer < 10UL )
    {    
    lcd.print( "0" );
    }
    }    
   
    lcd.print( Timer );  
    */
   Serial.println();

   Serial.print( " " );
   // routine to report elapsed time
   Serial.print( Timer / 1000UL );         // divide by 1000 to convert to seconds - then cast to an int to print

   Serial.print( "." );                            

   Timer = Timer % 1000UL; // Timer now is remaining millis < 1 sec

   if ( Timer < 100UL )
   {
     Serial.print( "0" );    
     if ( Timer < 10UL )
     {    
       Serial.print( "0" );
     }
   }    

   Serial.println( Timer );  

   Timer = millis(); // start millis before the button is released

   if ( !digitalRead( BPIN ))
   {
     while ( !digitalRead( BPIN )); // wait until BPIN is released
   }
 }
}

Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

Webca

Thanks to everyone for helping me in this mather, and a special thanks to "HazardsMind" for solving my problem.

Here is the functioning code HazardsMind gave me:

Code: [Select]

#include <Wire.h>
#include <LiquidCrystal.h>

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

boolean test = false;
boolean start = false;
boolean lockout = false;            //**NEW BOOLEAN**
byte lastbuttonState2 = 0;          // new global variable
byte buttonState1 = 0;
byte buttonState2 = 0;

void setup()
{
   Serial.begin(9600);

   lcd.begin(16, 4);
   // 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()
{  // no more debounce
   buttonState2 = digitalRead(buttonPin);
   if(buttonState2 == HIGH){
     if(lockout == false){ // #1
       start = true;
       startTime = millis();
       lastButtonState = buttonState2;
       lockout = true; // (The lockout) this will make sure it does not go back to this IF statement
     }
   }
   if(buttonState2 == HIGH && start == true){ // #2
     elapsedTime =   millis() - startTime;              // store elapsed time
     lastButtonState = buttonState;                     // store buttonState in lastButtonState, to compare next time

     lcd.setCursor(0,2);

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

     lcd.print(".");

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

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

     lcd.print(fractional);  // print fractional part of time
   }
   else lcd.clear(); // #3
}

GoForSmoke

Hey, at least he got rid of the delays. Now it's responsive!

He's still working millis() with signed longs but you won't be running this any 24 days straight so no problem.

It's still good practice (so you don't trip up later doing "what worked" before) to use unsigned long or unsigned long long to work with millis() and micros(). Both of those functions return unsigned long which is clue #1 about best use of those values.

Micros() rolls over in 1.193... hours using unsigned long. It turns negative in half that with signed. Millis() can measure intervals to over 48 days because it takes 49.7... to overflow using unsigned long. With signed, just under 25 days to find that bug. With unsigned long long the longest interval is over 4 billion times as large, counting millisecs. The sun is not that old! With signed... you should live to see the bug!
Or of course knowing about rollover the clever programmer writes some logic to handle it. But why do that when there's no need for extra code at all with unsigned long.
If signed integers are like measuring sticks with 2 ends for - and +, unsigned integers are circles like clock faces. You measure around the circle and don't worry about crossing zero, it comes out in the math always as a positive. The sign is in the direction from-to you measured, not in the result, unsigned don't have sign.



Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

Go Up