Controlling the duration of a the freqout command with a potentiometer
Ypasner
Posts: 4
I am attempting to write a program that will play the song "Twinkle Twinkle Little Star" using a Piezoelectric Speaker that has a potentiometer attached that can increase or decrease the duration of each note. I use a BS2 program and am just a beginner. So far I have tried using the freqout command (pin, duration, and frequency). I wrote a subroutine that utilized the RCtime command and named it "duration". This subroutine displayed the resistance of the potentiometer in terms of time, which I declared as a variable for the subroutine. I then used the GOSUB duration command in the duration slot of the freqout command. That didn't work. I also tried just using the variable "time" in the duration slot and was deemed unsuccessful yet again. Is there a different way to do this?
Comments
You probably know that the DEBUG command really slows down program execution, so you need to comment all DEGUGs out to test your program at full speed.
DO
duration:
time VAR word
HIGH 0
PAUSE 100
RCTIME 0, 1, time
DEBUG HOME, "time = ", DEC5 time
RETURN
FREQOUT 15, 2093, GOSUB duration
FREQOUT 15, 2093, GOSUB duration
FREQOUT 15, 3136, GOSUB duration
FREQOUT 15, 3136, GOSUB duration
FREQOUT 15, 3520, GOSUB duration
FREQOUT 15, 3520, GOSUB duration
FREQOUT 15, 3136, GOSUB duration
LOOP
I've also tired replacing the "GOSUB duration" with just the time variable. But that didn't work. I can't think of a good way to get around using the DEBUG command because I am not super familiar with the RCTIME command. I was wondering if there is a DATA index I need to make or LOOKUP LOOKDOWN... But I haven't been able to find any answers yet.
Try moving the duration subroutine outside of the DO/LOOP, and use the time variable in the FREQOUT command:
What is the circuit you're using for RCTIME?
Are you getting any reasonable number for variable time from the DEBUG command?
If you've questions about that command or circuit, check the RCTIME section in the BasicStamp editor help system.
RCTIME returns a value in units of 2 microseconds, whereas FREQOUT works with units of milliseconds. You may have to adjust the time values. A lot depends on the circuit. Ideally, I think the RCTIME command can happen very fast to give back a quick reading in microseconds, then adjust that by multiplying if necessary to give a note of reasonable length.
The PAUSE 100 will be unnecessary,
The capacitor charged during the time the note is playing.
...why, thank you Sir!
That is the circuit that I am using for the RCtime except I'm using pin 0 instead of 7. I'm getting numbers between 0 and ~ 575 in the DEBUG command. How would I multiply to get a note of reasonable length?
With the circuit as is, I am getting the RCtime circuit to work, but no noise. I am using the following code (sorry about the previous code. I think the freqout command uses a pin, duration, frequency pattern):
time VAR word
duration:
RCTIME 0,1, time
DEBUG HOME, "time = ", DEC5 time
HIGH 0
RETURN
DO
GOSUB duration
FREQOUT 15, time, 2093
FREQOUT 15, time, 2093
FREQOUT 15, time, 3136
FREQOUT 15, time, 3136
FREQOUT 15, time, 3520
FREQOUT 15, time, 3520
FREQOUT 15, time, 3136
LOOP
I'm using the following circuit for my speaker excpet pin 15 instead of 9:
Snapshot 2012-11-07 18-04-55.tiff
Thank you all every much by the way. This circuit has been driving me nuts for days. This is also the first forum I've ever been in, so thank you for all being helpful, kind, and awesome!
If your program is entered the way you have it printed, it
would appear to me that your program is stuck in the duration loop.
duration:
RCTIME 0,1, time
DEBUG HOME, "time = ", DEC5 time
HIGH 0
RETURN
Using the RETURN statement statement where you have it in your program would appear to me that the program will return back to the beginning of the duration subroutine.
The program never gets a chance to go to your DO LOOP.
And yes, this particular Forum is filled with helpful people who enjoy sharing information without flames.
Hope to see you around!
BTW - I make a small edit to your original post and marked the thread as "solved" to inform other readers.