Let's Make Robots!

Minimal Picaxe LED blinker

I just found a different way to make an LED blink with a picaxe 28x1:

main:
   inc b0
   let pin7 = b0 >> 7
   ' do other important stuff here
goto main

The variable in b0 is incremented in every new loop. It will roll over from 255 to 0 and start over again. The output pin (pin7 in this case) takes the value (0 or 1) from the most significant bit in b0. Only when b0 is 128 or higher, will the pin go high (and the Diode will Emit Light). When b0 rolls over, back to values below 128, the LED will no longer receive power. So in this example, the LED will be on 50% of the time and off 50% of the time. It would take the code a total of 256 loops around the code to complete one LED blink cycle. The user sees the LED light up only once per blink cycle.

The interesting part is that there is no pause in the code. This code could be extended to do anything in "real time" and keep the LED blinking. The speed of the blink would be slowed down by the extra code, because it would take the code longer to loop around.

The value "7" can be any value in the range 0-7, where 0 will give you the fastest blinking (128 blinks per roll over of b0).

On a Picaxe28x1, the slowest setting 7, without any extra code, results in approximately 120 blinks per minute or 2 Hz. Apparantly, the Picaxe manages to roll over the byte twice per second. That translates into roughly 512 gotos per second.

A little piezo speaker can make the faster blink rates audible (which is all the rage according to our webmaster). Value 0 produces a tone of approximately 256 Hz, I guess.

Comment viewing options

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

This is a more elaborate example of the same principle.

#picaxe28x1
#no_data
#no_table
' make the PWM'ed LED go bright and dark, like HAL9000's eye
' this code will oscillate approx. 26 times per minute
' 65 bytes

symbol counter = b0
symbol duty = w8

duty = 500
pwmout 1, 200, duty

main:
  if counter >= 128 then
    duty = duty + 3 max 1000
  else
    duty = duty - 4 min 6
  endif
  pwmduty 1, duty
  inc counter
goto main