Advertisement
Ceiifadore

Servo Motor

Jun 30th, 2015
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. #include <Servo.h>
  2.  
  3. Servo servo1;
  4. Servo servo2;
  5. Servo servo3;
  6. Servo servo4;
  7.  
  8. char buffer[4];
  9. int received;
  10.  
  11. void setup(){
  12.  
  13. //Inicia a porta serial
  14. Serial.begin(9600);
  15.  
  16. //Atribui as entradas do Arduino
  17. //as variaveis
  18. servo1.attach(13); //garra
  19. servo2.attach(12); //pulso
  20. servo3.attach(11); //base
  21. servo4.attach(10); //cotovelo
  22.  
  23. received = 0;
  24. buffer[received] = '\0';
  25. }
  26.  
  27. void loop(){
  28.  
  29. //Verifica se possui alguma entrada de dados
  30. //na entrada Serial
  31. if(Serial.available()){
  32.  
  33. char c = Serial.read();
  34.  
  35. // Se o "c" for igual ao caracter "A" (97 na tabela ASCII)
  36. if(c == 97){
  37. Serial.println(buffer);
  38. int numero = atoi(buffer);
  39. if(numero <= 30){
  40. servo1.write(numero);
  41. received = 0;
  42. }
  43.  
  44. // Se o "c" for igual ao caracter "B" (98 na tabela ASCII)
  45. if(c == 98){
  46. Serial.println(buffer);
  47. int numero = atoi(buffer);
  48. if(numero <= 60){
  49. servo2.write(numero);
  50. received = 0;
  51. }
  52. received = 0;
  53. }
  54. // Se o "c" for igual ao caracter "C" (99 na tabela ASCII)
  55. if(c == 99){
  56. Serial.println(buffer);
  57. int numero = atoi(buffer);
  58. if(numero >= 360){
  59. servo3.write(numero);
  60. received = 0;
  61. }
  62. received = 0;
  63. }
  64. // Se o "c" for igual ao caracter "D" (100 na tabela ASCII)
  65. if(c == 99){
  66. Serial.println(buffer);
  67. int numero = atoi(buffer);
  68. if(numero <= 120){
  69. servo4.write(numero);
  70. received = 0;
  71. }
  72. received = 0;
  73. }
  74. if(c != 97 && c != 98 && c != 99 && c != 100){
  75. buffer[received++] = c;
  76. }
  77.  
  78. Serial.flush();
  79. }
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement