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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
#define motorLeftAPin 6 #define motorLeftBPin 5 #define motorRightAPin 9 #define motorRightBPin 3 char pre; void setup() { Serial.begin(9600); analogWrite(motorLeftAPin, 0); analogWrite(motorLeftBPin, 0); analogWrite(motorRightAPin, 0); analogWrite(motorRightBPin, 0); } void motors(char RL, int FSB) { if (RL=='R') { switch (FSB) { case 2: // 고속전진 analogWrite(motorRightBPin, 0); analogWrite(motorRightAPin, 255); break; case 1: // 저속전진 analogWrite(motorRightBPin, 0); analogWrite(motorRightAPin, 150); break; case 0: // 정지 analogWrite(motorRightAPin, 0); analogWrite(motorRightBPin, 0); break; case -1: // 저속후진 analogWrite(motorRightAPin, 0); analogWrite(motorRightBPin, 150); break; case -2: // 고속후진 analogWrite(motorRightAPin, 0); analogWrite(motorRightBPin, 255); break; } } else if (RL=='L') { switch (FSB) { case 2: // 고속전진 analogWrite(motorLeftAPin, 0); analogWrite(motorLeftBPin, 255); break; case 1: // 저속전진 analogWrite(motorLeftAPin, 0); analogWrite(motorLeftBPin, 150); break; case 0: // 정지 analogWrite(motorLeftAPin, 0); analogWrite(motorLeftBPin, 0); break; case -1: // 저속후진 analogWrite(motorLeftBPin, 0); analogWrite(motorLeftAPin, 150); break; case -2: // 고속후진 analogWrite(motorLeftBPin, 0); analogWrite(motorLeftAPin, 255); break; } } } void loop() { char chr; if (Serial.available() > 0) { chr=Serial.read(); switch (chr) { case '1': motors('L', 1); motors('R', 2); break; case '2': if (pre=='2') { motors('L', 2); motors('R', 2); } else { motors('L', 1); motors('R', 1); } break; case '3': motors('L', 2); motors('R', 1); break; case '4': if (pre=='4') { motors('L', -2); motors('R', 2); } else { motors('L', -1); motors('R', 1); } break; case '5': motors('L', 0); motors('R', 0); break; case '6': if (pre=='4') { motors('L', 2); motors('R', -2); } else { motors('L', 1); motors('R', -1); } break; case '7': motors('L', -1); motors('R', -2); break; case '8': if (pre=='8') { motors('L', -2); motors('R', -2); } else { motors('L', -1); motors('R', -1); } break; case '9': motors('L', -2); motors('R', -1); break; } pre=chr; } } |
※ 수업용 자료입니다. (redcrow@naver.com)