Let's Make Robots!

Help with Sharp IR code

Im kinda new to this programming stuff so this is probably a really easy question. How do i get my robot to react to a increase or decrease of 20 points from my sharp IR. Right now  my code looks like this (b1 is IR reading)

if b1 > 115 then
 gosub TURNRIGHT
 end if

 if b1 < 85 then
 gosub TURNRIGHT
 endif

 my sensor reads about 95 when its on my black table but with different light sources or materials it can jump up or down. Instead of using 85 and 115 in my code i want it to react whenever there is a change of a specified amount.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Perhaps use a reference variable. Get that stored on the surface the robot is operating on. Think maybe using another B variable can work, storing in B2 maybe. Then try 

if b1 > (b2 + 20) then
 gosub TURNRIGHT
 end if

 if b1 < (b2 - 10) then
 gosub TURNRIGHT
 endif

 Or even :

if b1 > (b2 + 20) OR b1 < (b2-10)  then
 gosub TURNRIGHT
 end if

Note that this may not be standard PICAxe Basic, so a reference to the manual regarding expessions and boolean logic might be needed. 

 

Thats one of those irritating things about picaxe basic: no calculations in conditions

In order to note the change in readings. Use a variable (e.g. b2) to remember the last reading. Set the var to the last reading before you take a new one. use b3 (or something) for the difference

getreadings:

b2=b1 

readadc 0,b1 

return

----- SOME OTHER CODE --- 

if  b2>b1 then

b3=b2-b1

else

b3=b1-b2

endif

if b3>20 then

gosub TURNRIGHT

endif

or something like that

That woked great, thanks!