How Can I Do This?

I want to do something were when you press a button, a counter goes up one, and then the button has to go back low again before it can be counted as high again... Basically so that I don't end up with counter equaling 100 because the button keeps reading high from one long press...

In simpler terms, the button has to go low before it can be counted high again.

EX:

NO: HIGH (3 seconds) counter = 500 (or somethign like that)

YES: HIGH (1 second) LOW (7 seconds) HIGH (2 seconds) counter = 2

Sounds like you want a toggle switch, or a state machine type of deal...

Well, That still wouldn't work... I mean in software... Even if I do a toggle switch, the counter could still count 500 for one on... I want it so that the button HAS to be let go before another press can be counted...

If you search the site for toggle, I am fairly certain you will find the answer you are looking for...

while(button == LOW){} //while button is low, wait...
count++; //add to counter if button is pressed
while(button == HIGH){} //wait until button is released

That's one of the simplest ways I can think to do it.

Mowcius

The simplest way is to use the button library that AlphaBeta developed. http://www.arduino.cc/playground/Code/Button

There are examples provided that show how to use the uniquePress method.

@mowcius

while(button == LOW){} //while button is low, wait...
count++; //add to counter if button is pressed
while(button == HIGH){} //wait until button is released

What's going to cause button to change from LOW to HIGH?

What's going to cause button to change from LOW to HIGH?

Umm, you know... When you press the button... ;)

maybe I meant:

while(digitalRead(button) == LOW){} //while button is low, wait...
count++; //add to counter if button is pressed
while(digitalRead(button) == HIGH){} //wait until button is released
//See if pushed and held
if(digitalRead(ButtonPin)) {
  delay(40);
  while(digitalRead(ButtonPin)) {

    //button is on...
    //set button flag to on
    button_flag = 1; 

 }
}

if(button_flag) {
  button_counter++;
  button_flag = 0;
}

This here will set a flag high when button is pushed.

It has debouncing, and the program won't leave the while loop until digitalRead(buttonpin) = 0.

The idea is that you increment your counter when you see the flag as 1, and then reset the flag to 0.

@Mowcius, I think that the second piece of code you posted would still be wrong... I want one press to equal one point in the counter. I think that in your code, the counter will add and add until the button goes high again...? Tatal noob here...

@Mowcius, I think that the second piece of code you posted would still be wrong... I want one press to equal one point in the counter. I think that in your code, the counter will add and add until the button goes high again...? Tatal noob here...

Nope, the second piece of code is right...

The: while(digitaRead(button) == HIGH){} means that while it is high, it loops within the empty brackets, not the previous line of code

Mowcius

Hey Jeremy1998-

The code is right. If you are having trouble testing it, it is probably because of bouncing in the switch.

This would solve it

while(digitalRead(button) == LOW){/*while button is low, wait*/}

[glow]delay(40);  //wait for bouncing to stop[/glow]

count++; //add to counter if button is pressed

while(digitalRead(button) == HIGH){/*wait until button is released*/}

:)

I've put together this sketch to debounce the button signal and increment the counter when the button goes from the "not-pressed" state to the "pressed" state.

const int btnPin = 5;

// 0 = not pressed, 1 = pressed
int prevBtnStatus = 0;
int currBtnStatus = 0;

// debounce parameters
int cntOn = 0;    // number "pressed" samples
int cntOff = 0;    // number "not-pressed" samples
const int requiredSamples = 3;    // if we sample the same value these many times, we declare the button state as stable
const unsigned int sampleDelay = 10;    // unit: ms; sample the button pin every these many milliseconds

// to calculate when it's time to sample the button pin
unsigned long prevMillis;

// the counter to increment
// when the button gets pressed
int counter = 0;


void setup() {
    Serial.begin(9600);
    pinMode(btnPin, INPUT);
    
    // initialization
    prevMillis = millis();
}


void loop() {
    // let's see if it's time to sample the button pin
    if (millis() - prevMillis >= sampleDelay) {
        prevMillis = millis();
        
        // count the number of on or off samples
        if (digitalRead(btnPin)) {    // !digitalRead() for active-low buttons
            cntOn++;    // one more sample where the button was pressed
            cntOff = 0;
        }
        else {
            cntOn = 0;
            cntOff++;    // one more sample where the button was not pressed
        }
        
        // calculate current button status
        
        if (cntOn >= requiredSamples) {
            currBtnStatus = 1;
            cntOn = 0;
            cntOff = 0;
        }
        
        if (cntOff >= requiredSamples) {
            currBtnStatus = 0;
            cntOn = 0;
            cntOff = 0;
        }
        
        // when button goes from not-pressed to pressed,
        // increment the counter and print its value on the
        // serial port
        if ((prevBtnStatus == 0) && (currBtnStatus == 1)) {
            counter++;
            Serial.print("counter=");
            Serial.println(counter);
        }
        
        // update the previous button status variable
        if (prevBtnStatus != currBtnStatus) {
            prevBtnStatus = currBtnStatus;
        }
    }
}

HTH