Skip to content

Instantly share code, notes, and snippets.

@chtzvt
Last active March 2, 2017 03:27
Show Gist options
  • Save chtzvt/cbab4c80d68399ef2926d90cb843c4b4 to your computer and use it in GitHub Desktop.
Save chtzvt/cbab4c80d68399ef2926d90cb843c4b4 to your computer and use it in GitHub Desktop.
Given a VEX Cortex with a motor and potentiometer, this program will allow you to control a pointer in order to spell arbitrary strings.
#pragma config(Sensor, in1, POT, sensorPotentiometer)
#pragma config(Motor, port1, MOTOR1, tmotorVex269_HBridge, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
const static int TOLERANCE = 1; // Leeway for pointing to letter
const static int MOTOR_SPEED = 32; // Speed at which to move motor
const static int WAIT_TIME_SEC = 2; // Number of seconds to wait on each letter
// Function should return degree value of position of letters on the dial.
// (Forgive me, for I know not of how to implement a proper lookup table in C)
// switch statement would also work here
int findPosition(char c){
if(c == 'a')
return 0;
else if(c == 'c')
return 10;
else if(c == 'k')
return 20;
else if(c == 'r')
return 30;
else if(c == 's')
return 40;
else if(c == 't')
return 50;
else
return 0;
}
int getDirection(int cur, int next){
if(cur > next)
return -1; // go Backwards
else
return 1; // go Forwards
}
int queryPot() {
return SensorValue(POT);
}
bool withinBounds(int pos){
return pos < (queryPot() + TOLERANCE) && pos > (queryPot() - TOLERANCE);
}
void moveMotor(int speed, int direction){
setMotor(MOTOR1, (speed * direction));
}
void pointToCharacter(char c){
int dir = getDirection(queryPot(), findPosition(c));
while(!withinBounds(findPosition(c)))
moveMotor(MOTOR_SPEED, dir); // Move motor until we are within the tolerable bounds of the letter.
moveMotor(0, 1); // Stop motor on letter
}
task main() {
// You could iterate over a string here.
pointToCharacter('c');
wait(WAIT_TIME_SEC);
pointToCharacter('a');
wait(WAIT_TIME_SEC);
pointToCharacter('t');
// Return to home position
wait(2*WAIT_TIME_SEC);
pointToCharacter('a');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment