Code for altering sequence time?

Sodapep

Member
Hello,

I'm working on a 8 segment sequencer with the 20M2. I can currently figure out how to cause a string of 8 outputs to go high and low in succession with only 1 pin being high at all times. I use the wait command between pins then restart the code.

That's pretty archaic. I was wondering if there was a way to define the speed at which the cycle runs using a potentiometer input or a tap tempo input. If you could point me in the right code direction I'd love to read up about it.

It would be great to have to gliss between the outputs, but I'm interested in getting the speed under control first. Thank you for any advice.

I'm thinking that a pot could be connected to the analog in and the voltage read to a variable, which would then be used to set the pause time.
 
Last edited:

Rick100

Senior Member
I was wondering if there was a way to define the speed at which the cycle runs using a potentiometer input or a tap tempo input. If you could point me in the right code direction I'd love to read up about it.

It would be great to have to gliss between the outputs, but I'm interested in getting the speed under control first. Thank you for any advice.

I'm thinking that a pot could be connected to the analog in and the voltage read to a variable, which would then be used to set the pause time.
Hello Sodapep,
Reading a pot with the adc will work. Manual 3 will show you how to wire it. There are many ways to get the sequence you want. Here's some sample code to try in the simulator.

Code:
#picaxe 20M2
dirsb = %11111111 'set all of portb to outputs 
main:
b0 = 1  'initial value with 1 pin high
for b1 = 0 to 7
	outpinsb = b0   'put value of b0 on portb pins
	readadc10 c.7,w2  'read the pot on pin c.7
	pause w2   'pause length of w2 in milliseconds
	b0 = b0 * 2  'shift b0 1 bit to left
next
goto main
Good luck,
Rick
 

1968neil

Senior Member
not quite sure what your trying to achieve but i'm guessing a knight rider scanner with variable speed ?
Try this, does just that :) Neil

Code:
#picaxe18m2


let dirsB = %11111111 ; 1 is output. 0 is input

main:

readadc C.1, b0 ' Read POT into var b0

for b1 = 0 to 7 step 1  ' turn ON or OFF the LEDs by using 'lookup' to set var b2
	lookup b1, ( %00000001, %00000010, %00000100 ,%00001000, %00010000, %00100000, %01000000, %10000000), b2
	let pinsb = b2
	pause b0
next b1


for b1 = 7 to 0 step -1 ' turn ON or OFF the LEDs by using 'lookup' to set var b2
	lookup b1, ( %00000001, %00000010, %00000100 ,%00001000, %00010000, %00100000, %01000000, %10000000), b2
	let pinsb = b2
	pause b0
next b1

goto main
 

Rick100

Senior Member
Here's a third method that is very similar to 1968neil's lookup example. It uses the eeprom and read commands to store and retrieve the bit pattern.
Code:
#picaxe 20M2
dirsb = %11111111 'set all of portb to outputs 
main:
b0 = 1  'initial value with 1 pin high
for b1 = 0 to 7
	read b1,b0	'read data
	outpinsb = b0   'put value of b0 on portb pins
	readadc10 c.7,w2  'read the pot on pin c.7
	pause w2   'pause length of w2 in milliseconds
next
goto main
eeprom 0,(1,2,4,8,16,32,64,128)
Good luck,
Rick
 

Sodapep

Member
Wow, thanks Rick and Neil! Three codes to learn. I really apprecaite your help.

Neil - the effect is similar to the knight rider you had mentioned (although I had to look up what that meant!). I had been working with a 4017 decade counter, but I wanted to gain more control and see if I can learn some more stuff about the PICAXE. The counter is driving transistor switches, which do contain LED's (for the knight rider look!) but also control some other stuff.

I will work with the codes you have both posted and post back when I inevitably hit a road block. Thank you!
 

Sodapep

Member
I simulated the first two codes as eeprom is a bit beyond me at the moment.

Rick's code does cylce through the outputs and starts back again, but I can't seem to get the values read on the input to alter the wait time. I tried messing with it to no avail.

I tried Neil's code and got the speed input to work. I have separated it out into sub routines, one as Neil had written with the tennis game type action and the other two operating in one or the other directions. The are all basically the same thing but with different steps. I also put the readadc10 command within each step sequence so the speed can be changed at any time rather than at the beginning of each loop.

So the basics are running! Thank you for your help.

The next step is to get the program to identify which way the user wants to outputs to go (knight rider, to the left, or to the right). I'll set up a three pole switch, connecting input pin C.2 to GND, +V or V*0.5, like three voltage windows.

Code:
let dirsB = %11111111

direction:
readadc10 C.2,b3
if b3 <= 100 then R
if b3 > 100 and b3 < 175 then KR
if b3 >= 175 then L

R:
for b1 = 0 to 7 step 1
	readadc C.1,b0
	lookup b1, (%00000001,%00000010,%00000100,%00001000,%00010000,%00100000,%01000000,%10000000), b2
	let pinsb = b2
	pause b0
next b1
goto direction

KR:
for b1 = 0 to 7 step 1
	readadc C.1,b0
	lookup b1, (%00000001,%00000010,%00000100,%00001000,%00010000,%00100000,%01000000,%10000000), b2
	let pinsb = b2
	pause b0
next b1

for b1 = 7 to 0 step -1
	readadc C.1,b0
	lookup b1, (%00000001,%00000010,%00000100,%00001000,%00010000,%00100000,%01000000,%10000000), b2
	let pinsb = b2
	pause b0
next b1
goto direction

L:
for b1 = 7 to 0 step -1
	readadc C.1,b0
	lookup b1, (%00000001,%00000010,%00000100,%00001000,%00010000,%00100000,%01000000,%10000000), b2
	let pinsb = b2
	pause b0
next b1
goto direction
However, I can't seem to get it to react to whatever value I put in on C.2, similar to the problem I had with Rick's code and the speed control
 

Rick100

Senior Member
That explains it! Thank you!!
You welcome. Regarding the eeprom command. It's just another way of storing data. Store the byte values using the 'eeprom' command and retrieve them with the 'read' command. I'm glad to see you using the simulator. Stepping through a program is a great learning tool.

Good luck,
Rick
 

Sodapep

Member
You welcome. Regarding the eeprom command. It's just another way of storing data. Store the byte values using the 'eeprom' command and retrieve them with the 'read' command. I'm glad to see you using the simulator. Stepping through a program is a great learning tool.

Good luck,
Rick
Worked like a charm! Thank you. This is the code that has all the basics down. The direction selection was changed to readadc and the speed control was set to readadc10 in order to get just above 1s wait times between each step.

Code:
let dirsB = %11111111

direction:
readadc C.2,b3
if b3 <= 100 then R
if b3 > 100 and b3 < 175 then KR
if b3 >= 175 then L

R:
for b1 = 0 to 7 step 1
	readadc10 C.1,b0
	lookup b1, (%00000001,%00000010,%00000100,%00001000,%00010000,%00100000,%01000000,%10000000), b2
	let pinsb = b2
	pause b0
next b1
goto direction

KR:
for b1 = 0 to 7 step 1
	readadc10 C.1,b0
	lookup b1, (%00000001,%00000010,%00000100,%00001000,%00010000,%00100000,%01000000,%10000000), b2
	let pinsb = b2
	pause b0
next b1

for b1 = 6 to 0 step -1
	readadc10 C.1,b0
	lookup b1, (%00000001,%00000010,%00000100,%00001000,%00010000,%00100000,%01000000,%10000000), b2
	let pinsb = b2
	pause b0
next b1
goto direction

L:
for b1 = 7 to 0 step -1
	readadc10 C.1,b0
	lookup b1, (%00000001,%00000010,%00000100,%00001000,%00010000,%00100000,%01000000,%10000000), b2
	let pinsb = b2
	pause b0
next b1
goto direction
To make things more complicated, I'd like to experiment with other features, but am having a hard time finding any whiff of them in the data sheets. I'd imagine it's beyond what this microcontroller can do, but I figure I'd ask as if anyone knows, it would be you guys!

The first is add fading between the steps. In conjuntion with full on to off states, is there a way to have the on pin reduce in voltage as the off pin comes up? Cross fading I guess. Maybe there is a way to do it with pulse width controls? I'm pretty sure the outputs can only be high or low...

The second ability would be to read the times between applying voltage to an input pin with a pushbutton, then translating that time into the pause times. Similar to what the speed control does. I guess something like one push to notify the picaxe, then two more pushes where it reads the time between the pushes and sets the pause times.

Thanks in advance for your guidance. Any commands that I can take a look at for guidance would be great, if either of these options are even possible. Thanks again.
 

Rick100

Senior Member
The first is add fading between the steps. In conjuntion with full on to off states, is there a way to have the on pin reduce in voltage as the off pin comes up? Cross fading I guess. Maybe there is a way to do it with pulse width controls? I'm pretty sure the outputs can only be high or low...
Hello Sodapep,

Pins B.1, C.2, C.3, and C.5 can be used for PWM on the 20M2, if I read the documentation correctly. Use the PWMOUT wizard in the Picaxe editor to set them up. All the channels have to have the same frequency so you'll have a command like this for each pin:
Code:
pwmout pwmdiv4, B.1, 255, 511 ; 977Hz at 50% @ 4MHz
The 255 sets the frequency to 977 Hertz and the 511 sets the duty cycle to 50%.

To vary the duty cycle, which will vary the brightness for the led, use the 'pwmduty' command for each pin like this:
Code:
pwmduty B.1,1023 ; set pwm duty to 100%
Use a for next loop with a word variable to adjust the pwmduty value from 0 to 1023. That should get you started. To control more than 1 led per PWM channel will require some form of multiplexing.
I built a rotating beacon similar to a lighthouse using an 08M2 and 6 leds with 1 PWM channel. I used a form of charlieplexing with 3 pairs of leds. Since you have more PWM channels it should be easier.

Here's a thread about a similar project. I like the setup in post 12.
http://www.picaxeforum.co.uk/showthread.php?21966-How-can-you-get-a-lot-of-PWM-pins-to-fade-many-LEDs


Good luck,
Rick
 

Sodapep

Member
Thanks for the invaluable information.

Here is where I am! The code for the sequencer is good to go, thanks to you guys. I may have to mess around with scaling the speed control as the LDR that is being controlled can only act so fast, but that problem will be faced when I get to breadboarding.

For the tempo control. I have another ucontroller that can take a tap'd sequence and turn it into a clock signal. It also has a pin that goes high and low based on the clock. I can use the pulsin command to read this and set it as the pause value. I'm not sure how I want the control to be yet - as in if I want every pulse for it to move to the next output, or if I want to divide the pause value by 8, making the sequence start at the beginning every beat rather than moving to the next sequenced output. Another problem for the breadboard.

That leaves the fading idea. I have been looking into the charliplexing code you posted, Rick, and I've having trouble coming up with an applicable way to use this. I see how it's used to make LED's fade in and out, but I'm not sure I understand how to adapt that to the transistor switches that the PICAXE is driving. I thought switching two PWM cycles set alternating at speeds equal to how fast the sequence is going, mixed into the high/low sequencer pins, but now I don't get how that makes any sense...

The search continues!
 

Sodapep

Member
Another idea:

After a bit more reading, it seems the PWM can drive the transistor base itself. This idea uses two 4066 SPST switch IC's. PWM 1 connects to all four switches on the one 4066, PWM 2 on the other. 8 output pins from the PICAXE will determine what switches are thrown and when. Each PWM will not be applied to more than one transistor at a time.

When visualized in a sequence (let's use LED's for ease of seeing it rather than transistor switches) LED's 1, 3, 5 and 7 will be associated with PWM 1 and 4066 1 and LED's 2, 4, 6 and 8 will be associated with PWM 2 and 4066 2.

The frequency (I think) of the PWM will be swept based on how the user wants the dimming rate to go. At a slow rate, PWM 1 will sweep slowly and the LED will slowly come on. When the PWM has reached the max, PWM 2 will be activated on LED 2 and begin to cycle up. When LED 1 is totally out (also when LED is full on) the PICAXE will switch PWM 1 to LED 3 and so on and so forth.

Eh?
 
Top