mpmcws

4WD Car Completo

Jun 15th, 2018
47
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.    Marco Cerani
  3.    4WD
  4.    maggio 2018 V3.2 - codice open source
  5. */
  6. #include <SoftwareSerial.h>
  7. #define bluetooth Serial
  8. SoftwareSerial mySerial(0, 1);
  9.   const int motorA1  = 4;
  10.   const int motorA2  = 5;
  11.   const int motorB1  = 10;
  12.   const int motorB2  = 11;
  13.   const int led  = 12;
  14.   const int clacson = 6 ;
  15.   int i=0;
  16.   int j=0;
  17.   int state;
  18.   int vSpeed=200;
  19.  
  20. void setup() {
  21.     pinMode(motorA1, OUTPUT);
  22.     pinMode(motorA2, OUTPUT);
  23.     pinMode(motorB1, OUTPUT);
  24.     pinMode(motorB2, OUTPUT);
  25.     pinMode(led, OUTPUT);
  26.     pinMode(clacson,OUTPUT);
  27.     Serial.begin(9600);
  28. }
  29. void loop() {
  30.     if(Serial.available() > 0){    
  31.       state = Serial.read();  
  32.       Stop();
  33.     }
  34.     if (state == 'A') {
  35.       analogWrite(motorA1, vSpeed);
  36.       analogWrite(motorA2, 0);
  37.       analogWrite(motorB1, vSpeed);      
  38.       analogWrite(motorB2, 0);    
  39.     }
  40.     else if (state == 'B') {
  41.       analogWrite(motorA1, 0);  
  42.       analogWrite(motorA2, vSpeed);
  43.       analogWrite(motorB1, 0);  
  44.       analogWrite(motorB2, vSpeed);
  45.     }
  46.     else if (state == 'C') {
  47.       analogWrite(motorA1, 200);  
  48.       analogWrite(motorA2, 0);
  49.       analogWrite(motorB1, 0);
  50.       analogWrite(motorB2, 200);
  51.     }
  52.     else if (state == 'D') {
  53.       analogWrite(motorA1, 0);  
  54.       analogWrite(motorA2, 200);
  55.       analogWrite(motorB1, 200);  
  56.       analogWrite(motorB2, 0);    
  57.     }
  58.     else if (state == 'E') {
  59.       if (i==0){  
  60.          digitalWrite(led, HIGH);
  61.          i=1;
  62.       }
  63.       else if (i==1){
  64.          digitalWrite(led, LOW);
  65.          i=0;
  66.       }
  67.       state='n';
  68.     }
  69.     else if (state == 'F'){
  70.       if (j==0){  
  71.         digitalWrite(clacson, HIGH);
  72.          j=1;
  73.       }
  74.       else if (j==1){
  75.          digitalWrite(clacson, LOW);
  76.          j=0;
  77.       }
  78.       state='n';  
  79.     }
  80.     else if (state == 'G'){
  81.         analogWrite(motorA1, 0);  
  82.         analogWrite(motorA2, 0);
  83.         analogWrite(motorB1, 0);  
  84.         analogWrite(motorB2, 0);
  85.         digitalWrite(clacson, LOW);
  86.     }  
  87. }
  88. void Stop() {
  89.   analogWrite(motorA1, 0);  
  90.   analogWrite(motorA2, 0);
  91.   analogWrite(motorB1, 0);
  92.   analogWrite(motorB2, 0);
  93. }
RAW Paste Data