Advertisement
RuiViana

Pisca_LED_Carrinho

Oct 5th, 2016
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <TimerOne.h>                                   // Biblioteca do timer1
  2. //----------MAPEAMENTO DO HARDWARE------------
  3.  
  4. //Motores
  5. const int motorA = 5;
  6. const int motorB = 6;
  7. const int dirA = 7;
  8. const int dirB = 8;
  9.  
  10. //Sensores QREE
  11. int SensorDir = A0;
  12. int SensorEsq = A1;
  13. int valorSensorDir = 0;
  14. int valorSensorEsq = 0;
  15.  
  16. //LEDs
  17. int LedDir = 11;
  18. int LedEsq = 10;
  19. int LedF1 = 5;
  20. int LedF2 = 3;
  21. byte Nivel = 0;
  22. //---------------------------------
  23. void Pisca()
  24. {
  25.   Nivel = !Nivel;
  26.   digitalWrite( LedF1, Nivel);
  27. }
  28. //---------------------------------
  29. void setup()
  30. {
  31.   pinMode(motorA, OUTPUT);
  32.   pinMode(motorB, OUTPUT);
  33.   pinMode(dirA, OUTPUT);
  34.   pinMode(dirB, OUTPUT);
  35.   pinMode(LedDir, OUTPUT);
  36.   pinMode(LedEsq, OUTPUT);
  37.   pinMode(LedF1, OUTPUT);
  38.   pinMode(LedF2, OUTPUT);
  39.   Timer1.initialize(250000);                         // Define interrupt de Timer1 a cada 250 ms
  40.   Timer1.attachInterrupt(Pisca);                  // OverFlow de Timer1
  41. }//END SETUP
  42. //---------------------------------
  43. void loop()
  44. {
  45.   valorSensorDir = analogRead(SensorDir);
  46.   valorSensorEsq = analogRead(SensorEsq);
  47.  
  48.   if ((valorSensorDir < 700) && (valorSensorEsq < 700))
  49.   {
  50.     Timer1.stop();
  51.     digitalWrite(dirA, HIGH); //Dire��o
  52.     digitalWrite(dirB, HIGH);
  53.     analogWrite(motorA, 200); //Velocidade
  54.     analogWrite(motorB, 200);
  55.   }
  56.  
  57.   if (valorSensorDir > 700)
  58.   {
  59.     //Aqui iria ligar os LEDs F1 e F2
  60.     Timer1.start();
  61.     analogWrite(motorA, 0); //Velocidade
  62.     analogWrite(motorB, 0);
  63.     delay(1000);
  64.     //Aqui desligar LEds F1 e F2
  65.     //Ligar LED Direira ou Esquerda
  66.     Timer1.stop();
  67.     digitalWrite(dirA, LOW); //Dire��o
  68.     digitalWrite(dirB, HIGH);
  69.     analogWrite(motorA, 200); //Velocidade
  70.     analogWrite(motorB, 200);
  71.     delay(1000);
  72.   }
  73.  
  74.   if (valorSensorEsq > 700)
  75.   {
  76.     //Aqui iria ligar os LEDs F1 e F2
  77.     Timer1.start();
  78.     analogWrite(motorA, 0); //Velocidade
  79.     analogWrite(motorB, 0);
  80.     delay(1000);
  81.     //Aqui desligar LEds F1 e F2
  82.     //Ligar LED Direira ou Esquerda
  83.     Timer1.stop();
  84.     digitalWrite(dirA, HIGH); //Dire��o
  85.     digitalWrite(dirB, LOW);
  86.     analogWrite(motorA, 200); //Velocidade
  87.     analogWrite(motorB, 200);
  88.     delay(1000);
  89.   }
  90. }// END LOOP
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement