I would like to press a button to toggle turning a motor a certain direction that would turn until I pressed that button again. How would I do this? I am using EasyCv4
I have this “chopstick” mechanism that is geared 1-3 . When the motor is on , the chopstick would come together and when it is off , it would come apart. Currently , I have to hold the button to keep the chopsticks together. I want to be able to use a toggle to keep the chopstick together.
Not sure about easyC, but here’s some pseudo-code:
task main()
{
bool flag = false;
while(true)
{
//If you've pressed the button, and the flag is not "raised"
if ((button == pressed) && (flag == false))
{
flag = true;
}
//If you've pressed the button and the flag is "raised"
else if ((button == pressed) && (flag == true))
{
flag = false;
}
//Runs (or doesn't run) the motor based on the flag
if (flag == true)
{
motor = 127;
}
else
{
motor = 0;
}
}
}
This uses a Boolean “flag” to determine if you’ve previously pressed the button.
The following, slightly different from the above example is in “pseudo EasyCV2” (not checked for syntax, as I don’t have access to software right now). It assumes you’re using a bumper sensor in DigitalInput 5 as your “button” (you could also us a button on the transmitter, which would be RxInput 5 or 6), and Motor port 1 – motor values 0 and 255 at full power clockwise and counterclockwise in EasyCV2 (would be - 127 and + 127 in EasyCV4).
define variable bumper
{
[INDENT]while (1==1)
{
[INDENT]SetMotor (1, 0)
bumper = GetDigitalInput(5)
while (bumper==1) // While bumper “not pressed”, keep getting bumper value
{
[INDENT]bumper = GetDigitalInput(5)[/INDENT]
}
wait (250) // Give .25 sec to lift off finger
SetMotor(1, 255) // Turn on motor CCW (switch direction)
while (bumper==1)
{
[INDENT]bumper = GetDigitalInput(5)[/INDENT]
}
wait (250)[/INDENT]
}
[/INDENT]
}
It turns on the motor in one direction and cycles in a loop looking for a “button pressed” value. When it finds the “pressed” condition (Digital Input not equal 1), it switches direction ad infinitum.