Study/Arduino
Arduino PWM Control
차희빈
2014. 8. 30. 17:33
시리얼통신 프로그램
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 | int pwmOut = 9; int pwmDuty = 0; int cnt = 10; char ch=0; void setup() { // user mode: PWM, Serial, Serial.begin(9600); pinMode(pwmOut, OUTPUT); Serial.print("Connect Success! input Number 1, 2, 3"); analogWrite(pwmOut, pwmDuty); } void loop() { if(Serial.available()){ ch=Serial.read(); } if(ch == '1') { pwmDuty-=cnt;Serial.println(pwmDuty); analogWrite(pwmOut, pwmDuty); ch=0; if( (pwmDuty <= 0) && (ch != '3')) { pwmDuty = 0; Serial.println("Min Speed"); } } else if(ch == '2') { pwmDuty+=cnt;Serial.println(pwmDuty); analogWrite(pwmOut, pwmDuty); ch=0; if(pwmDuty >= 255) { pwmDuty=255; Serial.println("Max Speed"); } } else if(ch == '3') {pwmDuty=0;analogWrite(pwmOut, pwmDuty);ch=0;} else if( ( (ch=='1') && (ch=='2') && (ch=='3') )&& 1 ) Serial.println("try again! (Number 1, 2, 3)"); } |
사용한 Arduino UNO
Serial 통신으로 1전송: PWM Duty 감소
2전송: PWM Duty 증가
3전송: PWM Dyty 0
그외 전송받으면 try again 송출