Hi guys, let me start by saying that I am very new to micro-controllers and programming. I have no formal training whatsoever. I have literally just started playing around with picaxe a few weeks ago. I got into it to see if I could build a circuit to control certain lighting effects on a model that I am building. The model is a 1/350 scale version of the USS Enterprise as seen in Star Trek The Motion picture. The lighting effects I'm working on are for the deflector dish and photon torpedos.
I'm using two momentary switches (on pins C.0 and C.1), one blue/yellow bi-colored LED (yellow on pwm C.2, blue on pwm C.3.) two red/white bi-colored LEDs (left red on pwm C.5, right red on pwm B.1)
By using pulse width modulation, the deflector dish will ramp up in yellow at the first button push, then at the second button push, the yellow LED fades off and the blue LED ramps up, then at the third button push the blue LED fades off.
The photon torpedo simulation alternates left and right, push the button once and the left red LED ramps up, then a quick 50ms burst from the white LED, then the red LED fades off, push the button again, the sequence repeats on the right red/white bi-LED.
As I have NO programming skills of my own, I have basically been operating from deductive reasoning using snippets of code I have found here and there, both in the picaxe manuals and various forums and tutorials. I am using the picaxe 20M2. The code I have managed to cobble together works just fine except for one thing, if I try to fire a photon torpedo while the deflector dish is in the process of ramping up or fading off, the LED will not respond to my switch until the PWM for the deflector dish has ceased, OR the deflector dish LED will not respond while a torpedo is being fired.
I was under the impression the M2 series could do parallel tasking. Is there something that can be done to easily rectify this, or will my code have to be totally revamped, or will I just have to live with it?
Don't laugh at my code guys (and gals). Like I said, I am a total rookie.
I'm using two momentary switches (on pins C.0 and C.1), one blue/yellow bi-colored LED (yellow on pwm C.2, blue on pwm C.3.) two red/white bi-colored LEDs (left red on pwm C.5, right red on pwm B.1)
By using pulse width modulation, the deflector dish will ramp up in yellow at the first button push, then at the second button push, the yellow LED fades off and the blue LED ramps up, then at the third button push the blue LED fades off.
The photon torpedo simulation alternates left and right, push the button once and the left red LED ramps up, then a quick 50ms burst from the white LED, then the red LED fades off, push the button again, the sequence repeats on the right red/white bi-LED.
As I have NO programming skills of my own, I have basically been operating from deductive reasoning using snippets of code I have found here and there, both in the picaxe manuals and various forums and tutorials. I am using the picaxe 20M2. The code I have managed to cobble together works just fine except for one thing, if I try to fire a photon torpedo while the deflector dish is in the process of ramping up or fading off, the LED will not respond to my switch until the PWM for the deflector dish has ceased, OR the deflector dish LED will not respond while a torpedo is being fired.
I was under the impression the M2 series could do parallel tasking. Is there something that can be done to easily rectify this, or will my code have to be totally revamped, or will I just have to live with it?
Don't laugh at my code guys (and gals). Like I said, I am a total rookie.
Code:
init:
symbol buttonA=b0 'rename the b0 variable to buttonA (for the deflector dish control switch)
symbol buttonB=b1 'rename the b1 variable to buttonB (for torpedo launcher control switch)
let buttonA=0
let buttonB=0
main: if pinC.0 = 1 then deflector 'if input pin C.0 goes high, go to the "deflector" subroutine.
if pinC.1 = 1 then torpedo 'if input pin C.1 goes high, go to the "torpedo" subroutine.
goto main 'otherwise loop back to main
deflector:
pause 50 ' pause 50 milliseconds to debounce switch
let buttonA = buttonA + 1 'adds 1 to the variable, this counts the number of button presses
If buttonA=1 then
pwmout pwmdiv4, C.2, 99, 0 'start PWM on Port C Pin 2 @ 0%
for w1 = 0 to 400 ' pwmduty needs a (W)ord var, not a (B)yte var
pwmduty C.2, w1 'adjust PWM Duty from 0% to 100%
pause 2 ' increase the on time
next w1
elseif buttonA=2 then
for w1 = 400 to 0 step -1 'same as above, but reduce the ON time
pwmduty C.2, w1
pause 2
next w1
pwmout C.2, off
pwmout pwmdiv4, C.3, 99, 0 'start PWM on Port C Pin 3 @ 0%
for w1 = 0 to 400 ' pwmduty needs a (W)ord var, not a (B)yte var
pwmduty C.3, w1 'adjust PWM Duty from 0% to 100%
pause 2 ' increase the on time
next w1
elseif buttonA=3 then
for w1 = 400 to 0 step -1 'same as above, but reduce the ON time
pwmduty C.3, w1
pause 2
next w1
pwmout C.3, off
buttonA=0 ' reset counter to 0
endif 'end of the if statement
goto main 'loop back to main
torpedo:
pause 50 ' pause 50 milliseconds to debounce switch
let buttonB = buttonB + 1 'adds increments of 1 to the variable, this counts the number of button presses
If buttonB=1 then
pwmout pwmdiv4, C.5, 99, 0
for w1 = 0 to 200 ' pwmduty needs a (W)ord var, not a (B)yte var
pwmduty C.5, w1 'adjust PWM Duty from 0% to 100%
pause 2 ' increase the on time
next w1
high B.2 ' turn on white LED on output B.2
pause 50 ' pause 50 milliseconds
low B.2 ' turn off white LED on output B.2
for w1 = 200 to 0 step -1 'same as above, but reduce the ON time
pwmduty C.5, w1
pause 2
next w1
pwmduty C.5, 0
elseif buttonB=2 then
pwmout pwmdiv4, B.1, 99, 0
for w1 = 0 to 200 ' pwmduty needs a (W)ord var, not a (B)yte var
pwmduty B.1, w1 'adjust PWM Duty from 0% to 100%
pause 2 ' increase the on time
next w1
high B.3 ' fire photon torpedo
pause 50 ' 50 milliseconds burst of white light
low B.3 ' turn off white LED on output 4
for w1 = 200 to 0 step -1 'same as above, but reduce the ON time
pwmduty B.1, w1
pause 2
next w1
pwmduty B.1, 0
buttonB=0 'reset the counter
endif 'end the if statement
goto main ' loop back to main