Bx-24 Code

' final code for the Nervous Jello by Elizabeth Goodman, 2001

'--- DECLARE VARIABLES -----------------
'------ Board Set-Up Variables ---------
dim potPin as byte
dim sensor1Pin as byte
dim sensor2Pin as byte
dim speakerPin as byte
dim servoPin as byte
dim LEDPin as byte

'------ Sensor Variables -------------
dim potVar as integer		' affects sensor 1

dim oldSensor1 as integer		' attached to disc
dim newSensor1 as integer
dim sensor1Out as integer

dim oldSensor2 as integer
dim newSensor2 as integer		' attached to scale
dim sensor2Out as integer

'------ Servo Variables ---------------
dim minPulse as single 		' the minimum pulseWidth - standard .0008
dim maxPulse as single 		' the maximum pulseWidth - .0023, but .0015 is good for small shakes
dim pulseWidth as single 	' the servo's destination pulse width
dim pulseStep as single		' how far the servo goes with each "step"
dim refreshPeriod as single ' the time between pulses  (mandatory)
dim i as integer			' repeat val
dim servoShake as integer	' how many times the servo shakes

'------ Music Variables ---------------
dim freq1 as integer
dim freq2 as integer
dim duration as integer
dim musicValue as integer

'---- MAIN ----------------------------
Sub Main() 

	LEDPin = 8
	potPin = 14
	sensor1Pin = 17
	sensor2Pin = 15

	call flashme(3)

	do
		call putPin(LEDPin, 1) 		' turns on the LED
		potVar = getADC(potPin) 	' regulates the sensitivity of sensor2 (adjusted on board)
		
		oldSensor2 = getADC(sensor2Pin)
		newSensor2 = getADC(sensor2Pin)
		sensor2Out = newSensor2 - oldSensor2
		
		if (sensor2Out < 0) then
			sensor2Out = -sensor2Out
		end if

		if (sensor2Out > potVar) then
			call shakeIt()
		end if
		
	loop

End Sub

'---- SHAKE IT ------------------------

Sub shakeIt()
	
	servoPin = 12

	minPulse = 0.0008	
	maxPulse = 0.0023
	pulseStep = 0.00001 
	refreshPeriod = 0.02 

	i = 0
	servoShake = 2
	
	do while  i <= servoShake	
			
		call pulseOut(servoPin, pulseWidth, 1) 	' move the servo/
		call sleep(refreshPeriod)

		oldSensor1 = getADC(sensor1Pin)			' get the value that sets the pitch
		newSensor1 = getADC(sensor1Pin)
		sensor1Out = newSensor1 - oldSensor1
		
		if (sensor1Out < 0) then
			sensor1Out = -sensor1Out
		end if

		' increases the pulsewidth for the next pulse: 
		if (pulseWidth <= maxPulse) then 			
			pulseWidth = pulseWidth + pulseStep
		else 
			pulseWidth = minPulse 				' back to beginning and
			call makeMusic(sensor1Out)
			i = i + 1							' increment the counter
		end if 

	loop

End Sub

'---- MAKE MUSIC ----------------------
Sub makeMusic(byVal musicValue as integer) 		' audible sound is between 200 - 3000 hz. 
												' length of tone in ms from 0 to 2000
	speakerPin = 10								' FreqOut outputs two frequencies in hz.
												' FreqOut halts other chip functions, so be careful
			
	freq1 = (musicValue * 10) + (musicValue * 8) + 1500			' violin freq is from 500 - 2000 hz 
	freq2 = freq1 + 300	
	duration = 300				
	call freqOut (speakerPin, freq1, freq2, duration)

	freq1 = (musicValue * 9) + (musicValue * 7) + 1500			' violin freq is from 500 - 2000 hz 
	freq2 = freq1 + 400
	duration = 300

	call freqOut (speakerPin, freq1, freq2, duration)

End Sub

'---- FLASHME -------------------------
Sub Flashme (byVal flashx as byte)
	dim x as byte
	for x = 0 to flashx
		call putPin(25,0)
		delay(0.1)
		call putPin(25,1)
		delay(0.1)
	next
	call putPin(26,0)
End Sub