Counting Blinks

Hello :),

I have two sensors, I need them measure in different times and then count how many times the output changed its state (from HIGH to LOW and form LOW to HIGH). The PROBLEM is COUNTING them.

If I let both work at the same time (SECTION 1) there is no problem, but in the other way (SECTION 2) it doesn’t count anything (even though I know it is oscillating).

THANKS!

int current1=12;
int current2=13;

int sensor1pin=6;
int sensor2pin=7;

int sensorValue1 =0;
int sensorValue2 =0;

int lastestate1=0;
int contador1=0;

int lastestate2=0;
int contador2=0;

unsigned long lastMillis=0;

void setup() {
Serial.begin(9600);
  pinMode(current1, OUTPUT);
  pinMode(current2, OUTPUT);
}

void loop() {
  
         ////SECTION 1////
    digitalWrite(current1, HIGH);
    digitalWrite(current2, HIGH);

         ////SECTION 2////
//  digitalWrite(current1, HIGH);   // turn the LED on (HIGH is the voltage level)
//    delay(1000);               // wait for a second
//  digitalWrite(current1, LOW);    // turn the LED off by making the voltage LOW
//    delay(1000); 
//  {digitalWrite(current2, HIGH);
//    delay(1000);
//  digitalWrite(current2, LOW);
//    delay(1000);}
  
unsigned long currentMillis = millis();

sensorValue1= digitalRead(sensor1pin);
sensorValue2= digitalRead(sensor2pin);


if  (sensorValue1!=lastestate1)
  {contador1=contador1+1;
  lastestate1=sensorValue1;
  }

if  (sensorValue2!=lastestate2)
  {contador2=contador2+1;
  lastestate2=sensorValue2;
  }

if ((currentMillis-lastMillis)>=2000){

  lastMillis=currentMillis;
  Serial.print(contador1);
  Serial.print(" ");  
  Serial.println(contador2);   
  contador1=0; 
  contador2=0;  
  }

}

If "the other way" involves the delay() calls, you can forget doing anything useful while the delay() happens.

Since you set the pins at known states, it hardly makes sense to then read the state of the pin. Just increment the counter when you set the pin's state.

Try 3 blocks

  if (currentMillis - lastMillis1 >= interval1) {...}

each with its own lastMillisX and intervalX variables or constants.
Let the first block toggle current1, the second one current2, and the third one (you wrote it already) logs the counts.

PaulS:
If "the other way" involves the delay() calls, you can forget doing anything useful while the delay() happens.

Since you set the pins at known states, it hardly makes sense to then read the state of the pin. Just increment the counter when you set the pin's state.

Thanks for answering!

So, is it possible to put a while/if between the pulses? I had been trying something like: "If (curren1==HIGH) {function}" But it hasn't work

Also, the idea it's to measure the frequency of pulses when I change resistances, that's why I supply current from a pin and read the out pulses from other pin.

Can't you use interrupts for this?

Connect an interrupt to the pins you want to count the output changes on, then count RISING and FALLING interrupts separately or just every change.

There are certain applications where the use of interrupts is appropriate. We’d need more information on what stlafab360 is trying to accomplish to say whether that’s the case here. What’s certain is that using interrupts as a band-aid to allow the continued use of delay or other unnecessarily blocking code is not appropriate. The solution for that is to learn to write non-blocking code, not to add unnecessary complexity.

The code as posted looks to me as proof-of-concept for counting rather than production code, so I kinda ignored the delay() statements instead focusing on the counting part, assuming the long delay() will be gone when whatever else the sketch should do is added.

Using interrupts should not be a cheap excuse for not understanding the Demonstration code for several things at a time, on the top of this forum.