Facebook
From remi, 4 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 201
  1. #include <Servo.h>
  2.  
  3. Servo myservo;  // create servo object to control a servo
  4.                 // a maximum of eight servo objects can be created
  5.  
  6. int pos = 0;    // variable to store the servo position
  7. int motor = 0;
  8.  
  9. void setup()
  10. {  
  11.   Serial.begin(9600);  // initialize serial:
  12.   myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  13.  
  14.   Serial.print("Arduino control Servo Motor Connected OK");
  15.   Serial.print('\n');
  16. }
  17.  
  18. void loop()
  19. {
  20.   // if there's any serial available, read it:
  21.   while (Serial.available() > 0) {
  22.    
  23.     // look for the next valid integer in the incoming serial stream:
  24.     motor = Serial.parseInt();
  25.    
  26.     // do it again:
  27.     pos = Serial.parseInt();
  28.  
  29.     // look for the newline. That's the end of your  sentence:
  30.     if (Serial.read() == '\n') {
  31.              
  32.        myservo.write(pos);              // tell servo to go to position in variable 'pos'
  33.        delay(15);                       // waits 15ms for the servo to reach the position
  34.      
  35.       // print the three numbers in one string as hexadecimal:
  36.       Serial.print("Data Response : ");
  37.       Serial.print(motor, DEC);
  38.       Serial.print(pos, DEC);
  39.      
  40.     }
  41.   }
  42. }
  43.  
  44.   //for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees
  45.   //{                                  // in steps of 1 degree
  46.   //  myservo.write(pos);              // tell servo to go to position in variable 'pos'
  47.   //  delay(15);                       // waits 15ms for the servo to reach the position
  48.   //}
  49.   //for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
  50.   //{                                
  51.   //  myservo.write(pos);              // tell servo to go to position in variable 'pos'
  52.   //  delay(15);                       // waits 15ms for the servo to reach the position
  53.   //}
  54.  
  55.  
  56.   //val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  57.   //val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
  58.   //myservo.write(val);                  // sets the servo position according to the scaled value
  59.   //delay(15);