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 |
#define motorLeftAPin 6 #define motorLeftBPin 5 #define motorRightAPin 9 #define motorRightBPin 3 void setup() { Serial.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 (Serial.available() > 0) { chr=Serial.read(); switch (chr) { case '1': motors('L', 0); motors('R', 1); break; case '2': motors('L', 1); motors('R', 1); break; case '3': motors('L', 1); motors('R', 0); break; case '4': motors('L', -1); motors('R', 1) break; case '5': motors('L', 0); motors('R', 0); break; case '6': motors('L', 1); motors('R', -1); break; case '7': motors('L', 0); motors('R', -1); break; case '8': motors('L', -1); motors('R', -1); break; case '9': motors('L', -1); motors('R', 0); break; } } } |
※ 수업용 자료입니다. (redcrow@naver.com)