Advertisement
aviosipov

Untitled

Apr 12th, 2014
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.77 KB | None | 0 0
  1.  
  2. #include <Wire.h>
  3. #include <LiquidCrystal_I2C.h>
  4. #include <Servo.h>
  5. #include <Easing.h>
  6.  
  7.  
  8.  
  9. Servo servo1 ; // base
  10. Servo servo2 ; // nack
  11. Servo servo3 ; // twist
  12. Servo servo4 ; // mouth
  13.  
  14. Servo servo5 ; // brow1
  15. Servo servo6 ; // brow2
  16.  
  17.  
  18.  
  19. int analogInputPin1 = 0;    // select the input pin for the potentiometer
  20. int analogInputPin2 = 1;    // select the input pin for the potentiometer
  21.  
  22. int analogInputValue1 = 0 ;
  23. int analogInputValue2 = 0 ;
  24.  
  25. LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display
  26.  
  27.  
  28. int value , analogValue ;
  29. String command ;
  30.  
  31. String inputString = "";    
  32. boolean stringComplete = false;
  33.  
  34. void updateServoStatus(int servo,int value) {
  35.  
  36.  
  37.   switch (servo) {
  38.  
  39.       case 97:
  40.        
  41.         lcd.setCursor(0,1);  
  42.         lcd.print("   ");
  43.         lcd.setCursor(0,1);  
  44.  
  45.         break;
  46.  
  47.       case  98:
  48.        
  49.         lcd.setCursor(4,1);  
  50.         lcd.print("   ");
  51.         lcd.setCursor(4,1);  
  52.  
  53.         break;
  54.  
  55.       case 99:
  56.  
  57.         lcd.setCursor(9,1);  
  58.         lcd.print("   ");
  59.         lcd.setCursor(9,1);  
  60.  
  61.         break;      
  62.  
  63.       case 100:
  64.  
  65.         lcd.setCursor(13,1);  
  66.         lcd.print("   ");
  67.         lcd.setCursor(13,1);  
  68.  
  69.         break;      
  70.   }
  71.  
  72.   lcd.print(value) ;    
  73.  
  74.  
  75. }
  76.  
  77.  
  78. void setup() {
  79.  
  80.  
  81.  
  82.  
  83.  
  84.   lcd.init();                      // initialize the lcd
  85.   lcd.backlight();
  86.   lcd.print("ready ...");
  87.  
  88.  
  89.  
  90.  
  91.   servo1.attach(8);  
  92.   servo2.attach(9);  
  93.   servo3.attach(10);  
  94.   servo4.attach(11);  
  95.   servo5.attach(12) ;
  96.   servo6.attach(13) ;
  97.  
  98.  
  99.   servo1.write(90) ;
  100.   servo2.write(90) ;
  101.   servo3.write(90);
  102.   servo4.write(90);
  103.   servo5.write(90);
  104.   servo6.write(90);
  105.  
  106.  
  107.  
  108.  // servoTest(servo1,90) ;
  109.  // servoTest(servo2,90) ;
  110.   //servoTest(servo3,90) ;
  111.  // servoTest(servo4,90) ;
  112.  // servoTest(servo5,90) ;
  113.  //  servoTest(servo6,90) ;
  114.  
  115.   /*
  116.   servoTest(servo5) ;
  117.   servoTest(servo6) ;
  118.  
  119.   servoTest(servo5) ;
  120.   servoTest(servo6) ;
  121.  
  122.   servo5.write(90) ;
  123.   servo6.write(90) ;
  124. */
  125.  
  126.   /// update display
  127.  
  128.   updateServoStatus(97,90);
  129.   updateServoStatus(98,90);
  130.   updateServoStatus(99,90);
  131.   updateServoStatus(100,90);
  132.  
  133.   /// init finished
  134.  
  135.   Serial.begin (57600);  // start serial communication !
  136.  
  137.  
  138.  
  139. }
  140.  
  141.  
  142.  
  143. void servoTest(Servo servo , int originalPose) {
  144.  
  145.   int startDeg = 40 ;
  146.   int stopDeg = 140 ;
  147.  
  148.  
  149.   for(int pos = startDeg ; pos < stopDeg ; pos += 1)  // goes from 0 degrees to 180 degrees
  150.   {                                  // in steps of 1 degree
  151.     servo.write(pos);              // tell servo to go to position in variable 'pos'
  152.     delay(5);                       // waits 15ms for the servo to reach the position
  153.   }
  154.   for(int pos = stopDeg; pos>=startDeg; pos-=1)     // goes from 180 degrees to 0 degrees
  155.   {                                
  156.     servo.write(pos);              // tell servo to go to position in variable 'pos'
  157.     delay(5);                       // waits 15ms for the servo to reach the position
  158.   }  
  159.  
  160.   servo.write(originalPose) ;
  161.  
  162. }
  163.  
  164.  
  165. void readAnalogData() {
  166.  
  167.   /// handle analog Potentiometer input
  168.  
  169.   analogInputValue1 = analogRead(analogInputPin1);    // read the value from the sensor
  170.   Serial.println(analogInputValue1 -700 )  ;
  171.  
  172.   analogInputValue2 = analogRead(analogInputPin2);    // read the value from the sensor
  173.   analogInputValue2 = analogInputValue2 - 700 ;
  174.  
  175.  
  176.  
  177.   analogValue = map(analogInputValue2, 700, 1023, 0, 179);
  178.   Serial.println(analogValue)  ;
  179.   Serial.println("-") ;
  180.  
  181.  //// servo1.write(val);
  182. }
  183.  
  184.  
  185. int reverseServo(int val) {
  186.  
  187.   ///return map(val,0, 180, 180, 0); // goes the other way
  188.   return val ;
  189. }
  190.  
  191. int mapServo(int val) {
  192.  
  193.   int v = map(val,0, 180, 0, 180); // goes the other  
  194.   return constrain(v,0,180) ;
  195. }
  196.  
  197.  
  198.  
  199.  
  200.  
  201. void serialEvent() {
  202.   while (Serial.available()) {
  203.     // get the new byte:
  204.     char inChar = (char)Serial.read();
  205.     // add it to the inputString:
  206.     inputString += inChar;
  207.     // if the incoming character is a newline, set a flag
  208.     // so the main loop can do something about it:
  209.     if (inChar == '\n') {
  210.       stringComplete = true;
  211.     }
  212.  
  213.  
  214.  
  215.   }
  216. }
  217.  
  218. void loop() {
  219.  
  220.  
  221.   /// recieve serial communication messges
  222.  
  223. //  lcd.setCursor(12,0);  
  224. //  lcd.print("*") ;
  225.  
  226.  
  227.   if (stringComplete) {
  228.  
  229.  
  230.     /// read number
  231.  
  232.     String valueString = inputString.substring(1,inputString.length()-1) ;
  233.     value = valueString.toInt() ;
  234.  
  235.  
  236.     lcd.setCursor(12,0);  
  237.     lcd.print("    ") ;
  238.     lcd.setCursor(12,0);      
  239.     lcd.print(inputString[0]); // command !
  240.  
  241.  
  242.  
  243.     if (inputString[0] == 'a') { // 'a' - motor1
  244.      
  245.       servo1.write(value) ;
  246.       updateServoStatus(inputString[0],value) ;
  247.  
  248.     }
  249.  
  250.     if (inputString[0] == 'b') { // 'b' - motor 2
  251.      
  252.       servo2.write(reverseServo(value)) ;
  253.       updateServoStatus(inputString[0],reverseServo(value)) ;
  254.  
  255.     }
  256.    
  257.    
  258.     if (inputString[0] == 'c') { // 'c' - motor 3
  259.      
  260.       servo3.write(reverseServo(value)) ;
  261.       updateServoStatus(inputString[0],reverseServo(value)) ;      
  262.  
  263.     }
  264.    
  265.  
  266.  
  267.     if (inputString[0] == 'd') { // 'd' - motor 4
  268.      
  269.       servo4.write(reverseServo(value)) ;
  270.       updateServoStatus(inputString[0],reverseServo(value)) ;      
  271.  
  272.     }
  273.  
  274.  
  275.  
  276.     if (inputString[0] == 'e') { // 'e' - motor 5
  277.      
  278.       servo5.write(reverseServo(value)) ;
  279.     //  updateServoStatus(inputString[0],reverseServo(value)) ;      
  280.  
  281.     }
  282.  
  283.  
  284.  
  285.  
  286.     if (inputString[0] == 'f') { // 'd' - motor 4
  287.      
  288.       servo6.write(reverseServo(value)) ;
  289.       //updateServoStatus(inputString[0],reverseServo(value)) ;      
  290.  
  291.     }
  292.  
  293.  
  294.     inputString = "";
  295.     stringComplete = false;
  296.  
  297.   }
  298.  
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement