Help with cybot light avoid code

I have hooked my cybot up to a 20x2 and am in the process of coding it with a light avoid program but I am having trouble with a certain line of code.

Code:
if rval = lval-50 or rval = lval+50 then goforward
I want to compare two variables and if they are within 50 of each other then it should go forward. With the code above I get a syntax error. Looking in the manual I can't see any command that would do what I want. Does an one have any ideas?

While I'm on the subject of this code, you may as well tell me if I have my hardware interrupt code setup correctly.
 

Attachments

BeanieBots

Moderator
Unfortunately, PICAXE BASIC does not permit calculations within an "if" statement.
You will need to calculate the two values, hold them in variables and then test against those.
 
Last edited:

lanternfish

Senior Member
Hi

This is not very elegant but overcomes your syntax error problem

Code:
	b3 = lval - 50
	if rval = b3 then goforward
	b3 = lval + 50
	if rval = b3 then goforward
If lval = 49 or lval = 206 then you will get an overflow error i.e. if lval = 49 then b3 will equal 255. And if lval = 206 then b3 will = 0.

How will this affect your program?
 
Last edited:

hippy

Ex-Staff (retired)
Another technique is ....

w0 = lval - rval
If w0 < ???? Or w0 > ???? Then ...

When lval >= rval the result will be $0000-$00FF ( 0 to 255 ), when when lval < rval the result will be $FFFF-$FEFF ( -1 to -255 ). A word variable has to be used to determine the subtraction has created a negative which appears as a large 16-bit integer through underflow wrapround.
 

lanternfish

Senior Member
I am wondering why you need this 'deadband'. And you appear to have no stop routine.

If you do not need the deadband then your program can be like this:

Code:
main:

	readadc lldr, lval	;read ldr values into variables
	readadc rldr, rval
	
	if lval < rval then goright				
	if rval < lval then goleft
	
goforward:
	low motorrr		;turn off reverse pins to prevent shorting out the hbridge
	low motorlr
	high motorrf	;turn motors on forwards
	high motorlf
goto main

(rest of code)
 
That will work perfectly hippy, thank you.

@lanternfish - The ldrs give slightly different values and if I didn't have the deadband then I believe the movement would be very jerky. I may be wrong but that is why I have it.
 

hippy

Ex-Staff (retired)
A simple way to measure light to left or right is to wire the two LDR as a potential divider, read via READADC and see what the reading is - it will be around 128 when both have equal light on them, above when light to one side, lower when light to the other.
 

BeanieBots

Moderator
To expand on Hippy's suggestion, I find it helps to put a pot (of a few kohms) between the two LDRs with the wiper to the ADC input. This serves two purposes. One is to prevent a low(ish) impedance across the power rail when both are exposed to high light levels, the other offers a way of trimming the mid point value to exactly 128.

Also, don't forget, you can use the difference to produce a number which can be used to set different speeds. It does not need to be hard left of right.
 
The ldr section of the code is working in the simulator (haven't had time to hook the 20x2 all up yet), but the hint is not working as I hoped. It will trigger once but then wont trigger again. Is this some bug in the simulator or my code. I'm not exactly sure what I am supposed to have in the interrupt gosub to re initialize hint.
 

Attachments

hippy

Ex-Staff (retired)
You need the 'return' rather than 'goto main' at the end of interrupt.
You need an additional 'hInt2flag=0' to clear the interrupt.
 
Top