I think it would help to produce a clear specification of what is needed and to start with it seems to split between two parts; the primary part which flings fish and a secondary part, an alarm which sounds when a fish hasn't been flung. We can deal with the first without worrying about the second, bolt that on later.
There is a three way switch, a speed pot and a motor. That motor is constant speed, only on or off, so no need for motor speed control; the speed pot controls the rate of delivery not the speed of motor.
Switch position 1: Motor stopped. Easy.
Switch position 2: Motor always on. Also easy.
Switch position 3: The more complicated part. Motor runs until a fish has been flung. Once flung the motor stops for a period set by the pot, then starts again. An additional requirement will probably be that it is responsive to pot changes; if set for a minute then switched to 10 seconds one does not want to wait for the rest of that minute to pass before it changes to a 10 second interval.
That gives us a basic framework for a single task, linear program which could be as follows -
The one thing missing in that is there is nothing which sets the 'fishFlung' variable. An interrupt when the opto detector activates as a fish passes through the beam and sets 'fishFlung=1' will solve that.
To add an alarm one needs to add some elapsed timer counting when the motor is on and we are waiting for the fish to be flung, something like ...
All timing is done with millisecond units and 16-bit word variables hold time as millisecond values. That limits things to 65 seconds which is okay here.
There is a three way switch, a speed pot and a motor. That motor is constant speed, only on or off, so no need for motor speed control; the speed pot controls the rate of delivery not the speed of motor.
Switch position 1: Motor stopped. Easy.
Switch position 2: Motor always on. Also easy.
Switch position 3: The more complicated part. Motor runs until a fish has been flung. Once flung the motor stops for a period set by the pot, then starts again. An additional requirement will probably be that it is responsive to pot changes; if set for a minute then switched to 10 seconds one does not want to wait for the rest of that minute to pass before it changes to a 10 second interval.
That gives us a basic framework for a single task, linear program which could be as follows -
Code:
MainLoop:
Do
Gosub DetermineSwitchPosition
If switchPosition = 1 Then Stopped
If switchPosition = 2 Then AlwaysOn
If switchPosition = 3 Then IntervalTiming
Loop
Stopped:
Gosub TurnMotorOff
Do
Gosub DetermineSwitchPosition
If switchPosition <> 1 Then MainLoop
Loop
AlwaysOn:
Gosub TurnMotorOn
Do
Gosub DetermineSwitchPosition
If switchPosition <> 2 Then MainLoop
Loop
IntervalTiming:
fishFlung = 0
Gosub TurnMotorOn
Do
Gosub DetermineSwitchPosition
If switchPosition <> 3 Then MainLoop
Loop Until fishFlung <> 0
Gosub TurnMotorOff
elapsedTime = 0
Do
Gosub DetermineSwitchPosition
If switchPosition <> 3 Then MainLoop
Pause 10
elapsedTime = elapsedTime + 10
Gosub DeterminePotTime
Loop Until elapsedTime >= potTime
Goto IntervalTiming
TurnMotorOn:
; Some code here
Return
TurnMotorOff
; Some code here
Return
DetermineSwitchPosition:
; Some code here which sets 'switchPosition' variable
Return
DeterminePotTime:
; Return potTime as 1000ms to 60000ms
Symbol MAX_SECONDS = 60
Symbol MIN_SECONDS = 1
ReadAdc POT_PIN, potTime
potTime = MAX_SECONDS - MIN_SECONDS * potTime / 255 + MIN_SECONDS * 1000
Return
To add an alarm one needs to add some elapsed timer counting when the motor is on and we are waiting for the fish to be flung, something like ...
Code:
IntervalTiming:
fishFlung = 0
[b]elapsedTime = 0[/b]
Gosub TurnMotorOn
Do
Gosub DeterineSwitchPosition
If switchPosition <> 3 Then MainLoop
[b]Pause 10
elapsedTime = elapsedTime + 10
If elapsedTime >= 60000 Then SoundAlarm[/b]
Loop Until fishFlung <> 0