Go Down

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

jmnijsse

Hi,
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

Gr.
Johan

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(){
  Serial.begin(9600);
}

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; 
  }
  Serial.print(sensorvalue);
  Serial.print(",");
  Serial.print(signal);
  Serial.print(",");
  Serial.println(signalCounter);
  delay(500);
}




HugoPT

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
Debian,Mint,Ubuntu
Arduino Mega 2560
Arduino Nano
Arduino Duemilanove
MAC OS Montain Lion
Raspberry PI Model B

jmnijsse

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?

Johan

wildbill

Quote
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?

PaulS

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.

jmnijsse

#5
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.

Thanks...

Johan

Go Up