Let's Make Robots!

picaxe basic, how to program to do two things at once

I successfuly built the LMR robot and it works.  Now I want to add some flashing LEDS to flash while the robot is moving.  I wrote a program to make them flash and that works,  I just can't figure how to incorporate that. seems I need to do two things at once???  

Comment viewing options

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

Parallel tasks are just typed at any point in the program with the predefined start0:, start1:, start2:, start3: labels. The first task is the default 'task 0', you could use a start0: label if you wanted but that is implied at the start of the program anyway.
The compiler automatically works out where the startX labels are so that all tasks start at the same time when the program commences.

e.g.



main:
high b.0
pause 500
low b.0
pause 500
goto main

start1:
high b.1
pause 5000
low b.1
pause 5000
goto start1


start2:
high b.2
pause 1000
low b.2
pause 1000
goto start2

You can see "Parallel Task Processing" in picaxe manual 1 for more information -  http://www.picaxe.com/docs/picaxe_manual1.pdf

I have read some of that and I guess my question is whether the X1 chip I seem to have will support parallel tasks??  I supposed I should just try it and see??????

You can set up something like this:

(psuedocode, of course...)


 

' in your variable declarations...

symbol counter = some byte variable

symbol counter_max = your value, maybe 10 is a good start

symbol led_pin = the pin your LED is hooked up to

main:

inc counter ' add to the very beginning of your main routine

if counter = counter_max then

   toggle led_pin ' there are better ways to command this but TOGGLE is a useable command. It turns off to on and vice versa.

   counter = 0 ' resets the counter

endif

' the rest of you code follows...

' ...

' ...

goto main


It won't set up a 100% steady blinking pattern because it will flash the LED based off how many times the programmed has cycled through. Every time a cycle of the program completes it adds one to the COUNTER variable. When COUNTER reaches COUNTER_MAX it will reverse the state of the LED pin and reset the COUNTER value for the next cycle. You can set the flashing speed by varying the COUNTER_MAX value to what you like best.

yes, I kinda understand that, but as far as I can figure, it still won't run at the same time the robot is moving???  I could just make them flash a few times before it starts moving, that would add some interest.  Now I have them on, a red one and a green one, when it is moving and one lights up for a left turn and the other lights up for a right turn.  fun stuff for this beginner.  

It will flash while the robot is drving forward. The code I suggested will run alongside the rest of your code like a parallel task. Plug it into the simulator and you can watch how it works...

Symbol dangerlevel = 70 ' how far away should thing be, before we react?
symbol turn = 300 ' this sets how much should be turned
symbol servo_turn = 700 ' This sets for how long time we should wait for the servo to turn (depending on it´s speed) before we measure distance

' ************************************************** the variables added to flash the LED

symbol counter = b3 ' I chose b3 arbitrarily, use whatever variable you like
symbol counter_max = 10 ' change value to adjust rate of flashing
symbol led_pin = 1 ' change pin number to whatever output pin your LED is connected to

' ***************************************************

 

main: ' the main loop

' ***************************************************code added to flash the LED

inc counter ' adds one to the counter value
if counter = counter_max then
 toggle led_pin ' turns off to on and vice versa.
 counter = 0 ' resets the counter
endif

' *************************************************** beginning of the normal SHR code

readadc 0, b1 ' read how much distance ahead
if b1 < dangerlevel then
gosub nodanger ' if nothing ahead, drive forward
else
gosub whichway ' if obstacle ahead then decide which way is better
end if
goto main ' this ends the loop, the rest are only sub-routines


nodanger:' this should be your combination to make the robot drive forward, these you most likely need to adjust to fit the way you have wired your robots motors
high 5 : high 6 : low 4 : low 7
return


whichway:
gosub totalhalt ' first stop!

'Look one way:
gosub lturn ' look to one side
pause servo_turn ' wait for the servo to be finished turning
readadc 0, b1
gosub totalhalt


'Look the other way:
gosub rturn ' look to another side
pause servo_turn ' wait for the servo to be finished turning
readadc 0, b2
gosub totalhalt

' Decide which is the better way:
if b1<b2 then
gosub body_lturn
else
gosub body_rturn
end if
return

body_lturn:
high 6 : low 5 : low 7 : high 4 ' this should be your combination that turns the robot one way
pause turn : gosub totalhalt
return

body_rturn:
high 5 : low 6 : low 4 : high 7 ' this should be your combination that turns the robot the other way
pause turn : gosub totalhalt
return

rturn:
servo 0, 100 ' look to one side
return

lturn:
servo 0, 200 ' look to the other side
return

totalhalt:
low 4 : low 5 : low 6 : low 7 ' low on all 4 halts the robot!
Servo 0,150 ' face forward
wait 1 ' freeze all for one second
return

 

O.K.  I will try that,  thanks a lot.  it will be fun experimenting with it.  Bill