I was helping the O.P. with this project and was disappointed to see him give up after all the hard work he put into it. Perhaps he will look in on this forum and be re-energized based on what I am about to put forward. I really feel bad because we went away on holidays just as his project was nearing conclusion. I was intrigued by this project and would like to replicate it with a few differences. The O.P. was using an 18M2 project board based on my suggestion, in order to keep his costs down. I will be using a Picaxe 28X2 shield base and the instant robot shield (AXE408) that was used on another project. The O.P. also used a servo which he salvaged from an old RC car to rotate the turret. I will be using a GM3 gear motor because I have several lying around.
With that having been said, let’s review what we want this puppy to do. On power up we would like the turret to rotate back and forth while emitting a weird alien sound.
Upon pressing one of the buttons on the transmitter, the turret will go into a manual mode and drive in the direction assigned to the button (right or left). So, two of the buttons on the transmitter will drive the turret right or left as desired. A third button will be used to fire the gun which consists of flashing an LED and emitting an alien gun sound.
If no commands are received from the transmitter after a suitable delay (TBD) the turret will revert back to auto mode (rotate back and forth on its own).
I have worked out the code for each of these functions and tested them on the simulator. However, I am stumped how to integrate this all into one program. Can I use the fourth button on the transmitter to latch auto mode on and off? Is the use of interrupts appropriate here? If so I don’t have a clue since I never used them before. Or perhaps the case construct would be more appropriate? As I said, I’m stumped right now. Any suggestions would be appreciated as well as some code snippets to get me going in the right direction would be great! Also attached is an I/O table with pin assignments and labels.
Here is what I have done so far. The first code snippet lists all the declarations and is the code for automatic turret rotation.
Code:
'******************Auto Turret.bas****************
'Version 1.1
'AJZ/July 2015
'This is code for the Super Sentinel using the AXE401 shield base
'and the AXE408 instant robot shield
'The sentinel is controlled by a 4 channel transmitter/receiver
'When the Channel button on the transmitter is pressed,
'the corresponding channel on the receiver goes high
'Channel A drives the turret left
'Channel B drives the turret right
'the limit of rotation is determined by Hall effect sensors
'motor is GM3 or equivalent, 5 volt 3 RPM
'When the power is applied to the shield, the turret rotates back and forth
'In addition the sound of an alien recording is played during rotation
'===Constants===
symbol atad = 2000 'sets long pause interval
symbol abit = 500 'sets short pause interval
'===Variables===
Symbol Chan_A = pinA.0 'Assign receiver channel A to Port A.0
Symbol Chan_B = pinA.1 'Assign receiver channel B to Port A.1
Symbol Chan_C = pinA.2 'Assign receiver channel C to Port A.2
Symbol Chan_D = pinA.3 'Assign receiver channel D to Port A.3
Symbol Limit_L = pinB.3 'Assign left travel limit to Port B.3
Symbol Limit_R = pinB.4 'Assign right travel limit to Port B.4
symbol motcont = C.1 'motor ON/OFF
symbol motdir = C.0 'determines motor direction HI/LO
symbol LED = C.4 'fires LED
symbol beep = C.6 'makes "alien recording" noise
symbol chirp = C.7 'makes "strange alien" noise
symbol gun = B.2 'makes "alien machine gun" noise
'===Directives===
#com 3 'specify serial port
#picaxe 28X2 'specify processor
'#no_data 'save download time
'#terminal of 'disable terminal window
'============== Begin Main Program ==========================
dirsA = %00000000 'all inputs
dirsB = %11100111 'B.3 & B.4 are inputs
dirsC = %11111111 'all outputs
auto:
high beep 'activate "strange alien" sound
pause abit 'a momentary pulse activates
low beep 'a 30 second sound bite
if Limit_L = 1 and Limit_R = 1 then 'turret is out of the limits
gosub left 'rotate turret left
end if
If Limit_L = 0 then 'turret is in left limit
pause abit
gosub right 'rotate turret right
end if
If Limit_R = 0 then 'turret is in right limit
pause abit
gosub left 'rotate turret left
end if
goto auto
left:
high motcont : high motdir 'high C.1 & C.0 go left
do until Limit_L = 0 'until limit made
loop
low motcont 'low C.1 stops motor
pause abit
return
right:
high motcont : low motdir 'high C.1 low C.0 go right
do until Limit_R = 0 'until limit made
loop
low motcont 'stop motor
pause abit
return
This next code snippet is for manual rotation of the turret.
Code:
'============== Begin manual turret Program ==========================
dirsA = %00000000 'all inputs
dirsB = %11100111 'B.3 & B.4 are inputs
dirsC = %11111111 'all outputs
main:
if Chan_A = 1 then 'press "A" on xmtr
gosub manleft 'rotate turret left
end if
If Chan_B = 1 then 'press "B" on xmtr
gosub manright 'rotate turret right
end if
goto main
manleft:
high beep 'activate "alien recording" sound
pause abit 'a momentary pulse activates
low beep 'a 30 second sound bite
high motcont : high motdir 'high C.1 & C.0 go left
if Chan_A = 0 or Limit_L = 0 then 'button released or limit made
low motcont 'stop motor
end if
do until Limit_L = 0 'until limit made
loop
low motcont 'low C.1 stops motor
pause abit
return
manright:
high beep 'activate "alien recording" sound
pause abit 'a momentary pulse activates
low beep 'a 30 second sound bite
high motcont : low motdir 'high C.1 low C.0 go right
if Chan_B = 0 or Limit_R = 0 then 'button released or limit made
low motcont 'stop motor
end if
do until Limit_R = 0 'until limit made
loop
low motcont 'stop motor
pause abit
return
Finally this last code snippet fires the gun.
Code:
'============== Begin gun Program ==========================
dirsA = %00000000 'all inputs
dirsB = %11100111 'B.3 & B.4 are inputs
dirsC = %11111111 'all outputs
main:
If Chan_D = 1 then 'Chan A button is pressed on xmtr
gosub shoot 'execute shoot sub-routine
end if
goto main
shoot:
high gun 'take pin B.4 high
pause 100 'a short pulse plays entire sound bite
low gun 'take pin B.4 low
for b0 = 1 to 3 'entire sound bite is about 3 seconds
high LED 'so we flash the LED at 0.5 sec intervals
pause abit
low LED
pause abit
next b0
return 'finished firing sequence
View attachment Super Sentinel I-O.pdf