Help with Metronome project

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
 

Attachments

Last edited:

Dippy

Moderator
I'm not sure how slight is 'slight', but what happens if you comment-out the Debug statement? That takes a bit of time albeit slight.
PS. I haven't tried it, it's just a thought.
 

Tricky Dicky

Senior Member
Ta! Dippy

Solved that in one go. I should have RTFM, just spotted that little snippet in the manual.

Any views on the rest of the code?

Richard
 

Dippy

Moderator
Well each sub could be shrunk to a small loop; just look at the numbers and how they halve..... and then double....

Not sure if you can do 'infinite' do/while loops in PICAXE, to replace the Goto, I shouldn't worry about it.
 

BCJKiwi

Senior Member
Since timing is the critical issue, the program could be refined to provide more consistent timing.

Adjust the duration of the sound so it matches the the pause length (same sort of algorithm as for the delay adjustment) - You already have a 'missing LED' at each end of the R2L / L2R and this 'missing LED' time slot could be used by the sound command of the same duration. Make this sound duration slightly shorter than the pause to compensate for the once per cycle code at Label_12D - if you need that degree of linkage between sound and light.

The program actually does not require most of the code in Label_12D - see suggestion below;
Code:
#PICAXE 18X
main:
  let b0=3
  let b2=1
label_12D: readadc  0,b1
  let w3=b0*b1
  let w4=b2*b1/5
'  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)
  sound  7,(100,w4)
'  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)
  sound  7,(100,w4)
'  return
goto Label_12D
 
Top