Sleepless and Troubled over Flasher cycling

joemahma

New Member
For you smarter kids in the class:
Am going KRAZY trying to figure out how to cycle between one set of LED flashes on and O8M, and then to change the flash pattern with a momentary switch to another flash pattern. Only need about four different patterns. Have seen this very thing done with a Cylon kit using an ATTiny 13a from Sparkfun. Killer is, the code for that little sucker is THREE pages long, and in a language other than BASIC.

Anyone who can give me the BASIC code to accomplish this will be the beneficiary of my eternal appreciation.

Thanks skads....
 

inglewoodpete

Senior Member
Sounds like a faily simple and straightforward project. A great learning exercise for a beginner PICAXEr, too.

The 08M has 1 pin that can only be an input, so use a pushbuttom on that one (with a pullup) to step through your various programs.

How long do you want each flash sequence? 8- and 16-transition sequences would be easiest because you have byte and word variables. Eg, using a delay of half a second between transistions, you could load a byte variable with %01010101 and get a LED to flash on and off four times in a four second cycle. A pattern like 10110101 would give a different sequence if clocked to an output pin.

Doesn't make sense yet? Then I suggest you start by reading manual 1 (Getting Started) then 2 (Command Language) and finally manual 3 (Interfacing with the real world). They make great bedtime reading and I think you'll soon get the idea.
 

SAborn

Senior Member
You did ask for an example of code so here is a simple one.

You will need a pull down resistor on pin3 and to add your flash routine into the code where it is indicated for each of the 4 flash routines.

It should give you a basis to build from.

Code:
symbol counter = b1


Main:

	If pinC.3 = 1 then 
	inc counter
	pause 50
	endif
	
	if counter = 4 then 
	counter =0
	endif
	
	if counter = 0 then gosub flash1
	if counter = 1 then gosub flash2
	if counter = 2 then gosub flash3
	if counter = 3 then gosub flash4
	
Flash1:

	'enter flash 1 routine here.
	
	return
	
Flash2:

	'enter flash 2 routine here.
	
	return
	
Flash3:

	'enter flash 3 routine here.
	
	return
	
	
Flash4:

	'enter flash 4 routine here.
	
	return
 

westaust55

Moderator
Welcome to the PICAXE forum.

A quick forum search found 11 threads where "cyclon" LED sequencing was the topic
There may be many other threads on LED flashing sequences.

To enable folks to best help you can I suggest that you:

1. Post a schematic diagram for you hardware. It is hard to write any specific code without knowing which pin is used for what - even though the 08M only has available 1 dedicated output and as IWP states 1 dedicated input plus 3 pins that can be either input or output.

2. Post the program code that you ahve developed to date. While it may not work it helps us understand what you are trying to achieve.

3. a simple plain English descripotion of what you are trying to achieve - maybe including the LED sequences for the 4 patterns you have in mind.
 
Last edited:

hippy

Ex-Staff (retired)
In most 'change from this sequence to that sequence' projects the problem is usually lack of responsiveness, not jumping to a new sequence until the previous has finished which may take some time, or button pushes being missed.

The traditional solution is usually two-fold -

1) Responding to button push no matter what the current sequence is doing - Using interrupts or polling in loops of short time to make up a longer time; eg, don't pause for 10 seconds, do 100 pauses of 100ms.

2) Immediately returning from a current sequence when a button push has been detected and go to the next sequence. That often means a lot of "If buttonPushedFlag=1 Then Return" type commands which makes the code complicated and large.

In a single task program it's actually a lot more work than it first appears but the M2's multi-tasking helps there; one task runs the sequences, another looks for button pushes and stops and restarts the sequencing task.

I'd choose an M2 for this project and here's a simple example. Two LED's on Output Pins 1 and 2, push button on Input Pin 3. Press button to start sequencing and to change to the next ...

Code:
#Picaxe 08M2

Symbol sequenceNumber = b0

