Shop OBEX P1 Docs P2 Docs Learn Events
Controlling the duration of a the freqout command with a potentiometer — Parallax Forums

Controlling the duration of a the freqout command with a potentiometer

YpasnerYpasner Posts: 4
edited 2012-11-08 00:00 in BASIC Stamp
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

  • ercoerco Posts: 20,257
    edited 2012-11-06 13:17
    Welcome to the forums, Ypasner! Your method sounds quite logical, but the devil's in the details (code). Attach your program so we can have a look and try to help out.

    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.
  • YpasnerYpasner Posts: 4
    edited 2012-11-06 23:41
    The program I have written out now is as follows:

    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.
  • davejamesdavejames Posts: 4,047
    edited 2012-11-07 00:33
    Hi Ypasner - welcome to the Forum.

    Try moving the duration subroutine outside of the DO/LOOP, and use the time variable in the FREQOUT command:
    time VAR word
    
    DO
      GOSUB duration
    
      FREQOUT 15, 2093, time
      FREQOUT 15, 2093, time
      FREQOUT 15, 3136, time
      FREQOUT 15, 3136, time
      FREQOUT 15, 3520, time
      FREQOUT 15, 3520, time
      FREQOUT 15, 3136, time
    LOOP
    
    duration:
      HIGH 0
      PAUSE 100
      RCTIME 0, 1, time
      DEBUG HOME, "time = ", DEC5 time
    RETURN
    

    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.
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2012-11-07 09:36
    +1 on Dave's comments.

    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,
    [SIZE=1][FONT=courier new]duration:
         RCTIME 0, 1, time   
         ' DEBUG HOME, "time = ", DEC5 time ' commented out once working
         HIGH 0      '  moved here, no more PAUSE 100
       RETURN[/FONT][/SIZE]
    

    The capacitor charged during the time the note is playing.
  • davejamesdavejames Posts: 4,047
    edited 2012-11-07 11:30
    +1 on Dave's comments.

    ...why, thank you Sir!
  • YpasnerYpasner Posts: 4
    edited 2012-11-07 18:06
    Snapshot 2012-11-07 17-26-37.tiff


    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!
  • garyggaryg Posts: 420
    edited 2012-11-07 18:23
    I'm definitely no expert, but:
    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.
  • YpasnerYpasner Posts: 4
    edited 2012-11-07 18:30
    Wow! I just switched the order and it works! Thank you all for the help, it's very much appreciated. I'm sure I'll be back very soon for more programming help, I'm starting on bobots next week. Thanks!
  • davejamesdavejames Posts: 4,047
    edited 2012-11-08 00:00
    Glad to here it's working. Thanks for letting us know.

    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.
Sign In or Register to comment.