Go Down

Topic: counter don't count (Read 850 times) previous topic - next topic


I try to count a HIGH / LOW state of a signal.
The parts of the code (analog signal to digital signal) works.
Count-part in another sketch (standard arduinosketch ButtonStateChange) works
But in the total sketch the count-part don't work.

Anyone an idea? (of course... i'm still a very newbee...)
Below the code


Code: [Select]

boolean signal;
int sensorvalue;
int signalCounter = 0;   // counter for the number of button presses
int signalState = 0;         // current state of the button
int lastsignalState = 0;     // previous state of the button

void setup(){

void loop(){

  sensorvalue = analogRead(A0);
  signalState = digitalRead(signal);

  if (sensorvalue == 1006){
    signal = HIGH;

  if (sensorvalue == 0){
    signal = LOW;
  if (signalState != lastsignalState) {
    if (signalState == HIGH) {
      signalCounter ++;

    lastsignalState = signalState; 


Code: [Select]
signalState = digitalRead(signal);
digitalRead is used to read a digital state of a pin but you are passing signal boolean variable!!
I believe your problem is here
Arduino Mega 2560
Arduino Nano
Arduino Duemilanove
MAC OS Montain Lion
Raspberry PI Model B


Yes, I've thought of this thing too.
I think: the variable signal can be HIGH or LOW. So I named it a boolean. But I understand boolean is a function who give feedback a 0 or 1.

But when I changed to

int signal = 0;

counting even don't working...

Or is the problem that digitalRead only can read out a pin? And not a variable?

How can I readout the HIGH- or LOW state of an variable?



Or is the problem that digitalRead only can read out a pin?

That's what digitalRead does - read the state of a pin. You're telling the function to read from the pin number that Signal holds, which will be 0 or 1. That's bad news because you are using the hardware Serial port, which also uses those pins. Which pin is your button attached to?


Code: [Select]
  if (sensorvalue == 1006){
    signal = HIGH;

  if (sensorvalue == 0){
    signal = LOW;

Exact matches involving analogRead() values are not a good idea.

Changing the value of signal, when signal appears to be a pin number (that's what the input to digitalRead() is) doesn't make sense.
The art of getting good answers lies in asking good questions.


Sep 18, 2012, 02:17 pm Last Edit: Sep 18, 2012, 02:18 pm by jmnijsse Reason: 1
Thanks for answers.
That was what I need: a changing-mind-hint...(I believe it isn't good english. :smiley-red:)

The sensor (line-tracer-sensor DFRobot)gives a stable analog value at 0 and 1006. So I used it on an analog port.
But now I put it in an digital-port and Arduino recognized it as HIGH and LOW.

It works fine now.



Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131