How to stop repeat? basic questiom

How do i get this to only say button pushed once when switch pin is low and only print the tim ewhen it retuns to low?

const int switchPin = 2; // the number of the input pin
long startTime; // store starting time here
long duration; // variable to store how long the timer has been running
float secduration; // variable to store the duration in seconds

void setup() {
// put your setup code here, to run once:
pinMode(switchPin, INPUT_PULLUP);
Serial.begin(9600);
}

void loop() {
if(digitalRead(switchPin) == LOW){
startTime = millis();
Serial.println("Button Pushed");
while(switchPin == LOW){
delay(1);
}
}
if(digitalRead(switchPin) != LOW){
duration = (millis() - startTime);
secduration = (float)duration/1000;
Serial.print("Button released after ");
Serial.print(secduration);
Serial.println(" seconds");
}
}

consider

const int switchPin = 2; // the number of the input pin

long startTime; // store starting time here
long duration; // variable to store how long the timer has been running
float secduration; // variable to store the duration in seconds

byte butLst;

void setup() {
    // put your setup code here, to run once:
    pinMode(switchPin, INPUT_PULLUP);
    butLst = digitalRead (switchPin);

    Serial.begin(9600);
}

void loop() {
    unsigned long msec = millis ();
    byte          but  = digitalRead(switchPin);

    if (butLst != but)  {
        if (LOW == but) {
            startTime = msec;
        }
        
        else {
            secduration = (msec - startTime) / 1000.;

            Serial.print("Button released after ");
            Serial.print(secduration);
            Serial.println(" seconds");
        }

        butLst = but;
        delay (10);     // debounce
    }
}

Take a look at the state change detection example and the state change for active low switches tutorials. They will allow you to detect when the switch becomes pressed (the transition) as opposed to when it is pressed (the level).

Read the forum guidelines. Post your test code in code tags. Use the IDE autoformat tool (ctrl-t or Tools, Auto Format) to indent the code for readability before posting code.

Try this way

const int switchPin = 2;      // the number of the input pin
long startTime;               // store starting time here
long duration;                // variable to store how long the timer has been running
float secduration;            // variable to store the duration in seconds
//--------------------------------------------------------
void setup() {
  pinMode(switchPin, INPUT_PULLUP);
  Serial.begin(9600);
}
//--------------------------------------------------------
void loop() {
  if (digitalRead(switchPin) == LOW)
  {
    Serial.println("Button Pushed");
          startTime = millis();
    while (digitalRead(switchPin) == LOW){}
    duration = (millis() - startTime);
    secduration = (float)duration / 1000;
    Serial.print("Button released after ");
    Serial.print(secduration);
    Serial.println(" seconds");
  }
}

Thank you so much! worked perfectly

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.