1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
#include <SoftwareSerial.h> SoftwareSerial BTSerial(2,3); #define motorRightAPin 6 #define motorRightBPin 5 #define motorLeftAPin 9 #define motorLeftBPin 10 void setup() { Serial.begin(9600); BTSerial.begin(9600); pinMode(motorLeftAPin, OUTPUT); pinMode(motorLeftBPin, OUTPUT); pinMode(motorRightAPin, OUTPUT); pinMode(motorRightBPin, OUTPUT); digitalWrite(motorLeftAPin, LOW); digitalWrite(motorLeftBPin, LOW); digitalWrite(motorRightAPin, LOW); digitalWrite(motorRightBPin, LOW); } void motors(char RL, int FSB) { if (RL=='R') { switch (FSB) { case 1: // 전진 digitalWrite(motorRightBPin, 0); digitalWrite(motorRightAPin, 1); break; case 0: // 정지 digitalWrite(motorRightAPin, 0); digitalWrite(motorRightBPin, 0); break; case -1: // 후진 digitalWrite(motorRightAPin, 0); digitalWrite(motorRightBPin, 1); break; } } else if (RL=='L') { switch (FSB) { case 1: // 전진 digitalWrite(motorLeftAPin, 0); digitalWrite(motorLeftBPin, 1); break; case 0: // 정지 digitalWrite(motorLeftAPin, 0); digitalWrite(motorLeftBPin, 0); break; case -1: // 후진 digitalWrite(motorLeftBPin, 0); digitalWrite(motorLeftAPin, 1); break; } } } void loop() { char chr; if (BTSerial.available() > 0) { chr=BTSerial.read(); switch (chr) { case '1': case 'q': motors('L', 0); motors('R', 1); break; case '2': case 'w': motors('L', 1); motors('R', 1); break; case '3': case 'e': motors('L', 1); motors('R', 0); break; case '4': case 'a': motors('L', -1); motors('R', 1); break; case '5': case 'A': motors('L', 0); motors('R', 0); break; case '6': case 'd': motors('L', 1); motors('R', -1); break; case '7': case 'E': motors('L', 0); motors('R', -1); break; case '8': case 'x': motors('L', -1); motors('R', -1); break; case '9': case 'B': motors('L', -1); motors('R', 0); break; } } } |