Pneumatics Code!

Hi fellow VEX users!

I have been trying to program our pneumatics to open when one button is being pressed, and close when another one is being pressed. It is not working though. This is my team’s first time using pneumatics, so I am not even sure of any potential problems. Could anyone take a look at my code and see if you see anything? Thanks!!!

1 Like

What exactly happens when you try to run that code?

Can you share the full code?

This is all of the code that I have for the pneumatics. Is there something I need to add?

The robot does not do anything. A memory error comes up on the screen but I’m not sure if this impacts anything. The robot’s lack of response also leads me to believe it could be mechanical.

That sounds like a thread worth pursuing – could you copy and paste the entire contents of your main.cpp into a reply? Remember to include triple backticks (```) before and after your code, on their own lines, for formatting.

1 Like

You need a forever loop around your pneumatics program

What do you mean by this?

It’s the usual problem, you are using Brain.ThreeWirePort.A before Brain has been instantiated.

4 Likes

You might have to use digital_out instead of pneumatics. This is the code I use:

digital_out numa = digital_out(Brain.ThreeWirePort.A);

int main(){
  while(true){
    if(Controller.ButtonA.pressing()){
      numa.set(true);
    }
    else if(Controller.ButtonB.pressing()){
      numa.set(false);
    }
    wait(10, msec);
  }
}
2 Likes
/*                                                                            */
/*    Module:       main.cpp                                                  */
/*    Author:       Shaelin Murphy                                            */
/*    Created:      Mon Nov 15 2021                                           */
/*    Description:  V5 project                                                */
/*                                                                            */
/*----------------------------------------------------------------------------*/

// ---- START VEXCODE CONFIGURED DEVICES ----
// Robot Configuration:
// [Name]               [Type]        [Port(s)]
// expander             triport       1               
// ---- END VEXCODE CONFIGURED DEVICES ----

#include "vex.h"

using namespace vex;



vex::controller Controller1 = vex::controller();
vex::motor fRight = vex::motor(vex::PORT11);
vex::motor bRight = vex::motor(vex::PORT2);
vex::motor fLeft = vex::motor(vex::PORT3,true);
vex::motor bLeft = vex::motor(vex::PORT4,true);

//extern pneumatics numa;
pneumatics numa = pneumatics(Brain.ThreeWirePort.A);




int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  while(1==1){
    if (Controller1.ButtonA.pressing()){
    numa.close();
    }
   if (Controller1.ButtonB.pressing()){
    numa.open();
    }
   
  wait (20,msec);
  }
}

you can use either, they do the same thing, the pneumatics class just allows you to use close() and open() functions.

4 Likes

I usually have a forever loop so that the program can repeat functions and not end right after it was started.

Thank you for this! I just tried it, and as soon as I ran the code, the pneumatic opened without me doing anything. Then, when I pressed the buttons, it wouldn’t do anything. Do you know why this is happening?

I used the while(1==1) loop to try and achieve this. Do I need another loop somewhere else?

Follow what @jpearman says w/r/t where things are declared. Ideally, put all global declarations of Motors, Sensors, Brain, etc. in the same file (e.g. robot-config.cpp). I believe if you use the UI to define motors/sensors it does this automatically. I would guess you have the “expert” option ticked.

4 Likes

Should just be able to do this
forever () {

I don’t believe that using forever will work, it’s probably better to just use while(true).

2 Likes

Thank you! This helped! You will receive a shoutout in our engineering journal for sure

2 Likes

Ok thank you!!!

1 Like