0

I want to reset this counter variable which I created after each 1 second, but I don't want to disturb the if statement.

Here is the code:

int button=A0;
int val;
int count=0;
int presss;


void setup() {
  pinMode(button,INPUT);
  Serial.begin(9600);
}


void loop() {
  val=analogRead(button);
  if(val>1000){
   presss=count++;
   delay(200);
 }
  Serial.println(presss);
  delay(1000);
 }
1

I would do it the following way:

  • Use a variable declared as: unsigned long time_passed;

  • In the setup() section I would add the line time_passed = millis();

  • In the loop() section I would add the if condition:

if(millis()-time_passed > 1000) { time_passed = millis(); presss = 0; }

I think this is still not working properly with your example, I recommend to make two changes in your code:

  • you have a delay of 1000 implemented, which means the cycle time of your loop is minimum 1000ms,that means you are never able to count more than 1, since your counter is resetted every loop

  • I cannot understand how presss = count++; should work. For proper counting I would suggest to do it this way: presss = presss + 1;

  • Note: max. value of your counter would be 5, since the delay in the if Statement will enable maximum 5 detections of your button within 1000ms

0

You could do something like this:

void loop() {
  for (int i = 0; i < 5; i++) {
    val = analogRead(button);
    if (val > 1000) count++;
    delay(200);
  }
  Serial.println(count);
  count = 0;   
 }

This will sample the button five times and count the number of times the value is larger than 1000, with a 200 ms delay between each read. This give approx. 1 s.

But this might not be the logic you are looking for.

Cheers!

0

If I understood the project, you want to count how many button clicks you did in one second, so I've adjusted your code a little bit. If you don't want your program to do something after a certain amount of time, but don't want it to wait don't use delay(ms) use millis() instead (see https://www.arduino.cc/reference/en/language/functions/time/millis/ )

int button=A0;
int val;
int presss=0;

int millisBegin = 0;
int millisNow;
int millisBorder = 1000;

void setup() 
{
  pinMode(button,INPUT);
  Serial.begin(9600);
}


void loop() 
{
  val=digitalRead(button);

  if(val==HIGH)
  {
    presss++;     //If the button is pressed it will count + 1

    while(val==HIGH)
    {
      val=digitalRead(button); //It will wait till you release the button here
    }
  }

  Serial.println(presss);

  millisNow = millis();

  if(millisNow-millisBegin >= millisBorder)
  {
    presss = 0;
    millisBegin = millisNow;
    Serial.println("~1 second passed");
  }
}

I checked it and it worked for me (I changed the if(val>1000) to if(val==HIGH), otherwise it might not always detect a button press

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.