Helo counting the number of times a pin goes low or high per second?

The primary expression bit is fixed but now I am trying to count how many times a pin goes low or high per second and it's being a real pain. I need help with that, this stupid debounce script is useless.

Hello everyone I found this bit of code online that should work for a project I need (with some changes of course) but I get these problems when compiling it.

BareMinimum.ino: In function 'void loop()':
BareMinimum:38: error: expected primary-expression before '>' token

I did have to fix a couple brackets so I'm guessing whoever posted the code may have forgotten something else but I'm just not seeing it. It brings up this error due to the line near the bottom that is "if (val <> valold) {"

int ledPin = 13;                // LED connected to digital pin 13
int photoPin = 2;               //
int val = 0;
int valold = 0;
int start;
unsigned int count = 0;

void setup()                    // run once, when the sketch starts
{
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
  pinMode(photoPin, INPUT);  
  valold = digitalRead(photoPin);
  Serial.begin(9600);
}

void loop()                     // run over and over again
{
  start = millis(); // take the start time
  count = 0; // reset couter
  while (millis()-start < 5000)
  {
     // check for overflow of millis()
     if (start > millis()) {
       start = millis();
       count = 0;
     }
    val = digitalRead(photoPin);
    if (val==LOW)
    {
      digitalWrite(ledPin, LOW);
    }
    else
    {
      digitalWrite(ledPin, HIGH);
      
      if (val <> valold) {
        count ++;
        valold = val;
      }
    }
  }
  Serial.println(count,DEC);
}
      if (val <> valold) {

No matter how much you'd like to, you are NOT allowed to invent syntax.

PaulS:

      if (val <> valold) {

No matter how much you'd like to, you are NOT allowed to invent syntax.

He didn't invent it, he imported it from Basic :wink: I remember using that syntax on my old ZX Spectrum "back in the day"...

PaulS:

      if (val <> valold) {

No matter how much you'd like to, you are NOT allowed to invent syntax.

oh, shit lol, it's so obvious.

I wonder why the original guy did that lol.

Well, I'm off to sulk in shame. Thanks.

edit: hmm, having some trouble now, I have it hooked to a hall sensor that outputs high or low, however the counter isn't increasing :confused:

I think I see part of it.

 if (val > valold) {
        count ++;
        valold = val;

I have a feeling that is the part which is messing it up, and if I am going to be counting up to 51 times per second (or ideally 25 per half second for a better update speed) I think this is too slow or something along those lines. hrmmm

He didn't invent it, he imported it from Basic

Whatever. It's not valid C syntax.

OP, the <> operator in Basic means not equal. In C, that is !=.

Ok so I just took out the value part because that seems to be stopping it for some reason, however now the count goes up into the thousands, even if the magnet is by the hall effect sensor for a fraction of a second, I guess the value part was to fix that, I'll have to work that out so that it only increases the counter once, though I'm not sure if it will work when the magnet is going by at 50 times per second.

int ledPin = 13;                // LED connected to digital pin 13               //
int val = 0;
int valold = 0;
int start;
unsigned int count = 0;

void setup()                    // run once, when the sketch starts
{
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
  pinMode(2, INPUT);  
  valold = digitalRead(2);
  Serial.begin(9600);
}

void loop()                     // run over and over again
{
  start = millis(); // take the start time
  count = 0; // reset couter
  while (millis()-start < 1000)
  {
     // check for overflow of millis()
     if (start > millis()) {
       start = millis();
       count = 0;
     }
    val = digitalRead(2);
    if (val==HIGH)
    {
      digitalWrite(ledPin, LOW);
    }
    else
    {
      digitalWrite(ledPin, HIGH);
        count ++;
      }
    }
  Serial.println(count,DEC);
}

What I need is a way to make it so that no matter how long it is triggered for it will only output one quick signal so that it only increases the counter once, though I'm not sure how I can do this and it needs to work at the different speeds it will be reading the rpm at.

I think the debounce code might be the key.

Crap, it uses millis so I think they two parts will interfere. Maybe I can set up a secondary loop. Right now though I need to figure out how to make it just read a long press as one short one once the press ends instead of toggling the led. I don't know how I would do that though,

  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
  pinMode(2, INPUT);  
  valold = digitalRead(2);

Why do some pins have names and some have just numbers?

Crap, it uses millis so I think they two parts will interfere.

Why? You can get to work on time, and meet you mates for drinks after work, without having to wear two watches, can't you?

PaulS:

  pinMode(ledPin, OUTPUT);      // sets the digital pin as output

pinMode(2, INPUT); 
  valold = digitalRead(2);



Why do some pins have names and some have just numbers?



> Crap, it uses millis so I think they two parts will interfere.


Why? You can get to work on time, and meet you mates for drinks after work, without having to wear two watches, can't you?

Because, I'm just getting the random bits of code together, I'll switch it all to pin numbers or variables at the end.

And won't this bit mess with the millis value that the button press bit is working with?

{
  start = millis(); // take the start time
  count = 0; // reset couter
  while (millis()-start < 1000)
  {
     // check for overflow of millis()
     if (start > millis()) {
       start = millis();
       count = 0;

millis() changes when it wants to, and it wants to change every 1000 microseconds. Looking at it will not change it. When you look at it, try to remember what it looked like, for later use.

well it's an effing mess but I'm getting there, I can't figure out how to ignore the toggle thing so I will just have it multiply the count twice, then by 60 to get the RPM, it's not quite working right now the highest can get to by hand is 5 though it should be going faster, I have to work on it more.

const int buttonPin = 2;    // the number of the pushbutton pin
const int ledPin = 13;      // the number of the LED pin
int val = 0;
int valold = 0;
int start;
unsigned int count = 0;


// Variables will change:
int ledState = HIGH;         // the current state of the output pi n
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

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

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
  // set initial LED state
  digitalWrite(ledPin, ledState);
}

void loop() {

  start = millis(); // take the start time
  count = 0; // reset couter
  while (millis()-start < 1000)
  {

    // read the state of the switch into a local variable:
    int reading = digitalRead(buttonPin);

    if (reading != lastButtonState) {
      // reset the debouncing timer
      lastDebounceTime = millis();
    }

    if ((millis() - lastDebounceTime) > debounceDelay) {


      if (reading != buttonState) {
        buttonState = reading;

        // only toggle the LED if the new button state is HIGH
        if (buttonState == HIGH) {
          count++;
        }
      }
    }
  }


  digitalWrite(ledPin, ledState);
  Serial.println(count,DEC);
}

uhg i dont even know if this fking thing is working, it doesnt read properly most of the time once i got a figure of 17 times per second which was 1020 rpm or so, i put it on another level of the pulley and got 24 times a second or 1440, the target is 1390, but this stupid thing wont read for some damn reason, I set it to go every 5 seconds, and the serial output worked a bit but then it just scrolled a fuckload of zeroes and i couldn't read anything.

Why does it have to be so damn difficult to count how many times a pin goes high or low during a certain period of time? uhg.

Why does it have to be so damn difficult to count how many times a pin goes high or low during a certain period of time?

It isn't, if you do it right.

    if (reading != lastButtonState) {

Where do you set lastButtonState?

I told you that the debounce code was crap, but you used it AS-IS, anyway. That wasn't all that bright.

PaulS:

Why does it have to be so damn difficult to count how many times a pin goes high or low during a certain period of time?

It isn't, if you do it right.

    if (reading != lastButtonState) {

Where do you set lastButtonState?

I told you that the debounce code was crap, but you used it AS-IS, anyway. That wasn't all that bright.

Actually, you never said anything about the debounce code, guess it wasn't too bright of you not to check your previous posts.

What you're trying to do is simple enough, but the code you're using to do it is a train wreck. I suggest you chuck it and start again.

Use the State Change Detection example to maintain a count of how many pulses have occurred. Don't worry about overflow, or try to zero the counter. Just make sure it is held in an unsigned variable big enough to hold the number of pulses that you want to be able to count in one sample period.

Then use the Blink Without Delay example to make some code run at regular intervals. The code will use a variable which holds the value of the counter last time it ran. Each time it runs the code will subtract the previous counter value from the current counter value to work out the change, and then update the saved value for next time.

There are other ways to approach the problem. The reason I'm suggesting this approach is that it gives you a very simple solution regardless of whether the pulse detection is being done by polling, or by interrupts.