Advertisement
STANAANDREY

tank robot

Jul 22nd, 2023 (edited)
997
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.57 KB | None | 0 0
  1.  
  2. //typedef unsigned char uchar
  3.  
  4. struct {
  5.   char get() {
  6.     char ch = 0;
  7.     if(Serial.available()) {
  8.       ch = Serial.read();
  9.     }
  10.     return ch;
  11.   }
  12. } bluetooth;
  13.  
  14. struct Mover{
  15.   static const int LEFT_DIR_PIN = 2;
  16.   static const int LEFT_POWER_PIN = 5;
  17.   static const int RIGHT_DIR_PIN = 4;
  18.   static const int RIGHT_POWER_PIN = 6;
  19.   void init() {
  20.     //right
  21.     pinMode(RIGHT_DIR_PIN, OUTPUT);
  22.     pinMode(RIGHT_POWER_PIN, OUTPUT);
  23.     analogWrite(RIGHT_POWER_PIN, 0);
  24.     //left
  25.     pinMode(LEFT_DIR_PIN, OUTPUT);
  26.     pinMode(LEFT_POWER_PIN, OUTPUT);
  27.     analogWrite(LEFT_POWER_PIN, 0);
  28.   }
  29.   void rotateRight(int speed) {
  30.     digitalWrite(RIGHT_DIR_PIN, LOW);
  31.     analogWrite(RIGHT_POWER_PIN, speed);
  32.   }
  33.   void rotateLeft(int speed) {
  34.     digitalWrite(LEFT_DIR_PIN, HIGH);
  35.     analogWrite(LEFT_POWER_PIN, speed);
  36.   }
  37.   void moveForward(int speed) {
  38.     digitalWrite(RIGHT_DIR_PIN, HIGH);
  39.     digitalWrite(LEFT_DIR_PIN, LOW);
  40.     analogWrite(RIGHT_POWER_PIN, speed);
  41.     analogWrite(LEFT_POWER_PIN, speed);
  42.   }
  43.   void moveBack(int speed) {
  44.     digitalWrite(RIGHT_DIR_PIN, LOW);
  45.     digitalWrite(LEFT_DIR_PIN, HIGH);
  46.     analogWrite(RIGHT_POWER_PIN, speed);
  47.     analogWrite(LEFT_POWER_PIN, speed);
  48.   }
  49.   void stop() {
  50.     analogWrite(RIGHT_POWER_PIN, 0);
  51.     analogWrite(LEFT_POWER_PIN, 0);
  52.   }
  53. };
  54.  
  55. struct Driver {
  56.   char lastCalled;
  57.   Mover mover;
  58.   static const int MAX_SPEED = 255;
  59.   static const int MIN_SPEED = 195;
  60.   static const int SPEED_DELTA = 15;
  61.   int speed = MIN_SPEED;
  62.   bool activated;
  63.  
  64.   Driver() {
  65.     lastCalled = 0;
  66.     mover.init();
  67.     activated = true;
  68.   }
  69.  
  70.   void drive(char ch) {
  71.     switch(ch) {
  72.       case 'i':
  73.         speed = speed == MAX_SPEED ? speed : speed + SPEED_DELTA;
  74.         drive(lastCalled);
  75.         break;
  76.       case 'd':
  77.         speed = speed == MIN_SPEED ? speed : speed - SPEED_DELTA;
  78.         drive(lastCalled);
  79.         break;
  80.       case 'l':
  81.         lastCalled = ch;
  82.         mover.rotateLeft(speed);
  83.         break;
  84.       case 'r':
  85.         lastCalled = ch;
  86.         mover.rotateRight(speed);
  87.         break;
  88.       case 'f':
  89.         lastCalled = ch;
  90.         mover.moveForward(speed);
  91.         break;
  92.       case 'b':
  93.         lastCalled = ch;
  94.         mover.moveBack(speed);
  95.         break;
  96.       case 's':
  97.         lastCalled = ch;
  98.         mover.stop();
  99.         break;
  100.       default:
  101.         break;
  102.     }
  103.   }
  104. } driver;
  105.  
  106. void setup() {
  107.   Serial.begin(9600);
  108. }
  109.  
  110. void loop() {
  111.   char ch;
  112.   if (ch = bluetooth.get()) {
  113.     Serial.println(ch);
  114.   }
  115.   driver.drive(ch);//*/
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement