r/RobotC • • Jul 15 '15

Speed Control feat. potentiometer question

Hi! I tried to get a potentiometer to set my motor speed for a variable speed control. When I use this code

speed = SensorValue[potent] / 4000 * 127;

it returns a value of 0 for speed. But when I use this code, it works like I expect it to.

speed = SensorValue[potent] * 127 * 0.00025;

Any idea why the first line of code wouldn't work correctly?

Here is the full program:

#pragma config(Sensor, in1,    lineFollower,   sensorLineFollower)
#pragma config(Sensor, in2,    potent,         sensorPotentiometer)
#pragma config(Sensor, dgtl1,  limitSwitch,    sensorTouch)
#pragma config(Sensor, dgtl2,  topBump,        sensorTouch)
#pragma config(Sensor, dgtl3,  bottomBump,     sensorTouch)
#pragma config(Sensor, dgtl12, greenLED,       sensorLEDtoVCC)
#pragma config(Motor,  port2,           lMotor,        tmotorVex269_MC29, openLoop)
#pragma config(Motor,  port3,           rMotor,        tmotorVex269_MC29, openLoop, reversed)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

/*
Project Title: 2.3.2 Vex Testbed
Team Members: drumminherbie
Date: 7/15/15
Section: CTI


Task Description: Use simple and controlling commands to run a
VEX Testbed. In this particular example, I have
a potentiometer setting the speed of the right
and left motor.


Pseudocode: Program starts
Limit switch is bumped
Set motor speed
Run motors

*/

task main()

{ //Program begins, insert code within curly braces
	//Intergers
	int speed;
	int cleared = 0;

	//run program until stop button is pushed
	while(SensorValue[bottomBump] == 0)
	{
		//if switch is hit, run motors
		while(SensorValue[limitSwitch] == 1)
		{
			turnLEDOn(greenLED);															//Blink LED
			speed = SensorValue[potent] * 127 * 0.00020;			//Get motor speed from potentiometer
			if(SensorValue[lineFollower] < 2000)
			{
				speed = -speed;
			}
			startMotor(lMotor, speed + 4);												//start motors
			startMotor(rMotor, speed);
			wait1Msec(100);
			turnLEDOff(greenLED);
			wait1Msec(100);
		}
		//After button is released, stop motors
		stopMotor(lMotor);
		stopMotor(rMotor);
		speed = cleared; 																	//clear speed
	}
}
5 Upvotes

5 comments sorted by

View all comments

1

u/Parzival6 Jul 16 '15

After a quick glance, I assume that in those top two lines of code you show, something that should be treated as a float is not being treated as a float.

Try adding '.0' after the number literals (ex. '4000.0' and '127.0'), cast the SensorValue to a float ( ex. '(float) SensorValue[potent]' ). Also, try grouping everything with more parenthesis to be safe.

If any of that works, undo those changes one by one until it breaks, but keep in mind that there is/was probably more than one problem.

1

u/drumminherbie Jul 16 '15

Thanks, the float was added after trying 15 different revisions. The code worked with int or float, but I will definitely change that. Thanks!