Analogread state laststate

Dear friends,

I am new to programming. I am dealing with analog IR sensors and i need to count crosses of line following robot.

Could you please advise how to count crosses to 10. When the robot crosses the line with for loop function counts to 10. I d like it to count 1 next cross + 1. (Int cross = 1; cross < 11; cross++).

Thanks.

Mihaletto:
Dear friends,

I am new to programming. I am dealing with analog IR sensors and i need to count crosses of line following robot.

Could you please advise how to count crosses to 10. When the robot crosses the line with for loop function counts to 10. I d like it to count 1 next cross + 1. (Int cross = 1; cross < 11; cross++).

Thanks.

Here are the Rules My program will follow:

  • sensor reports a value when it sees a line:
  • any value above 500 is a line in view.
  • any value below 100 is NO line.
  • any value between these is unknown.
enum STATE { UNKNOWN, LINE, NOLINE};
#define sensePin A0

static STATE lastState=UNKNOWN;  // because I have decided that I must Not See a line before I
  // can start counting Lines.

static lineCount = 0;

void loop(){
uint16_t reading = analogRead(sensePin);
if(reading>500){ //line is present
  if(lastState == NOLINE){
    lineCount++; // found new line!, only count it once
    }
  lastState = LINE;
  }
else if(reading<100) { //no Line !
  lastState = NOLINE; // now if I see a line again I can count It.
  }

if(lineCount >= 10){ // yea! found 10 lines!
  Serial.println("found Ten Lines");
  lineCount =0 ; // start over!
  }
}

you will have to change this for your actual hardware.

Chuck.

Thank you very much!

I'll try to test it.