Here’s what we did:
As I understand it, solonoids can’t have constant current applied to ‘em. Now, we didn’t want to use the joystick controls since you can accidentaly hit them in the wrong time. And since we were too cool for momentary switches, we used toggle switches. So basically our code does it so all that you have to do is flip the switch and the solonoid is pulsed out or in and then off (this is just for double solonoids, btw). This way we can have a cool-lookin’ flip switch but no harm done to the solonoids.
This is our code - I’m feeling a bit blue, so lets do something cool and help people out =)
if (R_G_Piston_p3_s4 = R_G_Piston_Last) then NotReset:
if (R_G_Piston_Counter <= DELAY) then NotResetA:
R_G_Piston_Last = R_G_Piston_p3_s4
R_G_Piston_Counter = 0
relay4_fwd = 0
relay4_rev = 0
NotResetA:
NotReset:
if (R_G_Piston_p3_s4 = R_G_Piston_Last) then NotToggle:
if (R_G_Piston_Counter > Delay) then NotToggleA:
R_G_Piston_Counter = R_G_Piston_Counter + 1 + delta_t
if R_G_Piston_p3_s4 = 1 then Next8a:
relay4_fwd = 0
relay4_rev = 1
next8a:
if R_G_Piston_p3_s4 = 0 then Next8b:
relay4_fwd = 1
relay4_rev = 0
Next8b:
NotToggleA:
NotToggle:
if (R_G_Piston_p3_s4 <> R_G_Piston_Last) then NotNeutral:
R_G_Piston_Counter = 0
R_G_Piston_Last = R_G_Piston_p3_s4
relay4_fwd = 0
relay4_rev = 0
NotNeutral:
Okay, so what does all that garbage mean? Good question. I don’t know =D
Let me start by telling you what everything is
R_G_Piston_p3_s4 - The piston switch (p3_s4 = port 3, switch 4, is either a 0 or a 1)
R_G_Piston_Last - The SWITCH’s position in the last cycle (either a 0 or a 1)
R_G_Piston_Counter - Basically a cycle-counter
Delay - The amount of cycles you want the relay (solonoid) to be pulsed before switching off - i.e. a few seconds, if even that
delta_t - since this is a cycle-driven loop, delta_t calculates how many packets (and so cycles) were missed and adds approproatly. Serves no purpose other than making timing more exact, but since its no longer than 1, 2 seconds, timing really isn’t important, so this is pretty much useless. But again, this is the hardcore way of solving your problem. If you wanna use delta_t, search for the explanation on this board. Otherwise, just leave it out.
Okay, so now how does that code transform into logic, you ask? Well heres the theory behind it.
- You flip the switch
- It recognizes the switch is in a different position it was last cycle
2a. If the switch is 0, relay is set to go fwd, if the switch is 1, relay go rev
2b. Adds one to the counter variable, leaves the Piston_Last variable as it is
- Repeat step 2, always adding one to the counter
- Everytime step 2 is repeated, it checks to see if the counter is greater than the delay. If so, then x number of cycles have passed and time to turn the relay off
- Once the counter is greater than the delay variable, the code sets Piston_Counter = 0 (resets it), sets relayx_fwd = 0 & relayx_rev = 0 (puts the relay into neutral), and sets Piston_Last = the current switch position
- Now when the loop repeats (new cycle), it sees that the last position is = to the current position, so it does nothing, until you flip the switch
Thats the logic, now apply the logic to the code. Yes the code is dirty alias-wise. No we don’t care =D Took my friend and I a few days to get this working all nice and happy, and yes, you could simplify it by using two momentary toggle switches, but we’re hardcore. And, best of all, now we can add those military-style switches to make uber-hardcore piston controls =)