VEX robotics 393 motor control test

Dependencies:   Servo mbed

Revision:
1:c41ae3b710a2
Parent:
0:1719fc9a160e
Child:
2:a42e1062af7b
--- a/main.cpp	Tue Jan 28 20:25:41 2014 +0000
+++ b/main.cpp	Tue Jan 28 20:43:23 2014 +0000
@@ -9,104 +9,64 @@
 // Add variables:
 //==================================
 
+
 //add a servo to PWM output on pin 25
 Servo myservo(p21);
 
-//add a variable for servo position
-float position = 0.5;
+//add a variable for motor speed
+//0.0 is full counter-clockwise
+//0.5 is zero rpm
+//1.0 is full clockwise
 
-//add variables for the onboard LEDS, which display for each button
+float speed = 0.5;
+
+//add variables for the 4 onboard LEDS
 DigitalOut led1(LED1), led2(LED2), led3(LED3), led4(LED4);
 
-
-
-//==================================
-// Functions for each button press
-//==================================
-/*
-
-// Functions for button 1
-void a_hit_interrupt (void) {
-
-    //move servo motor
-    position = 1.0;
-    myservo = position;
-    
-    //Set LED 1
-    led1 = 1, led2 = 0, led3 = 0, led4 = 0;
-}
- 
- 
- 
-// Functions for button 2
-void b_hit_interrupt (void) {
-
-    //move servo motor
-    position = 0.0;
-    myservo = position;
-    //Set LED 2
-    led1 = 0, led2 = 1, led3 = 0, led4 = 0;    
-}
-
-
-
-// Functions for button 3 
-void c_hit_interrupt (void) {
-
-    //Set LED 3
-    led1 = 0, led2 = 0, led3 = 1, led4 = 0;
-}
- 
- 
- 
-// Functions for button 4 
-void d_hit_interrupt (void) {
-
-    //Set LED 4
-    led1 = 0, led2 = 0, led3 = 0, led4 = 1;
-}
-
+//PC USB link
+Serial pc(USBTX, USBRX);
 
 
 //==========================================================
 //  MAIN FUNCTION
 //  waits for button press, then uses the button code above
 //==========================================================
-*/
+
 int main() {
-/*
-    //tune range depending on servo motor
-    float range = 0.0009;
-       
-    // Use internal pullup resistors to limit the signal current
-    a.mode(PullUp);
-    b.mode(PullUp);
-    c.mode(PullUp);
-    d.mode(PullUp);            
-    
-    // Delay for initial pullup switching to take effect
-    wait(.01);
+
+//prompt the PC user for differential speed value
+
+    pc.printf("Control of LED dimmer by host terminal\n\r"); 
+    pc.printf("Press 'u‘ = brighter, 'd‘ = dimmer\n\r");
+
+    while(1) {
+
+//get the value for speed
+
+        char c = pc.getc();
+        wait(0.001);
+        
+//up logic
+        
+        if((c == 'u') && (speed < 1.0)) {
+            speed += 0.001;
+            myservo = speed;
+        }
+
+//down logic
     
-    // tells the code what function should be called for each switch press
-    a.fall(&a_hit_interrupt);
-    b.fall(&b_hit_interrupt);
-    c.fall(&c_hit_interrupt);
-    d.fall(&d_hit_interrupt);
-    
+        if((c == 'd') && (speed > 0.0)) {
+            speed -= 0.001;
+            myservo = speed;
+        } 
+ 
+//display the speed to the PC   
+        pc.printf("%c %1.3f \n \r",c,speed); 
     
-    while(1) { 
-        //set the servo to use the set value
-        myservo.calibrate(range, 45.0); 
-        
-        //reset LEDs
-        led1 = 0, led2 = 0, led3 = 0, led4 = 0;
+    }
+
+//end while loop
 
-    }
-    */
-    myservo = position;
-    wait(1);
-    while (1){
-    myservo = myservo+0.01;
-    wait(0.1);
-    }
 }
+
+//end of main function