Tricky Dicky
Senior Member
I have a student who is making a metronome using a PICAXE 18A. The outputs are 7 LEDs which light up one after another from left to right and back right to left with a piezzo sounder emitting a short tone at the end of each swing. He wants to achieve a maximum beat rate of 200 beats per minute (seems a bit frenetic to me but then I am no musician).
His idea to get varying beats per minute is to use a potentiometer for an input and vary the time delays between each LED lighting. He started to programme in PIC Logicator and got part of his program running when we realised that due to the limitations of PIC Logicator he was unable to do readadc to a variable in order to do the necessary maths.
I have transferred his code as near to the original into Programme Editor and have been brushing up on my BASIC (programming is not my strong point) getting his code to work so that I am able to answer the inevitable questions when he gets back to the problem.
Firstly, the maths we worked it out that at 200 beats per min, the LEDs should swing through in approx. 300ms (ignoring for now the 0.1s tone at the end of each swing). With 6 delays between each LED that gives a minimum of 50ms delay as a base rate.
Secondly, the code. The attached code is what I have come up with which seems to be working albeit there does seem to be a slight delay between tones depending which way the LEDs are swinging which I cannot see why. I would be grateful if someone could cast a knowing eye over the code which I have to admit has come about more by trial and error, and see if there is anything I can improve. I realise there are probably far better solutions out there in PICAXE land but I would like to stick as far as possible to my student's approach and preserve as much of his original code rather than a completely radical new solution even if his approach might seem inferior. It more important for his GCSE project that it his original work.
I hope I get this attachment business right submitting this code is a first for me
Richard
OOps! got it wrong. Here goes again
'BASIC converted from flowchart:
'Untitled Flowchart:2
'Converted on 13/04/2008 at 16:00:13
main:
let b0=3
label_12D: readadc 0,b1
let w3=b0*b1
debug w3
gosub left_to_right
readadc 0,b1
let w3=b0*b1
gosub right_to_left
goto label_12D
left_to_right:
let pins = 2 ' %00000010
pause w3
let pins = 4 ' %00000100
pause w3
let pins = 8 ' %00001000
pause w3
let pins = 16 ' %00010000
pause w3
let pins = 32 ' %00100000
pause w3
let pins = 64 ' %01000000
pause w3
let pins = 0
sound 7,(100,10)
return
right_to_left:
let pins = 32 ' %00100000
pause w3
let pins = 16 ' %00010000
pause w3
let pins = 8 ' %00001000
pause w3
let pins = 4 ' %00000100
pause w3
let pins = 2 ' %00000010
pause w3
let pins = 1 ' %00000001
pause w3
let pins = 0
sound 7,(100,10)
return
His idea to get varying beats per minute is to use a potentiometer for an input and vary the time delays between each LED lighting. He started to programme in PIC Logicator and got part of his program running when we realised that due to the limitations of PIC Logicator he was unable to do readadc to a variable in order to do the necessary maths.
I have transferred his code as near to the original into Programme Editor and have been brushing up on my BASIC (programming is not my strong point) getting his code to work so that I am able to answer the inevitable questions when he gets back to the problem.
Firstly, the maths we worked it out that at 200 beats per min, the LEDs should swing through in approx. 300ms (ignoring for now the 0.1s tone at the end of each swing). With 6 delays between each LED that gives a minimum of 50ms delay as a base rate.
Secondly, the code. The attached code is what I have come up with which seems to be working albeit there does seem to be a slight delay between tones depending which way the LEDs are swinging which I cannot see why. I would be grateful if someone could cast a knowing eye over the code which I have to admit has come about more by trial and error, and see if there is anything I can improve. I realise there are probably far better solutions out there in PICAXE land but I would like to stick as far as possible to my student's approach and preserve as much of his original code rather than a completely radical new solution even if his approach might seem inferior. It more important for his GCSE project that it his original work.
I hope I get this attachment business right submitting this code is a first for me
Richard
OOps! got it wrong. Here goes again
'BASIC converted from flowchart:
'Untitled Flowchart:2
'Converted on 13/04/2008 at 16:00:13
main:
let b0=3
label_12D: readadc 0,b1
let w3=b0*b1
debug w3
gosub left_to_right
readadc 0,b1
let w3=b0*b1
gosub right_to_left
goto label_12D
left_to_right:
let pins = 2 ' %00000010
pause w3
let pins = 4 ' %00000100
pause w3
let pins = 8 ' %00001000
pause w3
let pins = 16 ' %00010000
pause w3
let pins = 32 ' %00100000
pause w3
let pins = 64 ' %01000000
pause w3
let pins = 0
sound 7,(100,10)
return
right_to_left:
let pins = 32 ' %00100000
pause w3
let pins = 16 ' %00010000
pause w3
let pins = 8 ' %00001000
pause w3
let pins = 4 ' %00000100
pause w3
let pins = 2 ' %00000010
pause w3
let pins = 1 ' %00000001
pause w3
let pins = 0
sound 7,(100,10)
return
Attachments
-
895 bytes Views: 12
Last edited: