Electronics Projects: How to Use IF Statements in PBASIC Code
Electronics Projects: How to Assign Names to I/O Pins in PBASIC Code
Electronics Components: Connect I/O Pins on a HomeWork Board

Electronics Projects: How to Create Sound Effects

With creative use of PBASIC commands, you can create some interesting and at times annoying sound effects in your electronics projects. The idea is to use short durations in the FREQUOUT command and use FOR-NEXT loops or some other means to vary the frequency. You can also use PAUSE commands between tones to create beeping or clicking effects.

The best way to learn what kinds of sound effects are possible with the FREQOUT command is to experiment. Use the programs included here as starting points for your own experiments.

This program plays two different beeping sounds when you press one of the pushbuttons. If you press Switch1 (on pin 14), a 5,000 Hz tone beeps twice per second. If you press Switch2 (on pin 10), a 5,000 Hz tone beeps five times per second.

' Sound Program
' Doug Lowe
' July 15, 2011
'
' This program creates fast and slow beeping sounds.
' A piezo speaker must be connected to pin 0.
' The normally open pushbutton switches must be connect to pins 10 and 14.
' {$STAMP BS2}
' {$PBASIC 2.5}
Speaker  PIN 0
Switch1  PIN 10
Switch2  PIN 14
Frequency VAR Word
Time   VAR Word
DO
 IF Switch1 = 1 THEN
  FREQOUT Speaker,250, 5000
  PAUSE 250
 ELSEIF Switch2 = 1 THEN
  FREQOUT Speaker,100, 5000
  PAUSE 100
 ENDIF
LOOP

This next program shows how you can use FREQOUT within a FOR-NEXT loop to create a continuously rising or falling tone, much like a police siren. The program varies the frequency from 3,000 to 5,000 Hz. When you press either of the pushbuttons, the rate at which the pitch rises and falls changes.

The rate at which the pitch rises or falls is governed by a variable named Time. Each time through the FOR-NEXT loop, the program calls a subroutine named GetTime, which checks the status of the pushbutton switches and changes the Time variable if either of the switches is down. That’s how the program changes the rate of the pitch change when the buttons are pressed.

' Siren Effect Program
' Doug Lowe
' July 15, 2011
'
' This program generates a rising and falling pitch similar to a police siren.
' The rate at which the pitch rises and falls changes if you press either
' of the two pushbuttons.
' {$STAMP BS2}
' {$PBASIC 2.5}
Speaker  PIN 0
Switch1  PIN 10
Switch2  PIN 14
Frequency VAR Word
Time   VAR Word
DO
 FOR Frequency = 3000 TO 5000 STEP 15
  GOSUB SetTime
  FREQOUT 0, Time, Frequency
 NEXT
 FOR Frequency = 5000 TO 3000 STEP 15
  GOSUB SetTime
  FREQOUT 0, Time, Frequency
 NEXT
LOOP
SetTime:
 Time = 15
 IF Switch1 = 1 THEN
  Time = 5
 ENDIF
 IF Switch2 = 1 THEN
  Time = 2
 ENDIF
 RETURN
  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Electronics Projects: How to Use Comments in PBASIC Code
Electronics Projects: How to Program Music on a BASIC Stamp
Digital Electronics: How to Install the BASIC Stamp Editor and Connect to the Stamp
Electronics Projects: How to Use a Piezo Speaker with a BASIC Stamp
Electronics Components: Buying a BASIC Stamp
Advertisement

Inside Dummies.com