Advertisement
tomateblue

Friday.ino

Jun 21st, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Servo.h>
  2. #include <Ultrasonic.h>
  3.  
  4. //Define os pinos para o trigger e echo
  5.  
  6. #define pino_trigger 7
  7. #define pino_echo 6
  8.  
  9. int dir1PinA = 2;
  10. int dir2PinA = 3;
  11. int speedPinA = 9;
  12.  
  13. int dir1PinB = 4;
  14. int dir2PinB = 5;
  15. int speedPinB = 10;
  16.  
  17. Servo servo;
  18.  
  19. //Inicializa o sensor nos pinos definidos acima
  20. Ultrasonic ultrasonic(pino_trigger, pino_echo);
  21.  
  22. int pos = 90;
  23.  
  24. void Motor(int status){
  25.   if (status == 1)
  26.    {
  27.      Serial.println("Direita!!!\n");
  28.      analogWrite(speedPinA, 255);
  29.      digitalWrite(dir1PinA, HIGH);
  30.      digitalWrite(dir2PinA, LOW);
  31.  
  32.      analogWrite(speedPinB, 255);
  33.      digitalWrite(dir1PinB, LOW);
  34.      digitalWrite(dir2PinB, HIGH);
  35.      
  36.    
  37.    }
  38.   if (status == 2)
  39.   {
  40.      Serial.println("Esquerda!!!\n");
  41.      analogWrite(speedPinA, 255);//Sets speed variable via PWM
  42.      digitalWrite(dir1PinA, LOW);
  43.      digitalWrite(dir2PinA, HIGH);
  44.  
  45.      analogWrite(speedPinB, 255);
  46.      digitalWrite(dir1PinB, HIGH);
  47.      digitalWrite(dir2PinB, LOW);
  48.      
  49.   }
  50.  
  51.   if (status == 3)
  52.   {
  53.      Serial.println("Frente!!!\n");
  54.      analogWrite(speedPinB, 255);
  55.      digitalWrite(dir1PinB, LOW);
  56.      digitalWrite(dir2PinB, HIGH);
  57.      
  58.      analogWrite(speedPinA, 255);//Sets speed variable via PWM
  59.      digitalWrite(dir1PinA, LOW);
  60.      digitalWrite(dir2PinA, HIGH);
  61.  
  62.      
  63.      
  64.   }
  65.   if (status == 4)
  66.   {
  67.      Serial.println("Back!!!\n");
  68.      analogWrite(speedPinA, 255);
  69.      digitalWrite(dir1PinA, HIGH);
  70.      digitalWrite(dir2PinA, LOW);
  71.        
  72.      analogWrite(speedPinB, 255);
  73.      digitalWrite(dir1PinB, HIGH);
  74.      digitalWrite(dir2PinB, LOW);
  75.      
  76.   }
  77.   if (status == 5)
  78.   {
  79.      Serial.println("STOP!!!\n");
  80.      analogWrite(speedPinA, 0);
  81.      digitalWrite(dir1PinA, LOW);
  82.      digitalWrite(dir2PinA, HIGH);
  83.  
  84.      analogWrite(speedPinB, 0);
  85.      digitalWrite(dir1PinB, LOW);
  86.      digitalWrite(dir2PinB, HIGH);
  87.      
  88.   }
  89. }
  90. void ServoMotor(int status){
  91.  
  92.   if (status == 1)
  93.   {
  94.      //Serial.println("Direita - 180 Graus\n");
  95.      for (pos = 15; pos<= 165 ; pos++)
  96.      {
  97.       servo.write(pos);
  98.       delay(30);
  99.      }
  100.   }
  101.   if (status == 2)
  102.   {
  103.     //Serial.println("Esquerda - 0 Graus\n");
  104.     for (pos = 165; pos > 15; pos--)
  105.     {
  106.          servo.write(pos);
  107.          delay(30);
  108.     }
  109.   }
  110.   if (status == 3)
  111.   {
  112.     //Serial.println("Frente - 90 Graus\n");
  113.     servo.write(90);
  114.     delay(30);
  115.   }
  116. }
  117.  
  118. int  leitura_Sensor(){
  119.   //Serial.println("Lendo dados do sensor...");
  120.   float cmMsec;
  121.   long microsec = ultrasonic.timing();
  122.   cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);
  123.   //Serial.println(cmMsec);
  124.   int sensor = cmMsec;
  125.   return sensor;
  126. }
  127.  
  128. int ServoAndSensor(){
  129.    int distancia;
  130.    int pos = 0;
  131.      for (pos = 15; pos<= 165 ; pos++)
  132.      {
  133.       servo.write(pos);
  134.       delay(30);
  135.       distancia =  leitura_Sensor();
  136.       Serial.println(distancia);
  137.      }
  138.    
  139.    
  140.     for (pos = 165; pos > 15; pos--)
  141.     {
  142.          servo.write(pos);
  143.          delay(30);
  144.          distancia =  leitura_Sensor();
  145.          Serial.println(distancia);
  146.     }
  147.    
  148. }
  149.  
  150.  
  151.  
  152.  
  153. void setup() {
  154.   Serial.begin(9600);
  155.   servo.attach(11);
  156.   servo.write(pos);
  157.  
  158.   pinMode(dir1PinA,OUTPUT);
  159.   pinMode(dir2PinA,OUTPUT);
  160.   pinMode(speedPinA,OUTPUT);
  161.  
  162.   pinMode(speedPinB,OUTPUT);
  163.   pinMode(dir2PinB,OUTPUT);
  164.   pinMode(speedPinB,OUTPUT);
  165. }
  166.  
  167.  
  168. void loop() {
  169.   ServoAndSensor();
  170.  
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement