Shop OBEX P1 Docs P2 Docs Learn Events
Need Help with code for a Doppler Effect for Sound demo project — Parallax Forums

Need Help with code for a Doppler Effect for Sound demo project

enjelikaenjelika Posts: 7
edited 2013-04-22 19:05 in BASIC Stamp
I am currently working on creating a demonstration for the Doppler Effect for Sound for an Intro to ENGR class:For this apparatus, there is an IR remote control that can turn the power on/off in which a rotational motor and the affixed speakers will turn on/off. Also, this IR remote control can cycle through a total of three preset frequencies: 500 MHz, 650 MHz, and 800 MHz.
I have tried my best to find information (via web searches) on how the coding will be for programming frequencies into the microprocessor to no avail. Also, I will need help on how to set up the program for the IR remote control element.

Please, can anyone help?

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2013-04-10 10:36
    You need to provide a better explanation of what you're trying to do. Specifically, what's the microcontroller supposed to do? What's it supposed to control? How's the Stamp supposed to control the power to the motor? How's it supposed to control the preset frequencies?

    There's a good reference for IR remote control programming. See "IR Remote for the BoeBot Robot" here.

    As mentioned in this tutorial, you need a remote control that uses Sony's remote control protocol. Most universal remotes can be set to use this. You'll also need a few parts shown in the tutorial. Most of these can be found at a RadioShack store or equivalent or they can be ordered from Parallax.
  • enjelikaenjelika Posts: 7
    edited 2013-04-10 15:16
    The IR remote needs to be able to turn the motor on (which will be set to a constant rpm), and then either cycle through three frequencies by a push of a button or choose one of three frequencies via the remote.
    Is there a way for the Stamp to turn the motor on and then, while the motor is on, send the frequency chosen to the speaker? Can this be done with one Stamp or do I need two?
  • Mike GreenMike Green Posts: 23,101
    edited 2013-04-10 20:24
    The Stamp can decode the button presses from the remote control and then act on them. Depending on how the motor is turned on and off and what its power demands might be, the Stamp can control a relay to supply power to the motor (see Nuts & Volts Column v1 n6 on controlling high powered devices with a Stamp). It's still not clear how you propose to control the frequency generator. The Stamp can substitute for the pushbutton that cycles through the three frequencies, again by using a relay.

    As far as relays are concerned, the Stamp turns it on or off and the relay will stay in tha state until the Stamp changes it. I still don't understand what you mean by "send the frequency chosen to the speaker". Do you expect the Stamp to generate the signal to the speaker? The Stamp can't really do that. It can generate tones in the range of 1Hz to 32KHz, but it can't do anything else while doing that. It can't look for IR signals. It can't change the relay settings. It also can't produce a signal that would drive a speaker, not without amplification. A 555 timer based tone generator would work using a power transistor as an amplifier. Do a web search for "tone generator ic". The Stamp could use reed relays to switch in and out different resistors to set the frequency.
  • enjelikaenjelika Posts: 7
    edited 2013-04-15 06:29
    Thank you, Mr. Green. The information provided helps greatly. Our lab intructor for class made it sound like the STAMP can create the audio frequencies, but I can see he was wrong. I'll need to set up some kind of circuit for the STAMP to control. Here's the basic idea for the STAMP:
    project layout.jpg

    Now, to figure out how to make an in-expensive tone generator circuit that will create the frequencies needed.

    Thank you again for all of your help.
    1024 x 1189 - 101K
  • LeonLeon Posts: 7,620
    edited 2013-04-15 06:35
    You don't really mean 500 MHz, 650 MHz and 800 MHz, do you?
  • enjelikaenjelika Posts: 7
    edited 2013-04-15 07:03
    I meant Hz. I had to look back at my project requirements. For some reason I kept thinking MHz.
  • Mike GreenMike Green Posts: 23,101
    edited 2013-04-15 07:25
    You can use a 555 timer to generate the tones (and drive a small speaker). In the datasheet, there's an example of an astable multivibrator that can be set for 800Hz with a particular resistor value for Ra. The Stamp can connect additional resistors in parallel with Ra to lower its value to produce the other frequencies since one end of Ra is connected to +5V. You'd connect these resistors to the Discharge pin of the 555 along with one end of Ra. The other end of the resistor would be connected to a Stamp I/O pin. When the I/O pin is in INPUT mode, the additional resistor is not connected. When the I/O pin is in HIGH mode, the additional resistor is in parallel with Ra.
  • LeonLeon Posts: 7,620
    edited 2013-04-15 08:10
    The easiest way to create those tones is to use the NCO in a PIC12F1501. Here is some test code of mine:
    ;NCO.asm
    ;Test program for 12F1501
    ;LED on RA2 (pin 5)
    ;NCO1 outputs on RA1 (pin 6) and RA5 (pin 2)
    
    
    	list		p=12f1501		; list directive to define processor
    	#include	<P12F1501.inc>	; processor specific variable definitions
    	errorlevel	-302
    
        __CONFIG _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_ON & _CP_OFF & _BOREN_OFF & _CLKOUTEN_OFF 
        __CONFIG _CONFIG2, _WRT_OFF & _STVREN_OFF & _LVP_OFF
    
    
    	ORG			0x000		; processor reset vector
      	goto		Main		; go to beginning of program
    
    	ORG			0x004		; interrupt vector location
    							; W reg, STATUS, BSR, FSTs, aand PCLATH regs are saved
    							; automatically in the Shadow regs (Bank 31)
    	retfie
    
    Main:
    	banksel		OSCCON
    	movlw		b'01111000'		; Int. osc. 16 MHz
    	movwf		OSCCON
    	btfss		OSCSTAT, HFIOFR	; Int. osc. running?
    	goto		$-1				; No, loop back
    	btfss		OSCSTAT, HFIOFS	; Osc. stable?
    	goto		$-1				; No, loop back.
    	
    	; initialise I/Os
    	banksel		TRISA
    	movlw		b'11111101'		; RA1 output (pin 6)
    	movwf		TRISA
    	movlw		b'11011111'		; RA5 output (pin 2)
    	movwf		TRISA
    
    	; initialise NCO registers
    	banksel		NCO1CON			
    	movlw		b'11010000'		; Enable NCO, enable O/P, O/P low, O/P active high, fixed duty-cycle mode.
    	movwf		NCO1CON
    	movlw		b'01100000'		; NCO output pulse 8 clocks, clock source HFINTOSC (16 MHz).
    	movwf		NCO1CLK
    	movlw		b'00000000'		; High inc. register (needs to precede setting of low inc. register).
    	movwf		NCO1INCH
    	movlw		b'00000001'		; Low inc. register.
    	movwf		NCO1INCL
    	banksel		APFCON
    	bsf			APFCON,NCO1SEL	; NCO1 output on RA5
    
    Loop:
    	goto		Loop
    
    	end
    
    
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2013-04-15 09:06
    Your lab instructor is not wrong. The Stamp can produce those frequencies, but you have to compromise about the way your demonstration works. As mentioned above, while the Stamp is generating the frequencies, it can't be listening for new codes from the remote or changing the on-off state of the motor. Is that really a problem without a workaround? Start the motor, choose a frequency, and listen to the result for a few seconds. Then turn on a green light to tell the participant that they can enter a new code.
  • enjelikaenjelika Posts: 7
    edited 2013-04-17 10:50
    I ended up making a simple tone generator circuit which depending on which pin from the STAMP powers it will determine the audio frequency omitted by the speaker. I went this route since the STAMP has to control the power to the motor too. Here's the code I have so far, I'll finish filling in the blanks later today when I find out what each button input signal:

    'Doppler Effect for Sound Demo - DopEffSound.bs2
    'IR remote turn motor on and off. IR remote choses audio frequency.
    '{$STAMP BS2}
    '{$PBASIC 2.5}

    '
    [Program description]
    'This program is designed to be used with the Doppler Effect for Sound
    'demonstration at Rose State College. The IR remote control signals
    'are used for controlling the demonstration; motor ON/OFF,
    'audio frequencies 500Hz, 650Hz, and 800Hz.
    '

    '
    [I/O definitions]
    IRdetect PIN 3
    Motor PIN 5 '10 volts
    Freq500 PIN 8 ' 5 volts
    Freq650 PIN 10 ' 5 volts
    Freq800 PIN 13 ' 5 volts
    '

    '
    [Variables]
    IRinput VAR Nib 'change to what the remote signal is (byte?)

    '
    [Program Code]
    Main:
    DO
    IRinput = IRdetect

    IF IRinput = (condition) THEN
    DO
    HIGH Motor

    IF IRinput = (condition) THEN
    DO
    HIGH Freq500

    ELSE IRinput = (condition) THEN
    DO
    HIGH Freq650

    ELSE IRinput = (condition) THEN
    DO
    HIGH Freq800
    END IF

    ELSE
    END IF
    LOOP

    Am I on the right track?
  • Mike GreenMike Green Posts: 23,101
    edited 2013-04-17 11:17
    Where do you turn off the motor and the outputs that control the tones? Once you set an I/O pin to HIGH, it stays that way until you turn it off using INPUT (or, in the case of the motor only, using LOW).

    Do remember that "IRinput = IRdetect" is really quite complex. The part about doing something with the IR codes is really very simple. You might just start with the IR detection code from "IR Remote for the BoeBot". Chapter 2 shows a variety of programs of increasing complexity depending on how you want to control things. In most cases, there's a SELECT statement where each CASE is a response to a particular remote control button. One might turn the motor on (HIGH) and another might turn it off (LOW). One CASE might set Freq800 HIGH and the others to INPUT, etc.
  • enjelikaenjelika Posts: 7
    edited 2013-04-18 06:26
    There is a nested If/Then loop within the Motor If/Then statement where the frequencies can be controlled. I was in a rush to get to class that I didn't fix it before I posted it.
    Would that work?
  • Mike GreenMike Green Posts: 23,101
    edited 2013-04-18 06:39
    It's really impossible to give specific advice without more information. Whether something works or not depends on what you mean by "work" and the context, so "nested If/Then" and "Motor If/Then" are not particularly helpful without the full source code (not a short-hand version with pieces missing) to examine and refer to. Again, the code for recognizing and interpreting the IR remote signal is going to set the overall structure of your program since it's large and complex while controlling the tone generator and motor is quite simple by comparison.
  • enjelikaenjelika Posts: 7
    edited 2013-04-22 10:33
    Eureka! This past weekend my group managed to get the project working. What we ended up doing was have one STAMP running the IR detect program which when a certain IR signal is received there will be a binary output to PIN 14 which will input a binary 1 to PIN 3 on the frequency generator STAMP which will output three cycling Hz frequencies to the speaker.

    We had a bit of an issue when soldering the circuit onto a printed circuit board, but we managed to fix it.
    Thank you for all of the references, and info. It helped greatly.

    - Debra
  • bsjohnbsjohn Posts: 19
    edited 2013-04-22 19:05
    Please share the final version of the code.
Sign In or Register to comment.