Please can someone help me

55 pts.
Tags:
C
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);
} 

Answer Wiki

Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Discuss This Question: 8  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • TomLiotta
    I don't quite grasp what's being done here:  
    if(readENC(enc)==WHITE)lastState=WHITE;else if(readENC(enc)==BLACK)lastState=BLACK;
      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
    125,585 pointsBadges:
    report
  • TomLiotta
    The formatting is irritating. Sorry. -- Tom
    125,585 pointsBadges:
    report
  • philpl1jb
    Starting with if(readENC(enc)==WHITE)lastState=WHITE;else if(readENC(enc)==BLACK)lastState=BLACK;Couldn't that be stated as:  lastState=readENC(enc);
    49,590 pointsBadges:
    report
  • philpl1jb
    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); }
    49,590 pointsBadges:
    report
  • philpl1jb
    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;}
    49,590 pointsBadges:
    report
  • TomLiotta
    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
    125,585 pointsBadges:
    report
  • TomLiotta
    @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
    125,585 pointsBadges:
    report
  • philpl1jb
    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?
    49,590 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following