The following EasyC code is loaded by Hubs for teams to use as default code.
A common team problem for teams that dont read the read_me will likely be
that motors plugged into motors ports 4,7,8 will keep running even with no Joystick movement. This is because these ports are intended for servos, and alternate between settings that do not include 0.
This is slightly SPA (Sense Plan Act) format, and uses code concepts:
- #define for named constants, like Joy1 and Buttons and Left/Right/Up/Down
- M[2…9] Motor value arrays.
- Analog potentiometer limit switches (new to BEST this year)
// ReadMe.txt
// BEST WarpXX (2012 Game)
// Default code, jg revision 7/16/2012
// Motor (control) Description
// Port (joystick or buttons)
// m2 (+j3) m2 and m9 are opposing analog pairs
// m3 (+j4)
// m4 (7/8 udlr : -127 default, -50, +50, +127)
// (suggest servo only) m4,m7 opp dig pairs
// m5 (+j1, limits per d1 and d2)
//
// m6 (+j2, limits per pot on a1)
// m7 (7/8udlr, four latched values: +127 default, +50, -50, -127) (suggest servo only) m4,m7 opp dig pairs
// m8 (6u, two values, -127 for released button and +127 for pressed button, was m7) (suggest servo only)
// m9 (-j3) m2 and m9 are opposing analog pairs
//// Main.h
#ifndef _main_h_
#define _main_h_
#include "API.h"
#include "UserInclude.h"
#define Joy1 1 //Joystick1
#define B6 6 //button 6
#define B7 7 //button 7
#define B8 8 //button 8
#define Down 1 //down button on 7,8
#define Up 2 //up button on 7,8
#define Left 3 //left button on 7,8
#define Right 4 //right button on 7,8
extern int i ; //local for loop variable
extern int potent_1 ; //Potentiometer sensor variable
#endif // _main_h_
///////// Main.c ///////////////////////////////////////////////
#include "Main.h"
void main ( void ) {
int M[11] = {0,0,0,0,0,0,0,0,0,0,0}; // temp values to set motor at end of loop M[0..10]
// Initialize motor ports and servos to default positions
// Use only servos in motor ports 4,7 with this code
M[4] = -127 ; // Get in habit of using temp variables
SetServo ( 4 , M[4] ) ; // default is -127
M[7] = +127 ;
SetServo ( 7 , M[7] ) ; // default is +127, m4,m7 are opposites
M[8] = -127 ;
SetServo ( 8 , M[8] ) ; // default -127, use on servo only!
SetJoystickAnalogDeadband ( Joy1 , 1 , 15 , 15 ) ;
SetJoystickAnalogDeadband ( Joy1 , 2 , 15 , 15 ) ;
SetJoystickAnalogDeadband ( Joy1 , 3 , 15 , 15 ) ;
SetJoystickAnalogDeadband ( Joy1 , 4 , 15 , 15 ) ;
while ( 1 ) // Loop forever
{
M[2] = GetJoystickAnalog( Joy1 , 3 ) ; // Left vertical stick
M[9] = -M[2] ; // motor port 9 and 2 rotate in opposite directions, ok for face to face motor pair
M[3] = GetJoystickAnalog( Joy1 , 4 ) ; // Left horizontal stick
// Set M[4] based on buttons 7/8 up/down/left/right
// See F6 Globals for definitions of B6,B7,B8, Up, Down, Left, Right
// Top down priority, if no button is set, keep doing what you were doing before
// Circular setting order, counterclockwise from top: up, left, down, right to increase
if ( GetJoystickDigital(Joy1, B7, Up ) + GetJoystickDigital(Joy1, B8, Up ) > 0 )
{ M[4] = -127 ; // B7Up or B8 Up == -127 }
else if ( GetJoystickDigital(1, B7, Left ) + GetJoystickDigital(1, B8, Left ) > 0 )
{ M[4] = -50 ; }
else if ( GetJoystickDigital(1, B7, Down) + GetJoystickDigital(1, B8, Down) > 0 )
{ M[4] = +50 ; }
else if ( GetJoystickDigital(1, B7, Right ) + GetJoystickDigital(1, B8, Right )> 0 )
{ M[4] = +127 ; }
M[7] = -M[4] ; // m7 is always opposite of m4
M[5] = GetJoystickAnalog( Joy1 , 1 ) ; // joystick1 is right horizontal, then apply limit switch 1,2 limits afterwards
if ( ( M[5] > +1 ) && ( GetDigitalInput( 1 ) == 0 ) )
{ M[5] = 0 ; }// Dont let m5 go positive if limit switch 1 is closed
if ( ( M[5] < -1 ) && ( GetDigitalInput( 2 ) == 0 ) )
{ M[5] = 0 ; }// Dont let m5 go negative if limit switch 1 is closed
M[6] = GetJoystickAnalog( Joy1 , 2 ) ; // stick2 is right vertical, apply potentiometer limits later
potent_1 = GetAnalogInput ( 1 ) ; // Get potentiometer setting, example code
if ( ( M[6] > +1 ) && ( potent_1 > 1200 ) ) // Need a smaller limit to be effective, this is just sample code
{ M[6] = 0 ; }// Dont let m6 go positive if potentiometer analog value is too big
if ( ( M[6] < -1 ) && ( potent_1 < -10 ) ) // Need a bigger limit to be effective, this is just sample code
{ M[6] = 0 ; }// Dont let m6 go negative if potentiometer analog value is too small
// m8 is either full +127 when button 6U is pressed, or full -127 when not pressed, no latching!
// M[8] = GetJoystickDigital(1,B6,Up) > 0 ? +127 : -127 ; // alt trinary method
if ( GetJoystickDigital(Joy1, B6, Up ) > 0 )
{ M[8] = +127 ; }
else { M[8] = -127 ; }
// Apply M] variables to the motors just once per loop
for ( i = 2 ; i <= 9 ; i++ ) // for i = 2..9
{ SetMotor ( i , M* ) ; }
} // end while 1
} // end main