Programming Pneumatics

Hey guys I am trying to program pneumatics for my robot using EasyC V4 and I am having some trouble.
I am using a double acting piston and I want it to be programmed so that when I hit the Channel 5 Up button it goes in until I hit the button again.
Right now I have this:

While (1 == 1)
{
button_pressed = GetJoyStickDigital ( 1, 5, 2 )
if ( button_pressed == 1 )
{
SetDigitalOutput ( 10, 1 )
}
if ( button_pressed == 0 )
{

SetDigitalOutput ( 10, 0 )
}

When I download this code into the brain and I press the button the piston only retracts when the button is pressed. I want the piston to stay retracted until I hit the button again.
Please Help

Something like this should work.

#include "Main.h"

void main ( void )
{
      int button_is_pressed = 0; 
      int output_state = 0; 
      int button_pressed; 

      while ( 1 )
      {
            button_pressed = GetJoystickDigital ( 1 , 5 , 2 ) ;
            if ( button_pressed == 1 )
            {
                  if ( button_is_pressed == 0 )
                  {
                        output_state = 1 - output_state ;
                        SetDigitalOutput ( 10 , output_state ) ;
                  }
                  button_is_pressed = 1 ;
            }
            else
            {
                  button_is_pressed = 0 ;
            }
      }
}

You need to change the state of the digital output every time you see a 0 to 1 transition on the button, but only do it once until the button is released again. Hopefully that code is correct, I don’t really use EasyC.

I am trying this and I am a bit confused when you say:

                    output_state = 1 - output_state ;

Is this a function?
Also the three things after the void are variables right?

Explore Devin Bailey

would the program look like this?
I think this is right but I am unsure. I will test this tomorrow when I get to school.

No, it’s just a quick way of converting a 0 into a 1, or a 1 into a 0. If output_state is 0 then 1 - 0 is 1. If output_state is 1 then 1 - 1 is 0.

Yes, they were defined as variables.

Pretty much, however, “==” is comparison not assignment. So replace the lines like.

button_is_pressed == 1;

with

button_is_pressed = 1;

just need to use one “=”

I did not test the code so what’s important to understand is the principle behind it. You want to detect the button press and change the state of the output. You only want to do this once until the button is released. button_is_pressed is a variable that is used to store the fact that the button was pressed the last time around the while loop. You could also replace the weird output_state line I had used with…

if( output_state == 0 )
    {
    output_state = 1;
    SetDigitalOutput ( 10 , 1) ;
    }
else
    {
    output_state = 0;
    SetOutputState( 10, 0 );
    }

There may be other ways to do this, as I said I don’t really use EasyC that much.

ok this helps :slight_smile: I will test this tomorrow at school.
Also do you know how to program the controller so that the arm raises to a certain position at the push of a button?
Thank you very much

Yes, but you need to try yourself first and let us know how you tried to do it and what did or did not work.

A hint, you may need a sensor like a potentiometer or encoder.

is there not a joystick to digital output block? or something like that (we recently switched to robotC so i cant remember its actual name)

EasyCv4 has an joystick function block called
JoysticktoDigitalOutput
and
JoysticktoDigitalLatch

The Latch version may do what you want.

Ok I will try to program it at school today and I will let you know how it goes. (I will be using a quad encoder).

ok I am trying to program it so that I can use 2 pistons and I copied the program you showed me but it isn’t working. It downloads without problem but when I got to press the buttons nothing happens.

I have attached a picture of the program.
program picture.pdf (114 KB)

Look closely at the code, it’s not quite what I posted. If the while statement is to be considered line 1, then line 6 is incorrect, should be “left_button_is_pressed”. Same with line 18 (but obviously right button).

ok i am stuck right now. this is my code.
Doc1.pdf (109 KB)

Ok, I’m trying to help but you are not reading the example and typing it in correctly. Perhaps I should have not used names which were so similar, my fault. In an effort to get you over this hurdle, I have entered it for you, I can not test fully but it should work. This was created using EasyC 4.0.2.7 but would I assume work on earlier versions.

You should also check out JoysticktoDigitalLatch as jgraber mentioned. Not sure but it may also do the job.
button_test.zip (2.11 KB)

It does, I just tried it for you. Interestingly, it does not appear in the help system.

Ok thanks! I got the code working and I programmed it so that with the press of 1 button 2 pistons work the same. Thank you for the help.
Right now I am working on the other program I told you about and I just don’t know where to start.
I want to make it so that when the Channel 6 Up button is pressed once the lift will raise to a certain position.

I’m glad you have made some progress.

To be able to help with the lift would require more detailed information on your design and what sensors you have incorporated (or could add to the design).

It’s clear that you are just beginning to learn programming. Learning to program like everything else takes time and effort, however, one of the things that my colleagues constantly remind me of is that software can easily be changed. Therefore, try out the different functions that EasyC provides, play with the different sensors you have available to see what they do, start with simple code and gradually add more until you are comfortable with the toolset EasyC provides. There are lots of posts on this forum talking about how to achieve the same goal as you have, controlling motors to move something to a position without user intervention.

In a few days post back with a plan. Tell us what you have tried, what worked and what you don’t understand. When you have a plan I will be happy to continue to help. Good Luck.

ok thank you very much

Ok I have added a quad encoder to the drive shaft of our lift and I have a program but I am kind of lost and confused.

If you could point me in the right direction it would be greatly appreciated.
Doc1.pdf (107 KB)