How do I do this [setfreq via an ADC reading]

I'd like to readadc and assign to a set of predefined values, specifically I want to use a pot to set the setfreq parameter, so I only want to set it to any of m1, m2, m4, m8, m16, m32, it will be in the main loop of the program, is it even possible?
 

premelec

Senior Member
Basically - READADC b0
If b0 > 10 and b0 < 20 THEN GOSUB freq1
ELSE
IF b0 >20 and b0 < 30 THEN GOSUB freq2
ELSE
etc.


freq1:
setfreq m1
Return

freq2:
setfreq m2
Return

And so on - or use the CASE statement etc...
 
Thanks, looking at the gosub way makes more sense for a beginner like me to keep track of things, however I referred to the manual which states:
"IF condition THEN
{code}
ELSEIF condition THEN
{code}
ELSEIF condition THEN
{code}
END IF"

Yet when I use that scheme, like this
IF b3 > 10 and b3 < 20 THEN GOSUB freq1
ELSEIF b3 >20 and b3 < 30 THEN GOSUB freq2
ELSEIF b3 > 30 and b3 < 40 THEN GOSUB freq3
ELSEIF b3 >40 and b3 < 50 THEN GOSUB freq4
ELSEIF b3 > 50 and b3 < 60 THEN GOSUB freq5
ELSEIF b3 >60 and b3 < 70 THEN GOSUB freq6
ENDIF
I get this syntax error
"ELSEIF b3 >20 and b3 < 30 THEN GOSUB freq2
^
Syntax error on line 18 at/before position 9

Error: Elseif without If"

Even though it appears I followed it correctly?
 

hippy

Technical Support
Staff member
The problem is that you have THEN GOSUB on the same line, with no colon statement separator. This is confusing the compiler as to what you mean.

I would personally use a SELECT CASE statement to do what you want ...

Code:
[color=Blue]ReadAdc [/color][color=Black]POT_PIN, [/color][color=Purple]b0[/color]
[color=Blue]Select Case [/color][color=Purple]b0
  [/color][color=Blue]Case [/color][color=DarkCyan]<=  [/color][color=Navy]43 [/color][color=Black]: [/color][color=Blue]Gosub [/color][color=Black]Freq1
  [/color][color=Blue]Case [/color][color=DarkCyan]<=  [/color][color=Navy]85 [/color][color=Black]: [/color][color=Blue]Gosub [/color][color=Black]Freq2
  [/color][color=Blue]Case [/color][color=DarkCyan]<= [/color][color=Navy]128 [/color][color=Black]: [/color][color=Blue]Gosub [/color][color=Black]Freq3
  [/color][color=Blue]Case [/color][color=DarkCyan]<= [/color][color=Navy]170 [/color][color=Black]: [/color][color=Blue]Gosub [/color][color=Black]Freq4
  [/color][color=Blue]Case [/color][color=DarkCyan]<= [/color][color=Navy]213 [/color][color=Black]: [/color][color=Blue]Gosub [/color][color=Black]Freq5
  [/color][color=Blue]Else        [/color][color=Black]: [/color][color=Blue]Gosub [/color][color=Black]Freq6
 [/color][color=Blue]End Select[/color]
 
Ah, appears that I need : after each THEN statement

EDIT Thanks Hippy, just sussed it after looking at another example, (damn newbs! ;))
 

geoff07

Senior Member
'cos the test is evaluated much more quickly in the firmware, compared to several tests in interpreted code. It is also easier to comprehend at a glance. If using the elseifs, you would not need both expressions to be evaluated, as the possible values reduce as each test is checked, as per hippy's case example.
 
Thanks for the explanation, I am messing about with the sound command, and noticed that if I have a pot control for note/noise, and another for duration some interesting sounds can be had, then I experimented with changing the setfreq and noted that it affects both the pitch and duration,so my next experiment is to have this 3rd pot to change the setfreq as per above, currently it compiles ok but does not run as expected, probably due to my code layout/structure, I'll post it later as got to go out now for a while.
 

Technical

Technical Support
Staff member
Quickest would be

Code:
[color=Blue]ReadAdc [/color][color=Black]POT_PIN, [/color][color=Purple]b0
b0 [/color][color=DarkCyan]= [/color][color=Purple]b0 [/color][color=DarkCyan]/ [/color][color=Navy]43 [/color][color=Green]'(reduce to value 0-5)[/color]
[color=Blue]on [/color][color=Purple]b0 [/color][color=Blue]gosub [/color][color=Black]freq1, freq2, freq3, freq4, freq5, freq6[/color]
 
Thanks, my eventual (if perhaps slightly deluded) goal is to make a kind of primitive "chip tune" drum machine/synth on the 08M2, I'm thinking of a way to have various sound parameters in some kind of index, these would have variations of note/noise, duration, setfreq, with the idea being to use a pot to select from this index, so lets say 20 different combinations spread across the pot.
 
Just for future reference, if in the above scenario I manage to create an index of sounds and use a button to play whichever sound the pot is selecting, given that the sound duration will be specified, is there a way to have a new button press interupt the current playing sound and start the new sound?
 

rossko57

Senior Member
You can arrange the button to reset the whole Picaxe, and restart your code from the top, which might do here.
 
Top