Count amount of an on/off switch on a interval of time

Hi everyone

im new programming arduino and i need an advice with the program i made
if i need to tap a librery or something or move a variable int, cons float anything

the description its simple
i need to count the amount of pulses from a on/off switch on an interval of time
there are some conditions and changes of some variables to compare when time gets to what i want

attached the program to this forum to check it

thank you and have a nice day.

contador_de_frecuencia_especifica.ino (1.76 KB)

Code from Original Post so others don't have to download it

int  ipin=12;             // pin sel like IN
int  opin=13;             // pin sel like OUT
int  statopin=LOW;        // Initial state of OUTPIN(13)                        
long tiempo=0;            // variable time 
long intervalo=15000;     // initial interval of time
int pulso= 0;             // amount of initial pulse of switchsensor 
int pulso1=300;           // amount of initial pulse for comparation 


void setup() {
  // put your setup code here, to run once:
  pinMode(ipin, INPUT);     // IN for sensor of revolutions (wheel switch)
  pinMode(opin, OUTPUT);    // OUT  true fals of condition statments
}

void loop() {
  // put your main code here, to run repeatedly:


  // Read the amount of ON/OFF  sensor
  // When ipin sense 1, variable pulse increment on one 
  // Pulse is a variable like count++
  
  if(digitalRead(ipin)==1){
    pulso =pulso+1;
    delay (100);             // DELAY for no rebound
    }

tiempo=millis();            //Capture time in millis on the variable called tiempo (time)


  // When tiempo(time) gets the amount of intervalo  AND  pulso gets pulso1 
  // Then state of pin 13 or opin change to HIGH 
  // Increment the amount of intervalo 
  // Change the amount of pulsos to compare
  // Pulso returns to cero

  
if ((tiempo == intervalo) && (pulso >= pulso1)){
  statopin = HIGH;
  pinMode(opin,statopin);
  intervalo=intervalo+2000;
  pulso1=200;
  pulso=0;
  }

  // If the time  ends 
  // And variable pulse doents gets pulso1 to compare
  // Then the state change or stays on LOW 
  // increment the amount of intervalo 
  // Change the amount of pulsos to compare
  // Pulso returns to cero
  
if((tiempo== intervalo)&& (pulso<pulso1)){
   statopin = LOW;
   pinMode(opin,statopin);
   intervalo=intervalo+2000;
   pulso1=200;
   pulso=0;
  }

  

}

...R

Having trouble with your description.

Any variables used with millis() should be 'type' unsigned long.

.

I don't think you are using millis() properly. It counts continuously from 0 when the Arduino starts so it will only be 15000 once in 49 days.

You need to record the value of millis() at the start of your count period and then compare that value with the current value of millis() - something like this

if (millis() - startMillis >= intervalMillis) {
   // the time has elapsed

   startMillis += intervalMillis;   // if you want to start another interval

}

...R

thank u now i know this , that arduino doesnt stop time even if u turn off arduino and yes i had bad defined a variable, thing is that i found a library to elapse time if it works like the last coment that would elapse time automatically giving me a unsigned long value starting from 0 if i dont elapse time again it will continue incresing the value that was defined like elapsed

#include <elapsedMillis.h>

// Program to count pulses of an On/Off switch on an interval of time
// when time gets v' intervalo compare pulse with v' pulse1
// resolving the states of if's and changing pinmode to LOW or HIGH
//

int ipin=12; // pin sel like IN
int opin=11; // pin sel like OUT
int statopin=LOW; // Initial state of OUTPIN(13)
elapsedMillis tiempo; // variable time
unsigned long intervalo=15000; // initial interval of time
int pulso= 0; // amount of initial pulse of switchsensor
int amtpulso=300; // amount of initial pulse for comparation

void setup() {
// put your setup code here, to run once:
pinMode(ipin, INPUT); // IN for sensor of revolutions (wheel switch)
pinMode(opin, OUTPUT); // OUT true fals of condition statments
}

void loop() {
// put your main code here, to run repeatedly:

// tiempo=millis()+1500; //Capture time in millis on the variable called tiempo (time)

// Read the amount of ON/OFF sensor
// When ipin sense 1, variable pulse increment on one
// Pulse is a variable like count++

if(digitalRead(ipin)==1){
pulso =pulso+1;
delay (100); // DELAY for no rebound
}

// When tiempo(time) gets the amount of intervalo AND pulso gets amtpulso
// Then state of pin 13 or opin change to HIGH
// Increment the amount of intervalo
// Change the amount of pulsos to compare
// Pulso returns to cero

if ((tiempo == intervalo) && (pulso >= amtpulso)){
intervalo=tiempo+2000;
statopin = HIGH;
pinMode(opin,statopin);
intervalo=tiempo+2000;
amtpulso=200;
pulso=0;
}

// If the time ends
// And variable pulse doents gets amtpulso to compare
// Then the state change or stays on LOW
// increment the amount of intervalo
// Change the amount of pulsos to compare
// Pulso returns to cero

if((tiempo== intervalo)&& (pulso<amtpulso)){
intervalo=intervalo+2000;
statopin = LOW;
pinMode(opin,statopin);
intervalo=intervalo+2000;
amtpulso=200;
pulso=0;
}
}

Please modify your Reply #4 and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

Is the problem now solved?

...R