Go Down

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

Webca

HazardsMind

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

And thanks for all your help:)

MaJiG

Your new initialization code:

Code: [Select]

byte lastbuttonState = 0; // new global variable


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

Please post your entire code...

HazardsMind


Your new initialization code:

Code: [Select]

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.
Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

MaJiG


Webca

I didnt get it to work :(

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 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
    }


}

Go Up