Advertisement
Ollivier

RoverRF433-Rec

Jul 13th, 2020
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.37 KB | None | 0 0
  1. // -----     -----     -----     -----     -----     -----
  2. // Branchements
  3. // -----     -----     -----     -----     -----     -----
  4. // D0  =>
  5. // D1  =>
  6. // D2  => Récepteur RF433
  7. // D3  => Feux de position Bleus (4 LEDs)
  8. // D4  => Direction Moteur 1 (pas encore connecté)
  9. // D5  => Contrôle moteur 1 (pas encore connecté)
  10. // D6  => Contrôle moteur 2 (pas encore connecté)
  11. // D7  => Direction Moteur 2 (pas encore connecté)
  12. // D8  => Feux arrière et intérieure Rouge (2 LEDs)
  13. // D9  => Buzzer
  14. // D10 => Servo Tourelle(pas encore connecté)
  15. // D11 => Servo Pince(pas encore connecté)
  16. // D12 => Oeil (projecteur tourelle) (1 LED)
  17. // D13 => Phares (2 LEDs)
  18. // -----     -----     -----     -----     -----     -----
  19.  
  20. #include <RCSwitch.h>
  21. #include <Servo.h>
  22.  
  23. Servo monServo;
  24. Servo maPince;
  25. RCSwitch mySwitch = RCSwitch();
  26.  
  27. int rotationServo;
  28. int rotationPince;
  29.  
  30. long temporisation;
  31.  
  32. const int bleu = 3;
  33. const int dirMot_1 = 4;
  34. const int contMot_1 = 5;
  35. const int contMot_2 = 6;
  36. const int dirMot_2 = 7;
  37. const int fArr = 8;
  38. const int phares = 9;
  39. const int tourelle = 10;
  40. const int pince = 11;
  41. const int oeil = 12;
  42. const int buzz = 13;
  43.  
  44. bool etatOeil = 0;
  45. bool etatFArr = 0;
  46. bool etatBleu = 0;
  47. int lum = 0;
  48. int valBleu = 0;
  49. long temp;
  50. int seuilMoteur = 30;
  51. int vitesseMoteurDroit;
  52. int vitesseMoteurGauche;
  53.  
  54. // Fonctions Moteurs
  55. // Gauche avance
  56. void M1_avance(char Speed)
  57. {
  58.  digitalWrite(dirMot_1,HIGH);
  59.  analogWrite(contMot_1,Speed);
  60. }
  61. // -----     -----     -----     -----     -----     -----    
  62.  
  63. // Droite avance
  64. void M2_avance(char Speed)
  65. {
  66.  digitalWrite(dirMot_2,HIGH);
  67.  analogWrite(contMot_2,Speed);
  68. }
  69. // -----     -----     -----     -----     -----     -----    
  70.  
  71. // Gauche recule
  72. void M1_recule(char Speed)
  73. {
  74.  digitalWrite(dirMot_1,LOW);
  75.  analogWrite(contMot_1,Speed);
  76. }
  77. // -----     -----     -----     -----     -----     -----    
  78.  
  79. // Droite recule
  80. void M2_recule(char Speed)
  81. {
  82.  digitalWrite(dirMot_2,LOW);
  83.  analogWrite(contMot_2,Speed);
  84. }
  85. // -----     -----     -----     -----     -----     -----    
  86.  
  87. // Gauche stop
  88. void M1_stop()
  89. {
  90.  digitalWrite(dirMot_1,HIGH);
  91.  analogWrite(contMot_1,0);
  92. }
  93. // -----     -----     -----     -----     -----     -----    
  94.  
  95. // Droite stop  
  96. void M2_stop()
  97. {
  98.  digitalWrite(dirMot_2,HIGH);
  99.  analogWrite(contMot_2,0);
  100. }
  101. // -----     -----     -----     -----     -----     -----    
  102.  
  103. void setup() {
  104.   Serial.begin(115200);
  105.   mySwitch.enableReceive(0);
  106.   pinMode(oeil, OUTPUT);
  107.   pinMode(fArr, OUTPUT);
  108.   pinMode(bleu, OUTPUT);
  109.   pinMode(buzz, OUTPUT);
  110.   pinMode(phares, OUTPUT);
  111.   for (int i=4;i<=7;i++){
  112.     pinMode(i, OUTPUT);                   //définit les port 4 à 7 en sortie
  113.   }
  114.   monServo.attach(tourelle);
  115.   maPince.attach(pince);
  116.   temp = millis();
  117. }
  118.  
  119. void loop() {
  120.   analogWrite(bleu, valBleu);
  121.   if ((millis() - temp) >= 1500) {        // on execute la suite toutes les 1.5 s
  122.     lum = analogRead(A0);                 // lum prend la valeur lue en A0 (photorésistance)
  123.     valBleu = map(lum, 0, 1023, 255, 0);  // valBleu prend la valeur de la conversion
  124.     Serial.println(valBleu);
  125.     temp = millis();
  126.   }
  127.   if (mySwitch.available()) {
  128.  
  129.     if (mySwitch.getReceivedValue() >= 1000 && mySwitch.getReceivedValue() < 2000) {
  130.  
  131.       rotationPince = mySwitch.getReceivedValue() - 1000;
  132.       maPince.write(rotationPince);
  133.       delay (200);
  134.       mySwitch.resetAvailable();
  135.  
  136.     } else if (mySwitch.getReceivedValue() >= 3000 && mySwitch.getReceivedValue() < 4000) {
  137.  
  138.       rotationServo = mySwitch.getReceivedValue() - 3000;
  139.       monServo.write(rotationServo);
  140.       delay (200);
  141.       mySwitch.resetAvailable();
  142.  
  143.     } else if (mySwitch.getReceivedValue() == 2001) {
  144.  
  145.       if (etatOeil == 1) {
  146.         etatOeil = 0;
  147.       } else if (etatOeil == 0) {
  148.         etatOeil = 1;
  149.       }
  150.       digitalWrite(oeil, etatOeil);
  151.       delay (500);
  152.      
  153.     } else if (mySwitch.getReceivedValue() == 2002) {
  154.  
  155.       if (etatFArr == 1) {
  156.         etatFArr = 0;
  157.       } else if (etatFArr == 0) {
  158.         etatFArr = 1;
  159.       }
  160.       digitalWrite(fArr, etatFArr);
  161.       digitalWrite(phares, etatFArr);
  162.       delay (500);
  163.      
  164.     } else if (mySwitch.getReceivedValue() == 2003) {
  165.  
  166.       /*tone(buzz, 329, 200);
  167.       delay (250);
  168.       tone(buzz, 329, 200);
  169.       delay (250);*/
  170.      
  171.       tone(buzz, 440, 1000); //a
  172.       delay (485);
  173.       noTone(buzz);
  174.       delay (15);
  175.       tone(buzz, 440, 1000); //a
  176.       delay (485);
  177.       noTone(buzz);
  178.       delay (15);
  179.       tone(buzz, 440, 1000); //a
  180.       delay (485);
  181.       noTone(buzz);
  182.       delay (15);
  183.       tone(buzz, 349, 700); //f
  184.       delay (335);
  185.       noTone(buzz);
  186.       delay (15);
  187.       tone(buzz, 523, 300); //cH
  188.       delay (185);
  189.       noTone(buzz);
  190.       delay (15);
  191.       tone(buzz, 440, 1000); //a
  192.       delay (485);
  193.       noTone(buzz);
  194.       delay (15);
  195.       tone(buzz, 349, 700); //f
  196.       delay (335);
  197.       noTone(buzz);
  198.       delay (15);
  199.       tone(buzz, 523, 300); //cH
  200.       delay (185);
  201.       noTone(buzz);
  202.       delay (15);
  203.       tone(buzz, 440, 1300); //a
  204.       delay (750);
  205.       noTone(buzz);
  206.      
  207.     } else if (mySwitch.getReceivedValue() >= 4000 && mySwitch.getReceivedValue() < 5000) {
  208.       vitesseMoteurDroit = mySwitch.getReceivedValue() - 4125;
  209.       if (vitesseMoteurDroit > seuilMoteur){
  210.         M2_avance (vitesseMoteurDroit);
  211.         mySwitch.resetAvailable();
  212.       } else if (vitesseMoteurDroit < seuilMoteur*(-1)){
  213.         vitesseMoteurDroit *= -1;
  214.         M2_recule (vitesseMoteurDroit);
  215.         mySwitch.resetAvailable();
  216.       }else if (vitesseMoteurDroit < seuilMoteur && vitesseMoteurDroit > seuilMoteur*(-1)){
  217.         M2_stop ();
  218.         mySwitch.resetAvailable();
  219.       }
  220.     } else if (mySwitch.getReceivedValue() >= 5000 && mySwitch.getReceivedValue() < 6000) {
  221.       vitesseMoteurGauche = mySwitch.getReceivedValue() - 5125;
  222.       if (vitesseMoteurGauche > seuilMoteur){
  223.         M1_avance (vitesseMoteurGauche);
  224.         mySwitch.resetAvailable();
  225.       } else if (vitesseMoteurGauche < seuilMoteur*(-1)){
  226.         vitesseMoteurGauche *= -1;
  227.         M1_recule (vitesseMoteurGauche);
  228.         mySwitch.resetAvailable();
  229.       } else if (vitesseMoteurGauche < seuilMoteur && vitesseMoteurGauche > seuilMoteur*(-1)){
  230.         M1_stop ();
  231.         mySwitch.resetAvailable();
  232.       }
  233.     }
  234.     mySwitch.resetAvailable();
  235.   }
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement