Advertisement
vulture2600

arduino serial mouse control

Oct 25th, 2013
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.00 KB | None | 0 0
  1. //code to allow Processing to control servos with mouse input.
  2. //PWM pins remaining are 3, 5, 6 and 9.
  3. #include <Servo.h>
  4. Servo servoX;
  5. Servo servoY;
  6. Servo servoA;
  7. Servo servoB;
  8. const int laserPin = 8;
  9. const int buzzerPin = 7;
  10. bool laser;
  11. int x1, y1, a1, b1;//servo position variables
  12.  
  13. void setup() {
  14.   servoX.attach(3);
  15.   servoY.attach(5);
  16.   servoA.attach(6);
  17.   servoB.attach(9);
  18.   pinMode(laserPin, OUTPUT);
  19.   pinMode(buzzerPin, OUTPUT);
  20.   digitalWrite(laserPin, LOW);
  21.   laser = false;
  22.   Serial.begin(19200);
  23.   servoX.write(90);
  24.   servoY.write(90);
  25.   servoA.write(90);
  26.   servoB.write(90);
  27.  
  28. }
  29.  
  30. void loop() {
  31.   /* most of this loop reads incomming characters from Processing and decides what to do with them on a case by case basis. if they are numbers, process them as integers coordinates.
  32.  if they are letters, they signify the end of a coordinate (ex. 45x, 125y), turn the laser on or off, or fire the buzzer.
  33.  */
  34.   a1 = servoA.read();//assigns actual position of servo to a1/b1.
  35.   b1 = servoB.read();
  36.   static int v = 0; //value to send to servo (0-180)
  37.   if (Serial.available()){    
  38.     char ch = Serial.read();
  39.     switch(ch){ //switch based on value of ch
  40.       case '0'...'9':
  41.         v = v * 10 + ch - '0';
  42.         break;
  43.         /* if chars sent are, for example, '45x' (turn servo x to 45)
  44.         v is what we want to send to servoX, currently 0
  45.         first digit is 10's place, second is 1's place. v now equals an integer.
  46.         third is not numeric so it will be handled by next case.
  47.         */
  48.        
  49.       case 'x': // if char is x, end of x coordinate recieved.
  50.         //send value of v to x servo and reset v to 0
  51.         x1 = map(v, 0, 180, 160, 0);//reverses x. reverse 160 and 0 if x is backwards. output values not symmetrical to account for servos actual range.
  52.         servoX.write(x1);
  53.         v = 0;
  54.         break;
  55.        
  56.       case 'y': //write y value to servoY
  57.         y1 = map(v, 0, 180, 10, 170);
  58.         servoY.write(y1);
  59.         v = 0;
  60.         break;
  61.        
  62.       case 'l': //laserOnOff() function:
  63.         laserOnOff();
  64.         break;
  65.        
  66.       case 'u': //moves servoA up.
  67.         a1 += 2; //increments a1 by 2 degrees
  68.         if (a1 >= 150){ //limits servoA to 150
  69.           a1 = 150;
  70.         }
  71.         servoA.write(a1);
  72.         if (a1 < 150){//if servoA is in range, tell processing to move position slider
  73.           Serial.write("2");
  74.         }
  75.         break;
  76.        
  77.       case 'd': //moves servoA down.
  78.         a1 -= 2;//decrements a1 by 2
  79.         if (a1 <= 20){//limits servoA to 20 degrees
  80.           a1 = 20;
  81.         }
  82.         servoA.write(a1);
  83.         if (a1 > 20){//if servoA is in range, tell processing to move position slider
  84.           Serial.write("3");
  85.         }
  86.         break;
  87.        
  88.       case 'a': // moves servoB left.
  89.         b1 -= 2; //decrements b1 by 2
  90.         if (b1 <= 20){//limits servoB to 20 degrees.
  91.           b1 = 20;
  92.         }
  93.         servoB.write(b1);
  94.         if (b1 > 20){//if servoB is within range, tell processing to move slider
  95.           Serial.write("4");
  96.         }
  97.         break;
  98.        
  99.       case 's': // moves servoB right.
  100.         b1 += 2;//increments b1 by 2
  101.         if (b1 >= 160){//limits servoB to 160 degrees.
  102.           b1 = 160;
  103.         }
  104.         servoB.write(b1);
  105.         if (b1 < 160){//if servoB is within range, tell processing to move slider
  106.           Serial.write("5");
  107.         }
  108.         break;
  109.        
  110.       case 'b': //turns on buzzer while right click is held
  111.         digitalWrite (buzzerPin, HIGH);
  112.         break;
  113.       case 'n': //turns off buzzer when right click is released.
  114.         digitalWrite (buzzerPin, LOW);
  115.         break;
  116.     }    
  117.   }
  118. }
  119. void laserOnOff(){//toggles laser on or off and sends feed back to processing.
  120.  
  121.   if(laser == false){
  122.     digitalWrite (laserPin, HIGH);
  123.     Serial.print("1");
  124.     laser = true;
  125.   }
  126.   else if (laser == true){
  127.     digitalWrite (laserPin, LOW);
  128.     Serial.print("0");
  129.     laser = false;
  130.   }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement