Wednesday, January 6, 2010

Code for BS2 Homebrew board

' {$STAMP BS2}
' {$PBASIC 2.5}
' Dump4everHB4-1.bs2 as of 2009dec29 from Dump4everV3.bs2
' reWritten for a Homebrew Stamp board
' V1 started in 2009November
'Thanks: Wayne Roderick DumpA403.bs2 04/03/2000 tetonsl@ida.net
' V2 Changed Positioner code to try to get the push bar to place car OVER brake
' V3 Changed Pusher bar to a Coupler Grabber
' HB4 Homebrew Stamp board and added IR Remote and another Servo
'HB4-1 Hardwired a RESET button on the Box
'Emergency Stop button code removed as OBE
'Removing IF (emergencyStopBtn=1) THEN END
'Cleared enough code to use Make_Music :) NotesAndDurations.bs2
'Initialize Servo pins to LOW :
'PULSOUT ArmServo, 466 ' Arm UP 'dunno what step does to leftovers
'PAUSE 20
'DIR13=0 'remove pulse to insure stop
'that would make it an antennae, PULSOUT returns it to previous: LOW output

Copyright DATA "I","v","a","n","H","e","r","r","i","n","g","2","0","1","0"
' What's a Microcontroller - NotesAndDurations.bs2
' Play the first few notes from Frere Jacques. 14
Notes DATA "C","D","E","C","C","D","E","C","E","F","G","E","F","G","Q"
Durations DATA 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 4, 4, 2

'Beethovans 5th 9
'Notes DATA "G","G","G","e","P","F","F","F","D","Q"
'Durations DATA 8, 8, 8, 2, 8, 8, 8, 8, 2

' Twinkle Twinkle 14 fudged durations, i dunno
'Notes DATA "C","C","G","G","A","A","G", "F", "F", "E", "E", "D", "D", "G","Q"
'Durations DATA 4, 4, 4, 4, 4, 4, 8, 4, 4, 4, 4, 4, 4, 8
WholeNote CON 2000


' Controls Five servos of an automated HO scale rotary dumper
' 1. Dumper motor is a Parallax Futaba limitless servo.
' It has a PULSOUT dead band (758..761 x2usec) 1515..1522.
' Values near the deadband control the speed that reachs a maximum
' of about 60RPM at PULSOUT values of about +/-40. 1 RevPerSecond.
' The travel limits are controlled with a single flapper switch
' flapped at 2 positions Reverse and StopatTop
' 2. Positioner motor is a Parallax Futaba limitless servo.
' It has a PULSOUT dead band (756 TO 760 x2usec) 1512 to 1520 .
' 'ditto' has 2 flapper switches :PositionerAtEnd and PositionerBack
' 3. Positioner Arm motor is a Parallax Futaba servo.
' It rides on the Positioner cart and raises - lowers its arm
' to be the positioner car coupler grabber
' 4. Brake is an old Futaba servo.
' It is under the track and raises its arm
' to hold the car in the correct position.
' 5. Positioner other side incoming brake
' hitec retract servo 1430 : 715 is brake on ... 1440:720 is open

' Servos, LEDs, PhotoResist, Buttons, Switches are connected to 3-pin posts
' and connected from board to item by a 3 wire servo extension

' The red green LEDs will be flashing when system is powered up
' and a reset cycle has been done. It will flash idiot lights
' when the system is waiting for a train: checking Buttons

' After Initialization-Power on - the cars are placed in position
' Three Buttons on a Servo extension in a bashed Coalgon
StartButton PIN 15 'white-yel wire
'Just in case EmergencyStopBtn removed due to RESET button on Box
Position1CarBtn PIN 14 'red wire
'Used when done to dump last car(s) pulled onto dumper by other loco
RotateDumper1Btn PIN 13 'black wire

'Flapper switches for servo movement limits
'Positioner limits
PositionerAtEnd PIN 12 'black wire Positioner Near Dumper
'A B hehehe - this will haunt me right ?
PositionerBack PIN 11 'red wire Positioner at back
'1 switch-sensor used to Reverse and Stop at Top Dumper
DumperSensor PIN 10 ' white wire

'not really coded - just flashing lights
GreenLight PIN 9 'yel-white wire
'No error detection AFAIK so just for funsies
RedLight PIN 8 'red wire , black of set is ground

'Photo diode light detector
CheckLight PIN 7 'its own wire set, only using 2 wires
Siren PIN 6 'on board only- sounds at end of cycle
'IR detector for a hand held TV remote control IAW Three Buttons
IRDetector PIN 5 'its own wire set- Currently ON BOARD
'uses all 3 White is Signal,R-,B+ ---
'it Matches the Detector inputs S-+ NOT the Servo S+-

'Additional Servo Positioner Retract servo-
'but not needed if the Positioner actually Works ...
'used to Stop the incoming train cars at correct location
RetractServo PIN 4
'4 Servos on Pins: 12 :Positioner, 13: Arm, 14 : Brake, 15 : Dumper
PositionerServo PIN 3
ArmServo PIN 2
BrakeServo PIN 1
DumperServo PIN 0


'THE PLOT
'A Unit train arrives and the Power de-couples from front
' and runs around dumper to wait for its train to be dumped.
' The train is Pushed to Positioner by Industry Power.
'The Positioner is near dumper, Arm and the Brake are Down.
'the Start Button is Pressed
'Position1Car: Push 1 car forward and Brake it,
'Check if more cars (Light detector) {it is needed here the first time...}
'then go into the lOOP
'lOOP:
'Push 1 car forward and Brake it, the car before is Now in dumper
'Rotate Dumper
'Check if more cars (Light detector )
'Repeat LOOP until Detector Exits the Loop : no more cars
'Done: Retract brake and raise Positioner a LITTLE bit
' as it has GRABBED the Coupler
' Positioner Near Dumper and the Arm raised a LITTLE
'Thus at end 1 car on ready-track , 1 car On dumper : IS dumped
'Pull Last car through-onto Dumper from other end with Power locomotive.
' Then Dump Last car Using the 3rd Push button to activate Dumper

'global variables Byte : Value can be 0 to 255
counter VAR Word ' Value can be 0 to 65535
positionerSpeed VAR Byte
positionerSpeed=20 'Set here to change all places easier
positionerStop VAR Word
positionerStop=758 'center of dead band
brakeSet VAR Word
'/////////////////////////////////////////////////////
'Brake ups and downs
'1000 is Topped OUT : 500 : it hits the track above
'1050 is just clear of Top Out : 525
'1130 '1161 it Lifts the wheels from the track 580 or 581 582
'1136 ...1140 is Good and Tight
'567 '1140 : 570 '1164 : brakeSet=582
'1145 '1166 BrakeSet is just before : 583
'1150 ...is caught but...
'1200 is caught but way loose 600
'1500 is Below track : 750 normal centered
'1800 is bottom out:900
'Using HPP-21
'the SERVO CHANGES ??? 582 start, now 572
'1130 565 1134 567 1140 570 glitching 1144 572 SET 1150 575:loose
'in code ... 572 is still loose 565 IS OFF the rail..but o so sweetly 2009dec28
'/////////////////////////////////////////////////////
brakeSet= 565 '565 567 570 572 582 583 'Brake holds the axle Tight

'Photo resistor detection of train cars to dump
carShadow VAR Word
' >1600 is a car <50 is no car , THIS is middle low
carShadow=500
time VAR Word 'IF (time < CarShadow) THEN NoMoreCars=1
noMoreCars VAR Bit

'for Sound .. some of the other variables are "reused"
duration VAR Word
frequency VAR Word
noteLetter VAR Byte

'IR detector to receive commands from a SONY DVD remote
irPulse VAR Word(4) '12 as noted in demo- only 4or5 are needed
irCounter VAR Nib '- i am out of ram
irMessage VAR Byte ' i sometimes get false numbers...
' -----[ Constants ]-------------------------------------------------------
ActiveHigh CON 1 ' Used to set PULSIN commands for IR
ActiveLow CON 0 ' to detect +/- pulses.

'/////////////////////////////////////////////////////
'/////////////////////////////////////////////////////
'THE PROGRAM
'/////////////////////////////////////////////////////
'/////////////////////////////////////////////////////
Initialize:
'All pins are input at startup, PULSOUT takes care of flipping Output by itself
'The LOW command sets the specified pin to 0 (a 0 volt level)
' and then sets its mode to output.
' LOW6 is same as OUT6 = 0 DIR6 = 1
LOW RetractServo
LOW PositionerServo
LOW ArmServo
LOW BrakeServo
LOW DumperServo
'the photo resistor will be High 6 ONLY while running AFAIK
LOW CheckLight

DEBUG "Program started",CR 'Print to computer screen
DO 'Do a MAIN LOOP Responding to the 3 Buttons ...
'or the IR remote...
'Check Buttons at Start AND while Looping ...
'Flash some LEDs GreenLight PIN 9 , RedLight PIN 8
'this could be check- red- check- green- and save code space?
'this is GR GR GR GRR
'2 µs x 5000= 10 000 =10msec light ?? pause
'go check buttons rather than pause to 'seem' interactive
GOSUB Check_Buttons 'PAUSE 100
PULSOUT GreenLight,6500 ' ON Green LED
GOSUB Check_Buttons 'PAUSE 200 '200msec OFF
PULSOUT RedLight,5000 'red
GOSUB Check_Buttons 'PAUSE 100
PULSOUT GreenLight,6500 ' ON
GOSUB Check_Buttons 'PAUSE 200 '200msec OFF
PULSOUT RedLight,5000 'red
GOSUB Check_Buttons 'PAUSE 100
PULSOUT GreenLight,6500 ' ON Green LED
GOSUB Check_Buttons 'PAUSE 200 '200msec OFF
PULSOUT RedLight,5000 'red
GOSUB Check_Buttons 'PAUSE 100
PULSOUT GreenLight,6500 'four flashes ident this program version
GOSUB Check_Buttons 'PAUSE 200
PULSOUT RedLight,5000 'red
GOSUB Check_Buttons
PULSOUT RedLight,5000
'MAIN LOOP ENDS here
LOOP 'YES it will run Forever .. UNTIL IR Remote 0: END is pressed
END 'so this end is redundant and never used, wasting program space
'as long as the code is correct :)
'/////////////////////////////////////////////////////
'/////////////////////////////////////////////////////
'The Program Ends
'/////////////////////////////////////////////////////
'/////////////////////////////////////////////////////


'/////////////////////////////////////////////////////
'Check_Buttons:
'/////////////////////////////////////////////////////
Check_Buttons: '50
'time = 0 ' this is used and reused and gets trashed... so set+reset
FOR counter = 1 TO 10 'check a lot between the LEDs being ON
'Start and Run the Sequence .. StartDumpRun
IF (StartButton=1) THEN GOSUB Start_Dump_Run
'Emergency Stop OBE so changed to Position1CarBtn
IF (Position1CarBtn=1) THEN GOSUB Position_1Car
'Dumper
IF (RotateDumper1Btn=1) THEN GOSUB Rotate_Dumper
'IR TV control similar to 3 Buttons
'...except have 10 or more commands! BUT NO RESET btn - YET-
'Need a Pin available to send a LOW to Pin 22
'Catch 22 The End button on Box is not needed as there is a
'hardwired reset btn- But BOE Reset is NOT hardwired NOR very accessable
' Wait for IR detector output Use For 1 to 15 to check
FOR irCounter = 1 TO 15 'to go low. If detected then process
IF IRDetector = 0 THEN GOSUB Do_IR_Processing
NEXT
NEXT

RETURN 'return from a Gosub
'/////////////////////////////////////////////////////
'/////////////////////////////////////////////////////
' More than 4 GOSUBS ...
'GOSUB Check_Buttons GOSUB Start_Dump_Run GOSUB Position_1Car -> Go subs
'GOSUB Do_IR_Processing
'Does it Crash or Reset to Program Start ... eheheehe

'Gosub_Reset:
' GOSUB Gosub_Reset
'RETURN 'this Crashes
'/////////////////////////////////////////////////////
'/////////////////////////////////////////////////////
Do_IR_Processing:
FOR irCounter = 0 TO 15
PULSIN IRDetector,ActiveLow,irPulse(0)
IF irPulse(0) > 900 THEN EXIT ' Exit FOR...NEXT after start
'ENDIF ' pulse is detected.
NEXT 'counter 0 to 15

'... Process IR Pulses
DO
PULSIN IRDetector,ActiveHigh,irPulse(0)
LOOP UNTIL (irPulse(0) > 1400) AND (irPulse(0) <> 0)
PULSIN IRDetector,ActiveLow,irPulse(0)
PULSIN IRDetector,ActiveLow,irPulse(1)
PULSIN IRDetector,ActiveLow,irPulse(2)
PULSIN IRDetector,ActiveLow,irPulse(3)

FOR irCounter = 0 TO 3
IF (irPulse(irCounter) < 450) THEN
irMessage.LOWBIT(irCounter) = 0
ELSE
irMessage.LOWBIT(irCounter) = 1
ENDIF
NEXT

DEBUG "Binary Value: ", BIN8 irMessage, CR
DEBUG "Decimal Value: ", DEC4 irMessage, CR ' DEC DEC4

'changing to gosub rather than 8 debugs saved 2 words of code !
'but that was 24 characters , should be 12 words saved ?
'Since there is no screen when actually RUNNING DEBUG is useless then...
'Seems a shame and waste of code space
'DEBUG in Code Sections, then Comment out the Debug lines :)
'o but then its not as much fun to run the program eheheheh
SELECT irMessage 'these are Case xxx+1 : the 1 button sends a 000 etc...
'1 2 3
CASE 000 : GOSUB Start_Dump_Run'DEBUG "000"
CASE 001 : GOSUB Position_1Car 'DEBUG "001"
CASE 002 : GOSUB Rotate_Dumper 'DEBUG "002"
'4 5 6
CASE 003 : GOSUB Set_Brake'DEBUG "003"
CASE 004 : GOSUB Lower_Brake'DEBUG "004"
CASE 005 : GOSUB Lane_Open'DEBUG "005"
'7 8 9
CASE 006 : GOSUB Lane_Closed'DEBUG "006"
CASE 007 : GOSUB Hyperspaced'DEBUG "007"
CASE 008 : GOSUB Make_Music'DEBUG "008"
'0
CASE 009 : GOTO Initialize'END 'DEBUG "009"
CASE ELSE : GOSUB Hyperspaced 'DEBUG "Nada" 'Case else is catch all else
ENDSELECT 'sometimes the message gets scrambled and seems to stall
'- so the CASE ELSE gives it somewhere to go ..
'it should just go to line after ENDSELECT
'.. but well you know
'DEBUG CR 'this is from the debug
RETURN

'/////////////////////////////////////////////////////
'/////////////////////////////////////////////////////


'/////////////////////////////////////////////////////
'StartDumpRun Starts here
'/////////////////////////////////////////////////////
Start_Dump_Run :
DEBUG "Positioner started",CR
'Put 1 car ahead of Loop
'GOSUB LaneOpen 'moved into Position_1Car.. does a LOT more action there
'rather than 1 at start - 1 at end.. tho actually only times needed :)
GOSUB Position_1Car

'Check Light detector
GOSUB Traffic_Light
IF (noMoreCars=1) THEN GOTO Initialize
'END 'Something is Really wrong-or just testing
DEBUG "Lets dump this Unit Train!",CR
DEBUG "till the Light is less than the Shadow",CR
DO
DEBUG "Positioner started",CR
GOSUB Position_1Car

'Could maybe use a light detector at Edge of dumper to check for
'correct placement- but setting it up so no false reads seems Difficult-
'It would depend on where the light is from
' as the detector would be at edge of car.
'the one in middle of track underneath a car is prone to false reads...

DEBUG "Rotating Dumper",CR
GOSUB Rotate_Dumper


GOSUB Traffic_Light
IF (noMoreCars=1) THEN EXIT 'See Light at end of tunnel
LOOP

DEBUG "All done! use Locomotive for positioning",CR
DEBUG "Dumper Control with the Dumper button",CR
'Raise arm a LITTLE bit to release Coupler so Power can move the cars
FOR counter= 932 TO 789 STEP 10 'gradual raise arm a LITTLE bit
PULSOUT ArmServo, counter 'hope they dont roll away...
PAUSE 20 'o they cant-arm is on this side
NEXT 'Power is at the Front of train again
GOSUB Lower_Brake
'GOSUB LaneClosed 'moved into Position_1Car
GOSUB Hyperspaced 'If i knew music i would play a tune
GOSUB Make_Music 'or play a cellphone ringtone-er not enough ram
GOTO Initialize 'trick to avoid the fact
RETURN 'that it took too many GOSUBs to get here
'/////////////////////////////////////////////////////
'Start_Dump_Run ENDS here
'/////////////////////////////////////////////////////


'/////////////////////////////////////////////////////
' LaneClosed LaneOpen Swing the retract servo arm
'/////////////////////////////////////////////////////
' hitec retract servo 1430 : 715 is brake on ... 1440:720 is open
'Even though it is a retract servo - it only moves in the direction
'as LONG as there is a pulse given to it.
'other servos SNAP to a location.. if it is far away
Lane_Closed:
FOR counter= 0 TO 60
PULSOUT RetractServo, 500'715-counter 'Swing arm into train path
PAUSE 20 '750 is normal centered
NEXT
RETURN
'/////////////////////////////////////////////////////
Lane_Open:
FOR counter= 0 TO 60
PULSOUT RetractServo, 1000'820+counter '720 Swing arm clear of track
PAUSE 20
NEXT
RETURN
'/////////////////////////////////////////////////////
'/////////////////////////////////////////////////////


'/////////////////////////////////////////////////////
'Hyperspaced: notification of completion
'/////////////////////////////////////////////////////
Hyperspaced: 'freq values ~4500-5000 play loudest
PAUSE 100 '? Octave 6+7 1046..4186
FOR duration = 15 TO 1 'STEP 1 Step 1 is Normal? change
FOR frequency = 3000 TO 4500 STEP 60 ' 2000 2500 20
FREQOUT Siren, duration, frequency
NEXT
NEXT
'FREQOUT PIN, Duration, Freq1 {, Freq2}
FREQOUT Siren, 2000, 4000, 4010 ' 2000 2010
RETURN
'/////////////////////////////////////////////////////
'/////////////////////////////////////////////////////


'/////////////////////////////////////////////////////
'Traffic_Light:
'/////////////////////////////////////////////////////
' HIGH 6 is same as OUT6 = 1 and DIR6 = 1
' Make the specified pin an output and high.
'OUTx = 0:Low 0 Volt =1: High:5Volt
'DIRx = 0: Pin is an Input = 1: Pin is an Output
' The HIGH command sets the specified pin to 1 (a +5 volt level)
' and then sets its mode to output.
' LOW 6 OUT6 = 0 DIR6 = 1
' The LOW command sets the specified pin to 0 (a 0 volt level)
' and then sets its mode to output.
'/////////////////////////////////////////////////////
Traffic_Light:
HIGH CheckLight ' CheckLight PIN 6
PAUSE 100 'charge capacitor
RCTIME CheckLight, 1, time
IF (time < carShadow) THEN noMoreCars=1
'DEBUG HOME, "time = ", DEC5 time," CarShadow = ",DEC5 CarShadow
DEBUG "Light ", DEC5 time," < ",DEC5 carShadow, " Shadow?", CR
'>1600 is a car <50 is no car , carShadow = 500
' Dark is big, Light is small time values
LOW CheckLight ' i dunno if i should,
'but i dont want current charging all the time
RETURN
'/////////////////////////////////////////////////////
'/////////////////////////////////////////////////////


