mpmcws

4WD Car Base

Jun 15th, 2018
124
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   Software open source
  3.   by Marco C.
  4.   maggio 2018
  5.   4WD Car
  6. */
  7. const int motorA1  = 5;
  8. const int motorA2  = 6;
  9. const int motorB1  = 10;
  10. const int motorB2  = 11;
  11. int state;
  12. int vSpeed = 200;   // la velocità va da 0 a 255
  13. void setup() {
  14.   pinMode(motorA1, OUTPUT);
  15.   pinMode(motorA2, OUTPUT);
  16.   pinMode(motorB1, OUTPUT);
  17.   pinMode(motorB2, OUTPUT);
  18.   Serial.begin(9600);
  19. }
  20. void loop() {
  21.   if (Serial.available() > 0) {
  22.     state = Serial.read();
  23.     Stop();
  24.   }
  25.   //AVANTI
  26.   //se ricevo la lettera A
  27.   if (state == 'A') {
  28.     analogWrite(motorA1, vSpeed);
  29.     analogWrite(motorA2, 0);
  30.     analogWrite(motorB1, vSpeed);
  31.     analogWrite(motorB2, 0);
  32.   }
  33.   //INDIETRO
  34.   else if (state == 'B') {
  35.     analogWrite(motorA1, 0);
  36.     analogWrite(motorA2, vSpeed);
  37.     analogWrite(motorB1, 0);
  38.     analogWrite(motorB2, vSpeed);
  39.   }
  40.   //SINISTRA
  41.   else if (state == 'C') {
  42.     analogWrite(motorA1, 200);
  43.     analogWrite(motorA2, 0);
  44.     analogWrite(motorB1, 0);
  45.     analogWrite(motorB2, 200);
  46.   }
  47.   //DESTRA
  48.   else if (state == 'D') {
  49.     analogWrite(motorA1, 0);
  50.     analogWrite(motorA2, 200);
  51.     analogWrite(motorB1, 200);
  52.     analogWrite(motorB2, 0);
  53.   }
  54.   //STOP
  55.   else if (state == 'E') {
  56.     analogWrite(motorA1, 0);
  57.     analogWrite(motorA2, 0);
  58.     analogWrite(motorB1, 0);
  59.     analogWrite(motorB2, 0);
  60.   }
  61. }
  62. void Stop() {
  63.   analogWrite(motorA1, 0);
  64.   analogWrite(motorA2, 0);
  65.   analogWrite(motorB1, 0);
  66.   analogWrite(motorB2, 0);
  67. }
RAW Paste Data