|
| 1 | +const int motorIn1 = 2; |
| 2 | +const int motorIn2 = 3; |
| 3 | +const int motorIn3 = 4; |
| 4 | +const int motorIn4 = 5; |
| 5 | + |
| 6 | +String dpadLeft_press = "left|1"; |
| 7 | +String dpadLeft_release = "left|0"; |
| 8 | +String dpadUp_press = "up|1"; |
| 9 | +String dpadUp_release = "up|0"; |
| 10 | +String dpadRight_press = "right|1"; |
| 11 | +String dpadRight_release = "right|0"; |
| 12 | +String dpadDown_press = "down|1"; |
| 13 | +String dpadDown_release = "down|0"; |
| 14 | + |
| 15 | +const int DELAY = 1000; |
| 16 | + |
| 17 | +void setup() |
| 18 | +{ |
| 19 | + Serial1.begin(57600); |
| 20 | + Serial.begin(9600); |
| 21 | + pinMode(motorIn1, OUTPUT); |
| 22 | + pinMode(motorIn2, OUTPUT); |
| 23 | + pinMode(motorIn3, OUTPUT); |
| 24 | + pinMode(motorIn4, OUTPUT); |
| 25 | +} |
| 26 | + |
| 27 | +void loop() |
| 28 | +{ |
| 29 | + String command=""; |
| 30 | + while (Serial1.available()) { |
| 31 | + int v = Serial1.read(); |
| 32 | + Serial.println(v, DEC); |
| 33 | + if ( v != -1) { |
| 34 | + command += (char)v; |
| 35 | + Serial.println(command); |
| 36 | + if (command.equals(dpadLeft_press)) { |
| 37 | + left(); |
| 38 | + delay(DELAY); |
| 39 | + command = ""; |
| 40 | + } else if (command.equals(dpadLeft_release)) { |
| 41 | + motorstop(); |
| 42 | + command = ""; |
| 43 | + delay(500); |
| 44 | + } else if (command.equals(dpadUp_press)) { |
| 45 | + forward(); |
| 46 | + command = ""; |
| 47 | + delay(DELAY); |
| 48 | + } else if (command.equals(dpadUp_release)) { |
| 49 | + motorstop(); |
| 50 | + command = ""; |
| 51 | + delay(500); |
| 52 | + } else if (command.equals(dpadRight_press)) { |
| 53 | + right(); |
| 54 | + command = ""; |
| 55 | + delay(DELAY); |
| 56 | + } else if (command.equals(dpadRight_release)) { |
| 57 | + motorstop(); |
| 58 | + command = ""; |
| 59 | + delay(500); |
| 60 | + } else if (command.equals(dpadDown_press)) { |
| 61 | + backward(); |
| 62 | + command = ""; |
| 63 | + delay(DELAY); |
| 64 | + } else if (command.equals(dpadDown_release)) { |
| 65 | + motorstop(); |
| 66 | + command = ""; |
| 67 | + delay(500); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +void motorstop() |
| 74 | +{ |
| 75 | + digitalWrite(motorIn1, LOW); |
| 76 | + digitalWrite(motorIn2, LOW); |
| 77 | + digitalWrite(motorIn3, LOW); |
| 78 | + digitalWrite(motorIn4, LOW); |
| 79 | +} |
| 80 | + |
| 81 | +void forward() |
| 82 | +{ |
| 83 | + digitalWrite(motorIn1, HIGH); |
| 84 | + digitalWrite(motorIn2, LOW); |
| 85 | + digitalWrite(motorIn3, HIGH); |
| 86 | + digitalWrite(motorIn4, LOW); |
| 87 | +} |
| 88 | + |
| 89 | +void backward() |
| 90 | +{ |
| 91 | + digitalWrite(motorIn1, LOW); |
| 92 | + digitalWrite(motorIn2, HIGH); |
| 93 | + digitalWrite(motorIn3, LOW); |
| 94 | + digitalWrite(motorIn4, HIGH); |
| 95 | +} |
| 96 | + |
| 97 | +// Let right motor keep running, but stop left motor |
| 98 | +void right() |
| 99 | +{ |
| 100 | + digitalWrite(motorIn1, HIGH); |
| 101 | + digitalWrite(motorIn2, LOW); |
| 102 | + digitalWrite(motorIn3, LOW); |
| 103 | + digitalWrite(motorIn4, LOW); |
| 104 | +} |
| 105 | + |
| 106 | +// Let left motor keep running, but stop right motor |
| 107 | +void left() |
| 108 | +{ |
| 109 | + digitalWrite(motorIn1, LOW); |
| 110 | + digitalWrite(motorIn2, LOW); |
| 111 | + digitalWrite(motorIn3, HIGH); |
| 112 | + digitalWrite(motorIn4, LOW); |
| 113 | +} |
0 commit comments