SHARE
TWEET

Tank.ino

a guest Jan 20th, 2020 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <IRremote.h>
  2.  
  3. // PIN Setup
  4. #define IR_IN_PIN 2
  5. #define MOTOR_SPEED 11
  6. #define MOTOR_IN_1 13
  7. #define MOTOR_IN_2 12
  8.  
  9. // IR Setup
  10. IRrecv irrecv(IR_IN_PIN);
  11. decode_results results;
  12.  
  13. // State
  14. unsigned long latestInput = 0;
  15. unsigned long lastButtonInput = 0;
  16.  
  17. void setup() {
  18.     irSetup();
  19.     dcMotorSetup();
  20. }
  21.  
  22. void loop(){
  23.     if(irrecv.decode(&results)) {
  24.         switch(results.value){
  25.             case WILDCARD:
  26.                 results.value = lastButtonInput;
  27.                 break;
  28.             case GO_FORWARD:
  29.                 dcMotorMove(FORWARD);
  30.                 lastButtonInput = GO_FORWARD;
  31.                 break;
  32.             case GO_BACKWARD:
  33.                 dcMotorMove(BACKWARD);
  34.                 lastButtonInput = GO_BACKWARD;
  35.                 break;
  36.         }
  37.  
  38.         delay(30);
  39.         irrecv.resume();
  40.     } else {
  41.         if(latestInput || lastButtonInput) {
  42.             dcMotorStop();
  43.             latestInput = 0;
  44.             lastButtonInput = 0;
  45.         }
  46.     }
  47. }
  48.  
  49. void irSetup() {
  50.     irrecv.enableIRIn();
  51.     irrecv.blink13(true);
  52. }
  53.  
  54. void dcMotorSetup() {
  55.     pinMode(MOTOR_SPEED, OUTPUT);
  56.     pinMode(MOTOR_IN_1, OUTPUT);
  57.     pinMode(MOTOR_IN_2, OUTPUT);
  58. }
  59.  
  60. void dcMotorMove(int direction) {
  61.     switch(direction) {
  62.         case FORWARD:
  63.             digitalWrite(MOTOR_IN_1, HIGH);
  64.             digitalWrite(MOTOR_IN_2, LOW);
  65.             break;
  66.         case BACKWARD:
  67.             digitalWrite(MOTOR_IN_1, LOW);
  68.             digitalWrite(MOTOR_IN_2, HIGH);
  69.             break;
  70.     }
  71.     dcMotorAccelerate();
  72. }
  73.  
  74. void dcMotorAccelerate(int min_speed = 0, int max_speed = 255, int interval = 20) {
  75.     for (int i = min_speed; i =< max_speed; ++i) {
  76.         analogWrite(MOTOR_SPEED, i);
  77.         delay(interval);
  78.     }
  79. }
  80.  
  81. void dcMotorDecelerate(int min_speed = 0, int max_speed = 255, int interval = 20) {
  82.     for (int i = max_speed; i >= min_speed; i++) {
  83.         analogWrite(MOTOR_SPEED, i);
  84.         delay(interval);
  85.     }
  86. }
  87.  
  88. void dcMotorStop() {
  89.     dcMotorDecelerate();
  90.     digitalWrite(MOTOR_IN_1, LOW);
  91.     digitalWrite(MOTOR_IN_2, LOW);
  92. }
  93.  
  94. enum Moves {
  95.     GO_FORWARD = 0xFF629D,
  96.     GO_BACKWARD = 0xFFA857,
  97.     TURN_RIGHT = 0xFFC23D,
  98.     TURN_LEFT = 0xFF22DD,
  99.     TURN_RIGHT_90 = 0xFFE21D,
  100.     TURN_LEFT_90 = 0xFFA25D,
  101.     TURN_180 = 0xFF02FD,
  102.     WILDCARD = 0xFFFFFFFF
  103. }
  104.  
  105. enum Direction {
  106.     FORWARD,
  107.     BACKWARD
  108. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top