Show Posts
Pages: 1 [2]
16  Using Arduino / Programming Questions / Re: Cant stop code from being run after reset on: February 01, 2013, 03:14:07 pm
I am trying your sketch and what happens is it shows the counter in the display and when i click the button it resets the counter.

I want the counter to run in the background (not visible in the display) and when i click on the button after for example 5 seconds it shows 5.000 in the display and if i then click the button after 2 seconds again i want it to show 7.000 in the display.

I feel we are getting close though, and i really appreciate you sticking with me on this HazardsMind:)
17  Using Arduino / Programming Questions / Re: Cant stop code from being run after reset on: February 01, 2013, 01:40:24 pm
That is correct, i want it to appear to start when i press the button..

What do i have to do to the code to get it working as it should?
18  Using Arduino / Programming Questions / Re: Cant stop code from being run after reset on: February 01, 2013, 11:14:35 am
HazardsMind-

It has to be like this " elapsedTime =   millis(); - startTime; ".

It is supposed to be a regular timer that works in the "background" and when i press the button i get a readout of how far it has come
19  Using Arduino / Programming Questions / Re: Cant stop code from being run after reset on: February 01, 2013, 05:51:19 am
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
    }


}
20  Using Arduino / Programming Questions / Re: Cant stop code from being run after reset on: January 31, 2013, 04:41:14 pm
HazardsMind

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

And thanks for all your help:)
21  Using Arduino / Programming Questions / Re: Cant stop code from being run after reset on: January 31, 2013, 04:22:36 pm
How can i implement a latch?
22  Using Arduino / Programming Questions / Re: Cant stop code from being run after reset on: January 31, 2013, 03:53:25 pm
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...
23  Using Arduino / Programming Questions / Re: Cant stop code from being run after reset on: January 31, 2013, 03:30:47 pm
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...
24  Using Arduino / Programming Questions / Cant stop code from being run after reset on: January 31, 2013, 03:16:16 pm
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
Pages: 1 [2]