# PICAXE code help needed



## Spooky Dave (May 12, 2011)

Hi all,

I'm in pretty dire need of some help with my picaxe code. My eyes are going cross-ways from reading the manual, but I still can't seem to find the fix.

As a caveat, I'm still fairly new to picaxe. I've just barely begun delving into parallel tasking, which is necessary for my project. For now, I'm trying to get my two LED spotlights to flash at different intervals, then pause (for 5 seconds) before resetting.

I've found out--as you can see from the code below--how to make this work by placing a five second pause in each of the separate tasks. The problem is, no matter how much I try to sync up the waits, they get out of whack. Which means that the trigger will start one (which is done waiting) but not the other.

I'm trying to find a way to redirect both processes to a single wait. Is there a way to do this? To let each of them blink, simultaneously/parallel, and then come together in a single line of code to wait at the same time? Any help would be greatly appreciated. Code below, with the two "pause" commands in red text for easy finding.

Thanks a million if anybody can help me figure this out!

Dave

symbol counter1 = b1 
symbol counter2 = b2
symbol LED3 = b.6 
symbol LED6 = b.5
symbol trigger = pinc.7

start0:
if trigger = 0 then start0
for counter1 = 1 to 15	; start a for ... next loop
high LED3
pause 100
low LED3
pause 100
next counter1	; end of for ... next loop
pause 5000
goto start0

start1:
if trigger = 0 then start1
for counter2 = 1 to 30
high led6
pause 50
low led6
pause 50
next counter2
pause 5000
goto start1


----------



## fritz42_male (May 5, 2009)

Don't do it as 2 separate loops.

Without actually coding it (this is caused pseudo code) it would be something like this

Begineone:
Counter = 1


Begintwo
Counter = Counter + 1
If counter = 100 then gosub ledoneon
If counter = 500 then gosub ledtwoon
If counter = 300 then gosub ledoneoff
If counter =600 then gosub ledtwooff
If counter = 1000 then goto beginone # this sets the point at which the loop restarts
goto begintwo:

ledoneon:
Switch LED 1 on
return

ledoneoff
switch led 1 off
return

ledtwoon
switch led 2 on
return

ledtwooff
switch led 2 off
return



Then just change the size of the number in if counter = 1000. Remember to use a word variable for counter as you probably need the longer times. You use the pause at the end of the count and then go back to your trigger event


----------



## Spooky Dave (May 12, 2011)

Hi Fritz42,

Thanks so much for the help. I must admit a lot of that doesn't yet make sense (like I said, I'm still pretty new at PICAXE). I look forward to playing around with it tonight. One more quick question. From the pseudo-code above, does the second LED turn on after the first? 

Dave


----------



## fritz42_male (May 5, 2009)

Yes it does but that is easy to alter. All you want to do is create a timeline of X seconds long - that is what you are using the counter w0 for. At specific points in this timeline, you want LEDs to turn on and off. All you do is test to see if you have reached that point in the timeline and then switch on or off which ever LED you want.

Here is a working example for 1 LED 


Begin1:
w0=1


Begin2:
w0=w0 + 1
If w0 = 10 then gosub LED1ON
If w0 = 20 then gosub LED1OFF

If w0 = 1000 then goto Begin1
goto Begin2:


LED1ON:
High 1
return

LED1OFF:
Low 1
return






Why not program it up in the Program Editor and use the simulator to check it out? Just bear in mind that the simulator runs a lot slower than the actual Picaxe. After a few runs through, you will have the LEDs ticking off and on in no time!

What chip are you using and how many leds are you controlling?


----------



## coolbotz (Oct 21, 2009)

Which PICAXE are you using? Only the M2 parts have multiple STARTS. Go to PICAXE Forum for reading on multiple STARTS.


----------



## fritz42_male (May 5, 2009)

You don't need multiple starts anyway for a simple timeline like this.


----------



## robalex32 (Sep 6, 2010)

What are you talking about???


----------



## niblique71 (Dec 2, 2009)

There should be a dedicated microchip thread for the Picaxe and Arduino. I finnaly took the plunge and bought an 18m kit with that stupidly rediculously expensive programming cable. Now I just have to find the time to figure it out....... Probably winter when things get slow for me.. 

As for the relevence for the thread.... I enjoy looking at the codes people use and try to figure them out. Believe it or not they "Sorta" make sense.

I'm sure I'll be calling on all of you for help too at some point.


----------



## Spooky Dave (May 12, 2011)

Hey all,

Thanks so much for your advice. Alas, tonight got away from me and I wasn't able to play with any code. I'll definitely do so soon, however. Most likely tomorrow.

To answer questions, I'm using a picaxe 18-m2 to control two separate LED spotlights. One of the spotlights has a 3-LED bay, and the other has a 6-LED bay. These aren't powered directly from the chip, by the way, but through a ULN2803A. One pin is required per spotlight, not 3 or 6.

Again, I'm new to this but I thought that if I wanted to run multiple tasks at the same time, such as having an LED turn on at the same time as a motor, etc., that I had to use the parallel task function. 

That's what I'm after. I want two separate LED spotlights to come on--one to blink twice as fast as the other because it looks pretty awesome--and my pneumatic valve to fire at the exact same time as the LED lights. So, three output pins triggering three separate events/actions at a time. One for each of the LED spots and one for the valve, and then I want to be able to tell all three to pause for 20 seconds or so before resetting at the exact same time. 

I haven't been able to figure out how to synchronize that. I'm hoping very much that I'm overcomplicating things! But again, I haven't tried playing with any of the code ideas you guys have given me, so I'll definitely have to give it a go and let you know how things turn out.

Thanks again!


----------



## fritz42_male (May 5, 2009)

How many blinks are you looking for in the timeline?

If not too many then the method above is fine. If you are looking for 3 minutes of fast blinking then you would use the master loop to send blink start instructions to a parallel process.

Can you give me an idea of the timeline e.g:

On trigger I want one spotlight to blink 10 times, the other to blink 20 times over the same time and then both stop blinking and the valve fires. The valve is firing at 1 minute 45 seconds and everything stops and resets at 2 minutes 10 seconds.



It often helps just to draw a line on a piece of graph paper and show the events as they occur on the line.


----------



## Spooky Dave (May 12, 2011)

fritz42_male said:


> How many blinks are you looking for in the timeline?
> 
> If not too many then the method above is fine. If you are looking for 3 minutes of fast blinking then you would use the master loop to send blink start instructions to a parallel process.
> 
> ...


Hi Fritz42,

The timeline I'm shooting for is pretty simple (at least I hope it is). I want all three to turn on at once, run for about 10 seconds, and then turn off at the same time with a 20-second pause before resetting so it can't be triggered over and over again easily/accidentally by the same person.

The prop is a cemetery popup column. I will have a monster inside, and when the thing triggers, the top pops up to reveal the monster. Within the column I will have the two LED spotlights aiming in an upward direction at his face, one to the left and one to the right.

In my perfect world, when the prop is triggered, the valve will fire--as will the LED spots--all at once. LED 1 will be flashing 10 times per second, LED 2 will be flashing 5 times per second, but both for the same duration. In other words, LED 1 would flash 100 times, and LED 2 would flash 50 times. Then everything, LED's and valve, would turn off and wait 20 seconds before the trigger could be activated again.

I thought, previously, that in order to do these 3 things at the same time I had to use parallel tasking. Sounds like I'm making it harder than it needs to be. Thanks for all your valuable advice!

Dave


----------



## fritz42_male (May 5, 2009)

I'll have a play over the weekend. The 18M2 has 2 PWM out channels which you should be able to run the LEDs on.


----------

