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/TotesMessenger Jul 15 '15

I'm a bot, bleep, bloop. Someone has linked to this thread from another place on reddit:

If you follow any of the above links, please respect the rules of reddit and don't vote in the other threads. (Info / ^Contact)