Button1.pressed method

can someone please help me understand how to use the pressed method.

There an old simple example here.

5 Likes

Because code is always read from top to bottom, things like these always tend to confuse me at times.

I have a question in regards to the event:
Let’s say I have this within the while loop, with the variable “value” being a boolean (assumed value = true before the while loop is ran)

while(value){
p.ButtonL1.pressed( b1 );
}
//Additional code below

If value = true, then it would establish a connection between the controller button and the separate thread that would run the function correct?

But if value = false after a couple of seconds, and the code continues past the while loop towards the additional code, would the connection no longer be valid/exist since it’s outside parenthesis?

Also:
If we do

while(true){
p.ButtonL1.pressed( b1 );

//Code below the event that contains a nested while loop
}

If the system is running a line of code below the “p.ButtonL1.pressed( b1 );” line, and the button gets pressed, would the event still run, despite the system is not at the event’s initialization?

Connor, I believe your code will result in the formation of a small black hole.

0wap3cp4khm01-1

7 Likes

So basically speaking, the code creates a task that would run once if the controller button is pressed (but would continue to exist and wait if the controller is pressed another time)?

So putting it inside of a while loop would create a crapton of tasks which would either result in the cpu going “what the heck kill me” or “I give up on being your partner, goodnight”

3 Likes

haha, yes.

yes, an event (on V5) is a task waiting on a semaphore. When the event fires, the semaphore is made available and the task runs.

you would create multiple tasks all waiting for the same event, they would all run when the event triggers. We put a limit on the number of total events you can create so the V5 doesn’t go into that black hole.

7 Likes

I created a second example for the pressed event:

/*----------------------------------------------------------------------------*/
/*                                                                            */
/*    Module:       main.cpp                                                  */
/*    Author:       VEX                                                       */
/*    Created:      Thu Sep 26 2019                                           */
/*    Description:  Competition Template                                      */
/*                                                                            */
/*----------------------------------------------------------------------------*/

// ---- START VEXCODE CONFIGURED DEVICES ----
// Robot Configuration:
// [Name]               [Type]        [Port(s)]
// TestMotor            motor         1               
// Controller1          controller                    
// ---- END VEXCODE CONFIGURED DEVICES ----

#include "vex.h"

using namespace vex;

// A global instance of competition
competition Competition;

// define your global instances of motors and other devices here

/*---------------------------------------------------------------------------*/
/*                          Pre-Autonomous Functions                         */
/*                                                                           */
/*  You may want to perform some actions before the competition starts.      */
/*  Do them in the following function.  You must return from this function   */
/*  or the autonomous and usercontrol tasks will not be started.  This       */
/*  function is only called once after the V5 has been powered on and        */
/*  not every time that the robot is disabled.                               */
/*---------------------------------------------------------------------------*/

void pre_auton(void) {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // All activities that occur before the competition starts
  // Example: clearing encoders, setting servo positions, ...
}

/*---------------------------------------------------------------------------*/
/*                                                                           */
/*                              Autonomous Task                              */
/*                                                                           */
/*  This task is used to control your robot during the autonomous phase of   */
/*  a VEX Competition.                                                       */
/*                                                                           */
/*  You must modify the code to add your own robot specific commands here.   */
/*---------------------------------------------------------------------------*/

void autonomous(void) {
  // ..........................................................................
  // Insert autonomous user code here.
  // ..........................................................................
}

/*---------------------------------------------------------------------------*/
/*                                                                           */
/*                              User Control Task                            */
/*                                                                           */
/*  This task is used to control your robot during the user control phase of */
/*  a VEX Competition.                                                       */
/*                                                                           */
/*  You must modify the code to add your own robot specific commands here.   */
/*---------------------------------------------------------------------------*/

bool exitEvent = false;

void buttonL1EventFired(){

  while(exitEvent != true){

    //code
   vex::task::sleep(20); 
  }
}


void usercontrol(void) {
  // User control code here, inside the loop

  Controller1.ButtonL1.pressed(buttonL1EventFired);// This creates a task that would run the function within parenthesis once if the button is pressed

  while (1) {

    

    if(Controller1.ButtonL2.pressing()){
      exitEvent = true;
    }

    // This is the main execution loop for the user control program.
    // Each time through the loop your program should update motor + servo
    // values based on feedback from the joysticks.

    // ........................................................................
    // Insert user code here. This is where you use the joystick values to
    // update your motors, etc.
    // ........................................................................

    wait(20, msec); // Sleep the task for a short amount of time to
                    // prevent wasted resources.
  }
}

//
// Main will set up the competition functions and callbacks.
//
int main() {
  // Set up callbacks for autonomous and driver control periods.
  Competition.autonomous(autonomous);
  Competition.drivercontrol(usercontrol);

  // Run the pre-autonomous function.
  pre_auton();

  // Prevent main from exiting with an infinite loop.
  while (true) {
    wait(100, msec);
  }
}

Would this usage be correct?