hi, im playing around the the stopwatch code on the site.
I want an FRS to activate and deactivate the stopwatch on the condition that a push button has been pressed 1st. (i.e standby switch)
the FSR is working atm. but the part of code i have added for the pushbutton has no effect.
if anyone could point me in the right direction mcuh appreciated
many thanks
/* StopWatch
- Paul Badger 2008
- Demonstrates using millis(), pullup resistors,
- making two things happen at once, printing fractions
- Physical setup: momentary switch connected to pin 4, other side connected to ground
- LED with series resistor between pin 13 and ground
*/
#define ledPin 13 // LED connected to digital pin 13
#define fsrPin 2
#define buttonPin 4 // 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 fsrState; // variable to store button state
int lastfsrState;
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
// variable used to store fractional part of time
float t;
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // sets the digital pin as output
pinMode(fsrPin, INPUT); // not really necessary, pins default to INPUT anyway
analogWrite(fsrPin, HIGH); // turn on pullup resistors. Wire button so that press shorts pin to ground.
pinMode(buttonPin,INPUT);
digitalWrite (buttonPin, HIGH);
}
void loop()
{
// check for button press
buttonState = digitalRead(buttonPin); // read the button state and store
fsrState = analogRead (fsrPin);
if (buttonState == LOW && lastButtonState == HIGH && fsrState == false){
Serial.print("press FSR!! ");
delay (1000);
lastButtonState = buttonState;
}
else if (buttonState == LOW && lastButtonState == HIGH && fsrState == true){
Serial.print("Ready!! ");
delay (1000);
lastButtonState = buttonState;
}
if (fsrState == 0 && lastfsrState >= 1 && blinking == false){ // check for a high to low transition
// if true then found a new button press while clock is not running - start the clock
startTime = millis(); // store the start time
blinking = true; // turn on blinking while timing
delay(5); // short delay to debounce switch
lastfsrState = fsrState; // store fsrState in lastfsrState, to compare next time
}
else if (fsrState == 0 && lastfsrState >= 1 && blinking == true){ // check for a high to low transition
// if true then found a new button press while clock is running - stop the clock and report
elapsedTime = millis() - startTime; // store elapsed time
blinking = false; // turn off blinking, all done timing
lastfsrState = fsrState; // store buttonState in lastButtonState, to compare next time
// routine to report elapsed time
Serial.print("Time: ");
Serial.print("\t");
Serial.println( (int)(elapsedTime)); // divide by 1000 to convert to seconds - then cast to an int to print
delay(1000);
t =((elapsedTime)/2.00); // print time divided by 2
Serial.print("t=");
Serial.println(t);
delay(5);
}
else{
lastfsrState = fsrState; // store buttonState in lastButtonState, to compare next time
}
// blink routine - blink the LED while timing
// check to see if it's time to blink the LED; that is, the difference
// between the current time and last time we blinked the LED is larger than
// the interval at which we want to blink the LED.
if ( (millis() - previousmillis > interval) ) {
if (blinking == true){
previousmillis = millis(); // remember the last time we blinked the LED
// if the LED is off turn it on and vice-versa.
if (value == LOW)
value = HIGH;
else
value = LOW;
digitalWrite(ledPin, value);
}
else{
digitalWrite(ledPin, LOW); // turn off LED when not blinking
}
}
}