pneumatic code

Can anyone give me a sample code that connect a pneumatic to one of the buttons on the controller.

if(vexRT(Btn7D))
{
SensorValue(pneumatic)= 1;
}

What type of code are you looking for and for what platform? What do you want the pneumatics to do? Do you want them to fire then automatically come back or do you want them to stay up until you press another button? Do you use EasyC? I have a program I can give you that fires the pneumatics with the push of one button then brings it back with the push of a different button. I use EasyC though so it may not work if you have RobotC? But I can show you it if you need.

Owen forgot the release part:

if(vexRT[myButton]) SensorBoolean[mySolenoid] = true;
else SensorBoolean[mySolenoid] = false;

If you’re looking for a button to trigger a change (i.e. Push/Release to flip the switch), I can copy the code from last year.

I use Robot C. Yes the dual acting so press once to move then press again to retract.
thanks to Owen. That looks like the code i know, but for some reason it doesnt work. Whats weird is it is in the correct port. The button its self works. Any ideas what’s wrong?

Can you explain what your code means. I’m clueless

You can just do


SensorValue[s_piston] = vexRT[Btn8D];

EDIT: Just saw that you wanted a toggle. Let me write a toggle code.

EDIT 2:


boolean actuate = false;
while(true) {
if(vexRT[Btn8D] != actuate) {
actuate = !actuate;
if(actuate) SensorValue[s_piston] = !SensorValue[s_piston];
}
}

Our team might be using pneumatics in the future and we use EasyC. Could you tell me how your code works?

For sure. I will post the code in a few minutes. Computer is booting up but at the rate it’s going… It’ll be a while. But basically, we push one button, pneumatics fire. We push another, the pneumatics are released and fall back. Very simple code and we use single acting so its nice having that reverse action just in programming.

OK finally got the computer up. Here is our code:

#include "Main.h"

void OperatorControl ( unsigned long ulTime )
{
      int ClawOpenButton; 
      int ClawClosedButton; 

      while ( 1 )
      {
            ClawOpenButton = GetJoystickDigital ( 1 , 5 , 1 ) ;
            ClawClosedButton = GetJoystickDigital ( 1 , 5 , 2 ) ;
            if ( ClawOpenButton == 1 )
            {
                  SetDigitalOutput ( 4 , 1 ) ;
                  SetDigitalOutput ( 5 , 1 ) ;
            }
            if ( ClawClosedButton == 1 )
            {
                  SetDigitalOutput ( 4 , 0 ) ;
                  SetDigitalOutput ( 5 , 0 ) ;
            }
      }
}

Thanks! Very much appreciated!

iAndroidOS’s code works the same.

If you want something a little bit more verbose, I’ve used the following code below during Sack Attack:


bool TrayCurrent = false; // TRUE is down, FALSE is up. This boolean allows the Tray and the AutoTray to interact

void Tray(bool setting) {
	switch (setting) {
	case true:
		SensorValue[solenoid] = true;
		TrayCurrent = true;
		writeDebugStreamLine("Tray down");
		break;

	case false:
		SensorValue[solenoid] = false;
		TrayCurrent = false;
		writeDebugStreamLine("tray up");
		break;
	}
}

void AutoTray() {
	switch (TrayCurrent) {
	case true:
		SensorValue[solenoid] = false;
		TrayCurrent = false;
		writeDebugStreamLine("tray up");
		break;
	case false:
		SensorValue[solenoid] = true;
		TrayCurrent = true;
		writeDebugStreamLine("Tray down");
		break;
	}
}
}

During operator control, you would would create a boolean before the infinite loop runs that would detect whether or not the button press is “new”


bool TrayPressed= false; 

Then inside, your operator control:

if (vexRT[Btn5U] == true && TrayPressed == false) { AutoTray(); TrayPressed = true; }
		else if (vexRT[Btn5U] == false) { TrayPressed = false; }

If you would want to manually change the piston position, for a macro or during autonomous you can call the Tray(bool setting function):


 Tray(false); // sets the solenoid to the LOW position 

I can explain any of the code you’d like, just feel free to ask.

You will likely want to rename the functions, as well…

You said you wanted basically a toggle. You press a button, the piston extends, press it again, and it retracts, right?


boolean actuate = false;
while(true) {
if(vexRT[Btn8D] != actuate) {
actuate = !actuate;
if(actuate) SensorValue[s_piston] = !SensorValue[s_piston];
}
}

The way this code works is:
[LIST=1]
*]You have a variable called “actuate”. That variable will be changed back and forth so you toggle only when you press down on the button and not when it’s held down.
*]You check to see if the state of the button is not equal to that variable
*]You toggle the variable
*]If actuate is toggled to true, then toggle the piston.
[/LIST]

I just realized it may be tough to complete my program if someone is unfamiliar with EasyC. I’m not a pro programmer and had no idea what I was doing at the beginning of the year so I know something like this would have helped me. So, I decided to write it all out to help anyone who is new to programming and wants to do pneumatics in EasyC. So… I apologize for the length of this post but here goes.

So to do my code, we use variables. Variables are just something you can assign a number to and are just a place holder… I think… As I said, I’m not a programmer so that has been true from my experience but I could be wrong. Anyways,to actually make the code, you make to variables by double-clicking the variables square at the top of the code section. You type int in the “type” section then whatever you want to name your variable in the “name” section. I did ClawOpenButton and ClawClosedButton.

You then can close this window and begin writing the code. You need a while(1) statement so if you don’t already have a “while” on your screen in your code, go to the left side of the screen and hit the “Program Flow” drop down menu. Drag and drop a “While Loop” function block into your programming at the top and just type 1 in the place when it prompts you to. Anything else I tell you to place should be in the parenthesis the “While” statement makes.

After this, you need to define your pneumatics. So first, drop the “Joystick” menu down. Drag a “Get Joystick Digital” into the while statement and define what you want it to be on the controller. Then when it says retrieve to, say one of the variables. Repeat this for the other variable but change which button you press (1 or 2 for me).

Now, drag an “If” from the “Program Flow” section. Click the “Add Variable” drop down menu and click ClawOpenButton. Then go to the “Add Operator” and add “==”. Finally, add a 1 after and hit OK.

Now, drop down the “Outputs” tab down and drag a “Digital Output” into the “If” you just made. Select the port one of the actuators is in and put 1 for the set. Press OK then repeat this for the other actuators you will be using if you plan on using more than one. Just change the port though. Leave the 1 the same.

Drag a new “If” in and put it under the parenthesis of the “If” that you just completed. However, make it ClawClosedButton == 1. Then do the two “Get Joystick Digital” things again with the same ports as the first “If” but change the 1s to 0s.

So… sorry about the huge message :o. But I really struggled to find all of this the first time so I hope it helps someone out. Cheers.

Sorry, the explanation was great and all but it’s way over my head:p
Are you sure your thinking Robot C not C ++

Guess I will throw my hat in here as well.

The great thing about digital outputs is that you can read back the state they are in, this makes toggling them easy.

#pragma config(Sensor, dgtl1,  pnuematics,     sensorDigitalOut)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

// Pneumatics toggle demo
// James Pearman 2014.
// This is free code :)
task main()
{
    while(1)
        {
        // Button 8U toggles pneumatics output
        if( vexRT Btn8U ] ) {
            SensorValue pnuematics ] = 1 - SensorValue pnuematics ];
            // wait for button release
            while( vexRT Btn8U ] )
                wait1Msec(10);
            }
     
        // Don't hog the cpu
        wait1Msec(25);
        }
}

The code


SensorValue pnuematics ] = 1 - SensorValue pnuematics ];

subtracts the current value of the output (0 or 1) from 1 and has the effect of toggling that output.

That is far less complex than my method of toggling. xD I guess this is better for what CyberFalcon needs.

Not necessarily better, just different. It’s good to see different ways of achieving the same thing.

Coach(learning/still in training) here, being nosy and peeking into the thread. Thank you to all for your input. Your help is greatly appreciated. Think we found the problem, we tested the various buttons, they work, tested the program, it worked. We think it is the angle at which the pneumatic is at.

With everyone’s help we have learned more, some advice is in code that is above our current knowledge level. Thank you to those who have taken the time to simplify for us and explain.

All is much appreciated!

Would this be the same code if you are using a double acting cylinder vs single? Im trying this code with a double and while the open is strong, the close is very weak, making me think its a programing issue
-thanks