Help with counting

I am working on a school project where we are trying to count the number of times a button is pushed and store that value for later use...

ex. when it gets to 20, we want to make it react...

we have tried a variety of things, but i apparently dont know enough about the code to make this work...

Please Help

Why don't you go ahead and post what you've got so far?

int pingPin = 7;
int count = 0;
int val = 0;
int x = 1;
int y = 0;
int store;
int laststore;

void setup()
{
Serial.begin(9600);
}

void loop()
{
long duration, inches;

// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// We give a short LOW pulse beforehand to ensure a clean HIGH pulse.
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);

// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);

// convert the time into a distance
inches = microsecondsToInches(duration);
val = inches;

//Serial.print(inches);
//Serial.print("in, ");
Serial.println();
Serial.print(count);

//count++;

count = store;
store = x;
x = 1;

if (val <= 2){

store = ++x;
}
else{
store = laststore;
}

delay(100);
}

long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}

This code is for a ping sensor, which we will actually be using, but I figured that the button code might be a little easier or less complicated to understand...

 digitalWrite(pingPin, LOW);
 delayMicroseconds(2);
 digitalWrite(pingPin, HIGH);
 delayMicroseconds(5);
 digitalWrite(pingPin, LOW);

 pinMode(pingPin, INPUT);
 duration = pulseIn(pingPin, HIGH);

This runs sequentially...it doesn't start waiting for a pulse until it gets to that point in the code.

im sorry but i am not quite sure what you are saying with this... I really am a novice at this,

what I really want this thing to do is incrementally count and record a specific action... i guess that is why i was asking about the button... i dont really want it to just count...

ex. if someone were to walk by the sensor, i want it to count 1... if another person walks by the sensor, i want it to count 2... and so on...

thank you so much for your help! :slight_smile:

K i don't really get what is going on with your code, but here is some code to count the number of times a button is pressed:

int inPin = 7;         // the number of the input pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin
int count=0; //number of times button was pressed

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup()
{
  pinMode(inPin, INPUT);
}

void loop()
{
  reading = digitalRead(inPin);

  // if we just pressed the button (i.e. the input went from LOW to HIGH),
  // and we've waited long enough since the last press to ignore any noise...  
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    //increment count
    count++;

    // ... and remember when the last button press was
    time = millis();    
  }
  previous = reading;

  if (count==20)
  {
    //whatever you want to do over here
    count=0; //reset count
  }

}

I just modified the debounce code in the learning section of the main site