Start0:
  Suspend 1
  Do
    If pin3 = 1 Then
      Suspend 1
      sequenceNumber = sequenceNumber + 1 // 2
      Restart 1
      Do
        Pause 100 
      Loop Until pin3 = 0
    End If
    Pause 100
  Loop

Start1:
  Do
    Select Case sequenceNumber

      Case 0 : Low  1 : High 2 : Pause 500
               Low  1 : Low  2 : Pause 500
               High 1 : Low  2 : Pause 500
               Low  1 : Low  2 : Pause 500

      Case 1 : Low  1 : High 2 : Pause 250
               High 1 : Low  2 : Pause 250
               High 1 : High 2 : Pause 250

    End Select
  Loop
 

hippy

Ex-Staff (retired)
The above code but written for single task use, using interrupts ...

Code:
#Picaxe 08M2

Symbol sequenceNumber = b0
Symbol buttonPushFlag = b1

PowerOnReset:
  Gosub Interrupt_Enable
  Do
    If buttonPushFlag = 1 Then
      buttonPushFlag = 0
      sequenceNumber = sequenceNumber + 1 // 2
      Gosub RunSequence
    End If
  Loop

RunSequence:
  Do
    Select Case sequenceNumber

      Case 0 : Low  1 : High 2 : Pause 500 : If buttonPushFlag = 1 Then : Return : End If
               Low  1 : Low  2 : Pause 500 : If buttonPushFlag = 1 Then : Return : End If
               High 1 : Low  2 : Pause 500 : If buttonPushFlag = 1 Then : Return : End If
               Low  1 : Low  2 : Pause 500 : If buttonPushFlag = 1 Then : Return : End If

      Case 1 : Low  1 : High 2 : Pause 250 : If buttonPushFlag = 1 Then : Return : End If
               High 1 : Low  2 : Pause 250 : If buttonPushFlag = 1 Then : Return : End If
               High 1 : High 2 : Pause 250 : If buttonPushFlag = 1 Then : Return : End If

    End Select
  Loop

Interrupt:
  buttonPushFlag = 1
  Do
    Pause 100 
  Loop Until pin3 = 0

Interrupt_Enable:
  SetInt %01000, %01000
  Return
And using polling ...

Code:
#Picaxe 08M2

Symbol pauseTime      = w0
Symbol sequenceNumber = b2
Symbol buttonPushFlag = b3

PowerOnReset:
  Do
    pauseTime = 50 : Gosub DoPause
    If buttonPushFlag = 1 Then
      buttonPushFlag = 0
      sequenceNumber = sequenceNumber + 1 // 2
      Gosub RunSequence
    End If
  Loop

RunSequence:
  Do
    Select Case sequenceNumber

      Case 0 : Low  1 : High 2 : pauseTime = 500 : Gosub DoPause : If buttonPushFlag = 1 Then : Return : End If
               Low  1 : Low  2 : pauseTime = 500 : Gosub DoPause : If buttonPushFlag = 1 Then : Return : End If
               High 1 : Low  2 : pauseTime = 500 : Gosub DoPause : If buttonPushFlag = 1 Then : Return : End If
               Low  1 : Low  2 : pauseTime = 500 : Gosub DoPause : If buttonPushFlag = 1 Then : Return : End If

      Case 1 : Low  1 : High 2 : pauseTime = 250 : Gosub DoPause : If buttonPushFlag = 1 Then : Return : End If
               High 1 : Low  2 : pauseTime = 250 : Gosub DoPause : If buttonPushFlag = 1 Then : Return : End If
               High 1 : High 2 : pauseTime = 250 : Gosub DoPause : If buttonPushFlag = 1 Then : Return : End If

    End Select
  Loop

DoPause:
  Do While pauseTime >= 50 And buttonPushFlag = 0
    Pause 50
    pauseTime = pauseTime - 50
    If pin3 = 1 Then
      buttonPushFlag = 1
    End If
  Loop
  Return
 
