I am creating a robot that just drives around and has a pneumatics claw to demonstrate the use of sensors and pneumatics. I have 4 different operations that I have running through operator control on EasyC V4 and was hoping to use the Potentiometer to change between them. I have the “get potentiometer” and all the operations in a while loop so that I may change between the operations with out turning off operator control. I put in “if” statements that read “if pot <= 255” “if pot <= 510” and so on. This is not working and was wondering if anyone could help me troubleshoot this. It will choose one of the operations, seemingly at random.
If anyone could help me solve this problem or provide a sample code, that would be much appreciated.
that’ll work, you just have to do if(pot <= 255) for the first one, then if(pot > 255 && pot <= 510) for the next one, and then if(pot > 510 && pot<= whatever your next value is) and so on. They each have to have an upper and lower bound.
Hello, I use this code in RobotC to switch between robot actions, use a value range of the potentiometer to know what action I am, I hope you serve…
//////////////////////////////////////////////////////////////////////////////////////////
task main()
{
int selec;
while(true)
{
if (SensorValue[pot]<1000)
{
selec =1;
}
else if (SensorValue[pot]<2000 && SensorValue[pot]>1000)
{
selec =2;
}
else if (SensorValue[pot]<3000 && SensorValue[pot]>2000)
{
selec =3;
}
else if (SensorValue[pot]<4000 && SensorValue[pot]>3000)
{
selec =4;
}
/*-----------------------------------------------------------------*/
switch (selec)
{
case 1:
// code 1 //
break;
case 2:
// code 2 //
break;
case 3:
// code 3 //
break;
case 4:
//code 4 //
break;
default:
// off //
break;
}
}
}
///////////////////////////////////////////////////////////////////////////////////////
I think I already tried that, but will try again because at that time I was just playing around
Worked almost perfectly, the only problem was that it was running the last operation instead of the first when the pot was set to the first operation. Any ideas on why that is happening?
Can you post your code? What does the debugger say the pot value was?
If you are saying position 1 == 4, 2 == 3, ect. I would assume your pot is probably just backwards to the way you are turning it. You could change the number or turn the pot around.
My guess is that the you are using the entire bounds of the pot. Sometimes the edges of the pot can have funky values and cause similar effects.
This is the code I am using. I am using the entire bounds of the pot so that may be the problem.
#include "Main.h"
void OperatorControl ( unsigned long ulTime )
{
unsigned int rightlight;
unsigned int leftlight;
unsigned int rightline;
unsigned int midline;
unsigned int leftline;
long EncoderLeft = 1;
long EncoderRight = 1;
int Out;
int In;
unsigned int pot;
StartQuadEncoder ( 1 , 2 , 1 ) ;
StartQuadEncoder ( 3 , 4 , 1 ) ;
while ( 1==1 )
{
pot = GetAnalogInput ( 5 ) ;
// first
if ( pot <=255 )
{
Tank2 ( 1 , 3 , 2 , 2 , 3 , 1 , 1 ) ;
JoystickDigitalToMotor ( 1 , 5 , 1 , 75 , 2 , -75 , 5 ) ;
JoystickDigitalToMotor ( 1 , 5 , 1 , -75 , 2 , 75 , 6 ) ;
Out = GetJoystickDigital ( 1 , 6 , 1 ) ;
In = GetJoystickDigital ( 1 , 6 , 2 ) ;
if ( Out ==1 )
{
SetDigitalOutput ( 10 , 1 ) ;
}
else if ( In ==0 )
{
SetDigitalOutput ( 10 , 0 ) ;
}
}
if ( pot >255 && pot <=510 )
{
EncoderRight = GetQuadEncoder ( 1 , 2 ) ;
EncoderLeft = GetQuadEncoder ( 3 , 4 ) ;
PrintToScreen ( "EncoderRight Value:%d\n" , (long)EncoderRight ) ;
PrintToScreen ( "EncoderLeft Value:%d\n" , (long)EncoderLeft ) ;
if ( EncoderRight <=500 )
{
SetMotor ( 3 , -70 ) ;
}
else
{
SetMotor ( 3 , 0 ) ;
}
if ( EncoderLeft <=500 )
{
SetMotor ( 2 , 70 ) ;
}
else
{
SetMotor ( 2 , 0 ) ;
}
}
if ( pot >510 && pot <=765 )
{
SetServo ( 4 , 127 ) ;
Wait ( 350 ) ;
leftlight = GetAnalogInput ( 1 ) ;
PrintToScreen ( "leftlight Value:%d\n" , (int)leftlight ) ;
SetServo ( 4 , -127 ) ;
Wait ( 350 ) ;
rightlight = GetAnalogInput ( 1 ) ;
PrintToScreen ( "rightlight Value:%d\n" , (int)rightlight ) ;
if ( leftlight < rightlight )
{
SetMotor ( 3 , 0 ) ;
SetMotor ( 2 , 0 ) ;
}
else
{
SetMotor ( 3 , 127 ) ;
SetMotor ( 2 , -127 ) ;
}
Wait ( 2000 ) ;
SetMotor ( 3 , -127 ) ;
SetMotor ( 2 , -127 ) ;
Wait ( 500 ) ;
SetMotor ( 3 , 0 ) ;
SetMotor ( 2 , 0 ) ;
Wait ( 500 ) ;
}
if ( pot <765 && pot<= 1023 )
{
leftline = GetAnalogInput ( 2 ) ;
midline = GetAnalogInput ( 3 ) ;
rightline = GetAnalogInput ( 4 ) ;
PrintToScreen ( "leftline=%d\n" , (int)midline ) ;
PrintToScreen ( "midline =%d\n" , (int)midline ) ;
PrintToScreen ( "rightline =%d\n" , (int)rightline ) ;
if ( midline > 550 )
{
SetMotor ( 2 , 65 ) ;
Wait ( 5 ) ;
SetMotor ( 2 , 127 ) ;
}
else
{
SetMotor ( 3 , -127 ) ;
Wait ( 5 ) ;
SetMotor ( 3 , -110 ) ;
}
}
}
}
Thank You for all your help
Here’s your problem,
if ( pot <765 && pot<= 1023 )
Always true no matter what the value.
That was the problem, it works perfectly now, Thank You very much
It won’t make a difference in functionality, but you might want to use if, else if instead of multiple if statements. When you do this, you don’t need to have the following:
if (x < 500)
else if (x > 500 and x < 1000)
…
and only need:
if (x < 500)
else if (x < 1000)
…
This is because if you get to the else if part, the first if needs to be false, meaning that the pot value must be greater that 500. Hopefully you can understand what I mean!
A chain of else if is prone to errors when inserting new ifthenelse later.
Some of the code samples here will also fail in the unlikely cases of x == 500.
I think the following approach will work and fix both of those problems, but haven’t tested it. The angle checks need to be in increasing order.
The use of continue in this context makes it very clear than there is no lurking unqualified coding block later in the loop that will do something else.
while(1){
angle = GetAnalogInput( angle_sensor1);
if ( angle < 500) {
//code1
continue;} // done with code1, skip the rest, go back to while loop
if ( angle < 1000 ) {
// code2
continue; } // done with code2, skip the rest, go back to while loop
} //wend
if (SensorValue[pot]<1000)
{
selec =1;
}
else if (SensorValue[pot]<2000 && SensorValue[pot]>1000)
{.
selec =2;
}
else if (SensorValue[pot]<3000 && SensorValue[pot]>2000)
{
selec =3;
}
else if (SensorValue[pot]<4000 && SensorValue[pot]>3000)
{
selec =4;
This code can be simplified
selec = SensorValue[pot]/1024 +1;
//This makes use of integer divide. You can make any number of steps this way easily. A more general method is to use the following form
index_nsteps = SensorValue[pot] /(4096/nsteps) + 1 ;
where nsteps is the integer number of steps you want to have in your index.
Be careful… the order of the math steps is important.
It is slightly less efficient than using if else statements since an integer divide is used… but typically not a problem for Vex users.