'/////////////////////////////////////////////////////
'Position_1Car
'/////////////////////////////////////////////////////
'Brake should be Set, Arm Down
'Positioner ArmServo values
' 900 all UP, 1500 Car top, 1800 above coupler, 1830 grabs coupler
' 1840 ehh, 1850 ehehe, 1855 good little loose, 1860 ! little loose
' 1865 ok, 1870 tight, 1873 too Tight, 1880 Servo grumbling
'932=1864 1864=932=466
'/////////////////////////////////////////////////////
Position_1Car: 'Start at Gate, Move Back, Push Car Forward
'Check-Set brake just in case, Raise Arm, Move Positioner to Back,
PULSOUT BrakeServo, brakeSet
PAUSE 20
GOSUB Lane_Closed

'Raise arm SLOWLY ? to clear cars
FOR counter= 932 TO 466 STEP 10 '1864 to 932 gradually raise arm
PULSOUT ArmServo, counter
PAUSE 20
NEXT
PULSOUT ArmServo, 466 ' Arm UP 'dunno what step does to leftovers
PAUSE 20


FOR counter = 1 TO 5 'Move Positioner back past the switch
PULSOUT PositionerServo, positionerStop-positionerSpeed
PAUSE 20
NEXT
counter=5
DO 'Positioner - is CW is Reverse
counter=counter+1
PULSOUT PositionerServo, positionerStop-positionerSpeed
PAUSE 20
LOOP WHILE (PositionerBack=0) 'away from dumper
PULSOUT PositionerServo, positionerStop
PAUSE 20

DEBUG "Positioner Reverse: ", DEC counter, CR

'Lower Arm to Grab Coupler and push, Lower Brake so cars can move
FOR Counter= 466 TO 888 STEP 5'10 '932 to 1776 gradual Lower arm
PULSOUT ArmServo, Counter
PAUSE 20
NEXT
FOR Counter= 888 TO 932 'Very gradually Lower arm 1776 to 1864 Coupler bar
PULSOUT ArmServo, Counter
PAUSE 20
NEXT
PULSOUT ArmServo, 932 ' Arm grabbed coupler
PAUSE 20

GOSUB Lower_Brake 'so cars can move forward

GOSUB Lane_Open
'Move Positioner forward, Set Brake
'12 :Positioner + is CCW is Forward
FOR counter = 1 TO 5 'Move it past the switch
PULSOUT PositionerServo, positionerStop+positionerSpeed
PAUSE 20
NEXT
counter=5
DO
counter=counter+1
PULSOUT PositionerServo, positionerStop+positionerSpeed
PAUSE 20
LOOP WHILE (PositionerAtEnd=0) 'PositionerAtEnd
PULSOUT PositionerServo, positionerStop
PAUSE 20

DEBUG "Positioner Forward: ", DEC counter, CR

GOSUB Lane_Closed
GOSUB Set_Brake
RETURN
'/////////////////////////////////////////////////////
'/////////////////////////////////////////////////////


'/////////////////////////////////////////////////////
'Set_Brake:
'/////////////////////////////////////////////////////
Set_Brake:
'debug "Set Brake" 'commented out to reduce code size :(
FOR Counter= 750 TO brakeSet STEP 4 '10 gradual brake-raise arm to axle
PULSOUT BrakeServo, Counter '14 : Brake
PAUSE 20
NEXT
PULSOUT BrakeServo, brakeSet 'dunno what step does to leftovers
PAUSE 20
DEBUG "Brake Set" 'this and other used 6 bytes of code
RETURN ' from Sub SetBrake:

Lower_Brake:
'debug "Lower Brake"
FOR counter=brakeSet TO 750 STEP 10 'gradual release -- going down
PULSOUT BrakeServo, counter '14 : Brake
PAUSE 20
NEXT
PULSOUT BrakeServo, 750
PAUSE 20
DEBUG "Brake Lowered"
RETURN ' from Sub LowerBrake:
'/////////////////////////////////////////////////////
'/////////////////////////////////////////////////////


'/////////////////////////////////////////////////////
'/////////////////////////////////////////////////////
'RotateDumper
'/////////////////////////////////////////////////////
'Set Dumper to rotating (its Not a Position, it is the rotation Speed
'Check Switch-Sensor and Reverse when it is hit
'till it is hit again at top and Stopped
'changed from variables when room ran out
' dumperSpeed VAR Byte
' dumperSpeed=20 'Set here to change all places easier
' dumperStop VAR Word
' dumperStop=760 'center of dead band

Rotate_Dumper: '15 : Dumper
DEBUG "Dumper started", CR

FOR counter = 1 TO 5 'Move it past the switch - then keep going
PULSOUT DumperServo, 780 '760 + is CCW is Forward to Dump
PAUSE 20
'DEBUG "Dumper ",DEC DumperSensor ,CR 'used during actual de-bugging once
NEXT
counter=5
'The servo IS turning till Switch hit
DO 'DO:LOOP WHILE(?=0) is a trick
counter=counter+1
PULSOUT DumperServo, 780 '760 + is CCW is Forward to Dump
PAUSE 20
LOOP WHILE (DumperSensor=0)
PULSOUT DumperServo, 760 'Stop turning, so it does not spit gears reversing
PAUSE 20

DEBUG "Dumper Upsidedown: ", DEC counter, CR

DEBUG "Dumper returning", CR

FOR counter = 1 TO 5 'Move it past the switch
PULSOUT DumperServo, 740 '760 - is CW is Reverse to TOP
PAUSE 20
NEXT
counter=5
'The servo IS turning till Switch hit
DO
counter=counter+1
PULSOUT DumperServo, 740 '760 - is CW is Reverse to TOP
PAUSE 20
LOOP WHILE (DumperSensor=0)
PULSOUT DumperServo, 760 'Stop turning, we are at top again
PAUSE 20

DEBUG "Dumper Topside: ", DEC counter, CR 'to count turnaround loops
DEBUG "Dumper Done", CR 'totally useless but interesting
RETURN
'/////////////////////////////////////////////////////
'/////////////////////////////////////////////////////


'/////////////////////////////////////////////////////
'x VAR Bit ' Value can be 0 or 1
'y VAR Nib ' Value can be 0 to 15
'z VAR Byte ' Value can be 0 to 255
'zz VAR Word ' Value can be 0 to 65535
'/////////////////////////////////////////////////////
Make_Music: 'Music data is at TOP of program
' MMindex VAR Byte 'could use counter here too... uhm well NO
'counter 'time
'irCounter 'offset VAR Nib
' noteLetter VAR Byte 'irMessage got clobbered too much to use
'frequency 'noteFreq VAR Word
'duration 'noteDuration VAR Word
'MMindex VAR Nib
noteLetter = "a" 'If run again.. it is still a Q
time = 0 ' this is used and reused and gets trashed... so set+reset
irCounter=0
DO UNTIL noteLetter = "Q"
READ Notes + time, noteLetter
LOOKDOWN noteLetter, [ "A", "b", "B", "C", "d",
"D", "e", "E", "F", "g",
"G", "a", "P", "Q" ], irCounter
LOOKUP irCounter, [ 1760, 1865, 1976, 2093, 2217,
2349, 2489, 2637, 2794, 2960,
3136, 3322, 0, 0 ], frequency
READ Durations + time, duration
duration = WholeNote / duration
FREQOUT Siren, duration, frequency
time = time + 1
LOOP
RETURN
'/////////////////////////////////////////////////////
'/////////////////////////////////////////////////////



'/////////////////////////////////////////////////////
'NO MORE CODE :)
'/////////////////////////////////////////////////////
END 'just in double case, yes some more wasted code space :)

No comments:

Post a Comment