Last edited:

John West

Senior Member
Some very good responses here, info I find useful as well. Let's hope Joe checks back in after the holiday weekend here in the US, so he too can obtain the benefit of these replies.
 

joemahma

New Member
Grateful!

Joe has, indeed, logged back in to tell you how grateful I am for EVERYONE'S help. Your kind gestures and the professional level of your counsel have exceeded anything I could have hoped for. In a world where you can't even get a cashier to say "thank-you", you may understand my appreciation of your good will. I feel like I should thank each one of you individually, and I will as soon as time allows. For now, your help has me chomping at the bit to work on my project. As Inglewoodpete suggested, I'll be hitting the manual thoroughly this weekend and be trying to figure out some of the directions you guys have given me.

One other thing...
It was suggested that I should attach the code I'm working on, schematics, hardware, project goals, etc. to my queries. I'm just wondering how much of those things I can attach without A. Hogging server space, and B. Not becoming a pest. I'll read the Stickies again in the hope of answering these concerns myself, but in the interim please step on me if I abuse your good nature. And thanks again--what an unusually helpful group you are!!!
 

John West

Senior Member
I can only speak for myself (and make a few assumptions about the rest,) but I expect there is plenty of room on the Rev. Ed. server for your posts. If there isn't, we'll take up a collection and buy them a new $89 Terrabyte HD to hook in. That should last awhile. :)

Besides, there's a "completed projects" section of the bd that takes up server storage space with pictures and interesting comments, and I for one wish more folks would add to it and use up a few more bytes of server space. That's where I get ideas, learn new techniques and steal code from.

As far as providing too much info, that's not likely to be a problem. As long as it's thoughtfully organized and easy to read or view, that's precisely what we need to help solve problems. Problems that resist our efforts to solve for any length of time are often due to a failure on the part of the poster to provide sufficient info.

(Be sure to use the code quote/unquote function for displaying your code and it won't take up a lot of screen space no matter how long it is.)

And finally, a few of us in the forum are retired and don't have anything better to do with our time than help others solve their problems and look over their shoulder at all their hard work. It's a lot easier than working on our own projects.

As long as you aren't just asking us to design all your circuits and write all your code, (as some students try to get us to do,) you'll find this is truly an excellent help forum for PICAXE projects, and all sorts of related input and output devices, as well as a great place to find answers to basic technical questions about power supplies and test equipment and such, things that affect the success of your PICAXE projects. In fact, it's the most expert and helpful free tech forum I've found on the web so far, and I don't expect to run across a better one any time soon.

All the more reason to use a PICAXE in your projects, as far as I'm concerned. In fact, possibly the best reason. Read a few of the other forum threads and you'll see what I mean.
 
Last edited:

hippy

Ex-Staff (retired)
I'm just wondering how much of those things I can attach without A. Hogging server space, and B. Not becoming a pest.
Very much as John West suggests; if it helps us to help you, or what you post helps others, or it's useful information to have, then go right ahead.

A 100 MB photo of a PICAXE and a LED which takes half a day to download or needs a 50" screen to view it is likely overkill but, on the other hand, don't try to shrink things so much they are unreadable. Use sensible judgement and you should be okay.

No need to thank everyone individual; just let us know how you get on with your PICAXE projects and watching you succeed will usually be reward enough.
 

nick12ab

Senior Member
One other thing...
It was suggested that I should attach the code I'm working on, schematics, hardware, project goals, etc. to my queries. I'm just wondering how much of those things I can attach without A. Hogging server space, and B. Not becoming a pest. I'll read the Stickies again in the hope of answering these concerns myself, but in the interim please step on me if I abuse your good nature. And thanks again--what an unusually helpful group you are!!!
My suggestion would be the original resoloution for schematics otherwise you lose quality but for photos, 640x480 (0.3 megapixels) to 800x600 (0.5 megapixels) is sufficient.
 
Top