zed_com

byter middle

Feb 27th, 2021
781
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SoftwareSerial.h>
  2.  
  3. SoftwareSerial bt(2,3); // rx, tx
  4.  
  5. int sp = 255;
  6.  
  7. #define pwma 5
  8. #define pwmb 9
  9.  
  10. #define ain2 4 // Right motor AO1 +
  11. #define ain1 6 // Right motor AO2 -
  12. #define bin1 7 // Left motor BO1 +
  13. #define bin2 8 // Left motor BO2 -
  14.  
  15. void setup() {
  16.   pinMode(pwma, OUTPUT);
  17.   pinMode(pwmb, OUTPUT);
  18.   pinMode(ain2, OUTPUT);
  19.   pinMode(ain1, OUTPUT);
  20.   pinMode(bin1, OUTPUT);
  21.   pinMode(bin2, OUTPUT);
  22.  
  23.   analogWrite(pwma, sp);
  24.   analogWrite(pwmb, sp);
  25.  
  26.   bt.begin(9600);
  27.   Serial.begin(9600);
  28. }
  29. void loop () {
  30.   if (bt.available() > LOW) {
  31.    
  32.     char cmd = bt.read();
  33.     Serial.println(cmd);
  34.     switch (cmd) {
  35.       case 'F':
  36.         forward();
  37.         break;
  38.       case 'B':
  39.         backward();
  40.         break;
  41.       case 'L':
  42.         left();
  43.         break;
  44.       case 'R':
  45.         right();
  46.         break;
  47.       case 'G':
  48.         forwardLeft();
  49.         break;
  50.       case 'I':
  51.         forwardRight();
  52.         break;
  53.       case 'H':
  54.         backwardLeft();
  55.         break;
  56.       case 'J':
  57.         backwardRight();
  58.         break;
  59.       case '0':
  60.       case '1':
  61.       case '2':
  62.       case '3':
  63.       case '4':
  64.       case '5':
  65.       case '6':
  66.       case '7':
  67.       case '8':
  68.       case '9':
  69.       case 'q':
  70.         speedByter(cmd);
  71.         break;
  72.       default:
  73.         stopByter();
  74.         break;
  75.     }
  76.   }
  77. }
  78.  
  79. void speedByter(char s) {
  80.   if (s != 'q') {
  81.     int ss = (int)s;
  82.     sp = 255 / 100 * ss;
  83.  
  84.   } else {
  85.     sp = 255;
  86.   }
  87.   analogWrite(pwma, sp);
  88.   analogWrite(pwmb, sp);
  89. }
  90.  
  91. void stopByter() {
  92.   analogWrite(pwma, sp);
  93.   analogWrite(pwmb, sp);
  94.  
  95.   digitalWrite(ain2, LOW);
  96.   digitalWrite(ain1, LOW);
  97.   digitalWrite(bin1, LOW);
  98.   digitalWrite(bin2, LOW);
  99. }
  100. void forward() {
  101.   analogWrite(pwma, sp);
  102.   analogWrite(pwmb, sp);
  103.  
  104.   digitalWrite(ain2, HIGH);
  105.   digitalWrite(ain1, LOW);
  106.   digitalWrite(bin1, LOW);
  107.   digitalWrite(bin2, HIGH);
  108. }
  109. void backward() {
  110.   analogWrite(pwma, sp);
  111.   analogWrite(pwmb, sp);
  112.  
  113.   digitalWrite(ain2, LOW);
  114.   digitalWrite(ain1, HIGH);
  115.   digitalWrite(bin1, HIGH);
  116.   digitalWrite(bin2, LOW);
  117. }
  118. void left() {
  119.   analogWrite(pwma, sp);
  120.   analogWrite(pwmb, sp);
  121.  
  122.   digitalWrite(ain2, LOW);
  123.   digitalWrite(ain1, HIGH);
  124.   digitalWrite(bin1, LOW);
  125.   digitalWrite(bin2, HIGH);
  126. }
  127. void right() {
  128.   analogWrite(pwma, sp);
  129.   analogWrite(pwmb, sp);
  130.  
  131.   digitalWrite(ain2, HIGH);
  132.   digitalWrite(ain1, LOW);
  133.   digitalWrite(bin1, HIGH);
  134.   digitalWrite(bin2, LOW);
  135. }
  136.  
  137. void forwardLeft() {
  138.   analogWrite(pwma, sp / 3);
  139.  
  140.   digitalWrite(ain2, HIGH);
  141.   digitalWrite(ain1, LOW);
  142.   digitalWrite(bin1, LOW);
  143.   digitalWrite(bin2, HIGH);
  144. }
  145.  
  146. void forwardRight() {
  147.   analogWrite(pwmb, sp / 3);
  148.  
  149.   digitalWrite(ain2, HIGH);
  150.   digitalWrite(ain1, LOW);
  151.   digitalWrite(bin1, LOW);
  152.   digitalWrite(bin2, HIGH);
  153. }
  154.  
  155. void backwardLeft() {
  156.   analogWrite(pwmb, sp / 3);
  157.  
  158.   digitalWrite(ain2, LOW);
  159.   digitalWrite(ain1, HIGH);
  160.   digitalWrite(bin1, HIGH);
  161.   digitalWrite(bin2, LOW);
  162. }
  163.  
  164. void backwardRight() {
  165.   analogWrite(pwma, sp / 3);
  166.  
  167.   digitalWrite(ain2, LOW);
  168.   digitalWrite(ain1, HIGH);
  169.   digitalWrite(bin1, HIGH);
  170.   digitalWrite(bin2, LOW);
  171. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×