I'm trying to get curDist to only increment once when WHITE is read and if the value doesn't change from WHITE not to increment..My problem is when I hold a White colored object over the sensor it continually increments instead of incrementing once and checking if something changed.
uint8_t readENC(uint16_t enc)
{
uint8_t sensValue;
if(enc == ENC_1)
sensValue=readADC(8);
else
return 0;
if(sensValue<50)
return WHITE;
else if(sensValue>200)
return BLACK;
else
return 0;
}
void procENC(uint16_t enc, float dist, uint8_t speed)
{
uint8_t curState=0; // current state of the sensor (black or white)
float curDist=0.0;
uint8_t lastState=0;
char distBuf[16];
if(readENC(enc)==WHITE)
lastState=WHITE;
else if(readENC(enc)==BLACK)
lastState=BLACK;
while(curDist<dist)
{
//robot_drive(Forward,speed);
if(readENC(enc)==WHITE)
{
curState=WHITE;
if (curState==lastState)
lastState=curState;
}
else if(readENC(enc)==BLACK)
{
curState=BLACK;
if (lastState==curState)
lastState=curState;
}
else
readENC(enc);
if(lastState!=curState)
curDist += 0.5;
sprintf(distBuf, "CurDist: %f", curDist);
lcd_puts(distBuf);
}
Software/Hardware used:
ASKED:
September 29, 2012 7:33 PM
I don’t quite grasp what’s being done here:
If the first “read” returns BLACK, you “read” again. And if the second “read” then returns WHITE, you drop into the WHILE-loop without setting lastState to any value.
Tom
The formatting is irritating. Sorry. — Tom
Starting with if(readENC(enc)==WHITE)lastState=WHITE;else if(readENC(enc)==BLACK)lastState=BLACK;Couldn’t that be stated as: lastState=readENC(enc);
And the loop might look something like this???while(curDist<dist)
{
//robot_drive(Forward,speed);
curState=readEND(enc);
if(lastState!=curState)
curDist += 0.5;
lastState=curState;
sprintf(distBuf, “CurDist: %f”, curDist);
lcd_puts(distBuf);
}
Formating is terrible and now it won’t let me put in another code block.. oh well .. what I forgot was to block up the final if statement .. although it doesn’t particularly matter if the statement lastState=curState is done only when they are different or all the time.. if(lastState!=curState) {curDist += 0.5; lastState=curState;}
Couldn’t that be stated as: lastState=readENC(enc);
That’s the basic point I was thinking, but it depends on whether two “reads” are desired or not. That’s why I asked for some clarification.
Also, note that a very similar structure is seen within the WHILE-loop.
Tom
@Phil, I see your later comment now, so you already saw the second double “read”. It’d be nice to know what the code is supposed to do. It’s hard to figure out what’s wrong when you don’t know the purpose. — Tom
if(sensValue<50)
return WHITE;
else if(sensValue>200)
return BLACK;
else
return 0;
So what’s between 50 and 200, GRAY???
What should the program do when zero is returned?