I currently have easyC V4, and I don’t know how to program a function for Operator Control. Basically, what I want to do is create a function that with the press of a button from the controller, 2 motors will be activated. How do you do this?
What have you tried?
Joystick Digital to Motor sounds straightforward to control one motor,
just repeat the block with the same button but the second motor number.
That will meet your description of “press one button, activate 2 motors”.
I’m sorry, let me rephrase my question," how do you program with the push of a button two motors are activated **and continue to run **without the button being pressed, and when the button is pressed again, the two motors stop?."
The way I accomplish this is to program a logic latch and activate the motors on the result of the logic latch, Here’s some psuedo code:
int latch = 0;
If (buttonPress && latch == 0) latch = 1;
else if (buttonPress && latch = 1) latch = 0;
if (latch == 1) { //Activate motors
…
}
else if (latch == 0) { // stop motors
…
}
If you use easy C the physical implementation is not too hard, a little tedious, coding in RobotC and you may need wait statements to ensure multiple button presses are not recorded as one (you may need that in Easy C as well).
If you need to go both directions you’ll need to design a 3 way latch.
Hope this helps - Kb
int latch = 0;
If (buttonPress && latch == 0) latch = 1;
else if (buttonPress && latch = 1) latch = 0;
if (latch == 1) { //Activate motors
...
}
else if (latch == 0) { // stop motors
...
}
If this code is in a loop, you might want to add a one-shot variable so that it doesn’t oscillate your motors when you hold down the button:
int latch = 0;
int oneshot = 0;
//loop start
...
if (buttonPress && latch == 0 && oneshot == 0)
{
latch = 1;
oneshot = 1;
}
else if (buttonPress && latch == 1 && oneshot == 0)
{
latch = 0;
oneshot = 1;
}
if (!buttonPress) oneshot = 0;
if (latch == 1) { //Activate motors
...
}
else if (latch == 0) { // stop motors
...
}
...
//loop end
There are ways to make this code cleaner and more efficient, but I shall leave that as an exercise to the reader
Here is what my team did last year for round up to set our arm heights
#include "Main.h"
void ArmControl ( void )
{
char Channel8_Button1 = 0;
char Channel8_Button2 = 0;
Channel8_Button1 = GetJoystickDigital ( 1 , 8 , 1 ) ;
Channel8_Button2 = GetJoystickDigital ( 1 , 8 , 2 ) ;
if ( Channel8_Button1 == 1 )
{
if ( Button8_Control == 1 )
{
Button8_Control = 0 ;
}
else
{
Button8_Control = 1 ;
}
Wait ( 150 ) ;
}
else if ( Channel8_Button2 == 1 )
{
if ( Button8_Control == 2 )
{
Button8_Control = 0 ;
}
else
{
Button8_Control = 2 ;
}
Wait ( 150 ) ;
}
switch ( Button8_Control )
{
case 1 :
{
Channel8_Button1 = ArmHeight(271) ;
break ;
}
case 2 :
{
Channel8_Button2 = ArmHeight(219) ;
break ;
}
default :
{
right_stick_value = GetJoystickAnalog( 1 , 2 ) ;
if ( right_stick_value >= -5 && right_stick_value <= 5 )
{
SetMotor ( 1 , -25 ) ;
SetMotor ( 10 , -25 ) ;
}
else
{
JoystickToMotorAndLimit ( 1 , 2 , 1 , 1 , 2 , 1 ) ;
JoystickToMotorAndLimit ( 1 , 2 , 10 , 1 , 2 , 1 ) ;
}
}
}
}
It’s kinda long and pretty confusing so I am sure there is an easier way to do it, which I am exploring right now. If anyone know how to do it in EasyC(an easier way anyways) please tell me!
Sorry to post on this thread again but what is the equivalent to the ‘‘logic latch’’ in EasyC. I am assuming that you guys don’t use it but do you know anyway?
I’ve attached a sample in easyC which ‘should’ turn on motors 1 & 2 when 8U is pressed and turn them off again when 8U is pressed again. I can’t test it yet but it should work. The only ‘latch’ provided in easyC is the latch digital output (which works we tested it last year with an LED but it won’t drive a motor so you have to code your own ‘latch’. There are other ways to do this so feel free to experiment and improvise.
Cheers Kb
sampleLatch.zip (1.85 KB)