Hi guys, this is my first time posting on this forum so please forgive me for any mistakes I make. I will do my best.
The main goal of this program is to use a (OPB125A -- reflective object sensor) as a wheel encoder. The wheel was made using a 3D printer and has spokes that allow the reflective sensor to receive a HIGH or LOW input value. I want to take the readings from this and compare them to the previous readings that it made on the previous loop of the code and determine if there was a change in state from a HIGH to a LOW. And if there is a change in states (from HIGH to LOW) I want it to add 1 to a counter. I have tested the reflective sensor by itself and it works properly when I spin the wheel with my hand. However the problem I am having seems to be the XOR statement in my code. It doesn't matter if the last state equals the new state (HIGH or LOW), it still adds 1 to my counter anyway.
Any help is greatly appreciated. Thanks.
int EncoderL = 50;
int stateL = 0;
int newstateL = 0;
int laststateL = 0;
int CstateL = 0;
int counterL = 0;
void setup() {
pinMode(EncoderL, INPUT);
Serial.begin(9600);
}
void loop() {
stateL = digitalRead(EncoderL);
newstateL = stateL;
CstateL = newstateL ^ laststateL;
laststateL = newstateL;
if (CstateL = 1) {
counterL++;
}
Serial.println(counterL);
Serial.print("newstate = ");
Serial.println(newstateL);
Serial.print("laststate = ");
Serial.println(laststateL);
Serial.print(" Cstate = ");
Serial.println(CstateL);
delay(500);
}