how to count variable every 60 second and reset it?

Hi, i'm a beginner in arduino. i need your help about counting a variable of input sensor every 60 second and reset it. i don't really understand to use millis(). this is my code

int sensor = 2;
int hitobject = false;
int count = 0;
int start;

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

void loop() {
  // put your main code here, to run repeatedly:
  int val = digitalRead(sensor);
  
    if ((val==0) && (hitobject==false)) {
    count++;
    hitobject = true;
    Serial.println(count);
    }
    else if ((val==1)&&(hitobject==true)) {
      hitobject=false;
    }

}

I use e18-d80nk as the sensor. in my code it just count but i want the result of count show every 60 second and then reset and show the new result of count in 60 second. i really need your help. i want to make a simple tachometer. :slight_smile:

Have you read Using millis() for timing. A beginners guide at the top of the forum?

Maybe you could try this?

 unsigned long thisMillis = 0;
unsigned long lastMillis = 0;

void loop()
{
  int val=digitalRead(sensor);

  thisMillis = millis();
  
  if(thisMillis - lastMillis < 60)
  {
    if ((val==0) && (hitobject==false)) {
    count++;
    hitobject = true;
    Serial.println(count);
    }
    else if ((val==1)&&(hitobject==true)) {
      hitobject=false;
    }
 
  }
  else
  {
    lastMillis = thisMillis;
  }
}

Fail. Which branch is executed most often? :slight_smile:

I would do it in the following way.

1. Let the Counter-1 (of the TC1 Module of the ATmega328P of Arduino UNO) count the sensor pulses for 60-sec (60000 mS).

2. After each 6-sec time, read the counter; clear the counter; display the content of the counter on Serial/LCD/7seg Monitor.

3. 60-sec time interval would be created using the millis() function of the Arduino.

Setup
1. Let us connect the output of the Proximity Sensor with DPin-5 (Digital Pin 5) of UNO. DPin-5 is in turn connected with the input in (Pin-11 of MCU) of TC1.
2. Let us execute the following sketch. (Tested partially using loop back connection of DPin-13 with DPin-5.)

unsigned long lastMillis=0;
void setup() 
{
  Serial.begin(9600); //Serial Monitor buad rate
 // pinMode(13, OUTPUT);
  pinMode(5, INPUT);   //Sensor has pull-up.

  //--configure TC1 to count external pulses
  TCCR1A = 0x00;
  TCCR1B = 0x00;
  TCNT1 = 0x00;   //intial count 0
  TCCR1B = 0x07;  //start TC1 with external clock pulse Rising Edge
}

void loop() 
{
  while (millis() - lastMillis !=60000) //1000 ms - 1-sec 
    ;
  lastMillis = millis();    //shft current time
  Serial.println(TCNT1, DEC); //show value of 60-sec count on Serial Monitor
  TCNT1 = 0x0000;           //Reset Counter-1
   
  //digitalWrite(13, !digitalRead(13));

}

Really? All that trouble and you incorporate a back to back 60 second system freeze?

I tested for 1-sec time interval just to get 1, 0, 1, 0,...

I agree that it is not a satisfactory solution (blocking the system for 60-sec) at all; but, I have understood that the OP is just looking for a Counter to count his RPMs; so, I have just given a possible solution.

Thanks for the observation.

My turn? :stuck_out_tongue:

unsigned long timerStart,
              timerEnd = 60000; // 60 seconds            
bool sensor = 2,
     hitobject = false;
int count = 0;
int start;
void setup() {
  // put your setup code here, to run once:
  pinMode(sensor, INPUT);
  Serial.begin(9600);
}
void loop() {
  // put your main code here, to run repeatedly:
  int val = digitalRead(sensor);
  if ((val==0) && (hitobject==false)) {
    count++;
    hitobject = true;
    //Serial.println(count);
  }
   else if ((val==1)&&(hitobject==true)) {
      hitobject=false;
  }
   if(millis() - timerStart > timerEnd){
     Serial.println(count);
     count = 0;              // reset count
     timerStart += timerEnd; // reset 60 sec timer
  }
}