藉由手作荷蘭風車,馬達的部分,以 Arduino晶片控制,搭配按鈕開關,來起閉風車轉動。
其中:
接腳3:偵測按鈕是否按下(高低電位偵測藉以判斷按鈕是否按下),當按下按鈕,高電位;放開按鈕,低電位。
接腳5、6:與L9110馬達驅動板的 A1A、A1B接點連結。
啟動時,因為採用PWM時Arduino會產生額外的低頻噪音,為消除噪音,在程式中,改變計時器的設定。
因為接腳5、6分屬Timer0管轄,在void setup()中增加:
TCCR0A = _BV(COM0A1) | _BV(COM0B1) | _BV(WGM01) | _BV(WGM00);
TCCR0B = _BV(CS00);
OCR0A = 180;
OCR0B = 50;
上開程式碼以紅色色標之處為Timer0的0代號。
如果是採用3、11接腳,則為Timer 2;採用9、10接腳,則為Timer 1。
附上Arduino程式碼:
#define A_1A 6
#define A_1B 5
int button_pin=3;
int i=1;
void setup() {
// put your setup code here, to run once:
TCCR0A = _BV(COM0A1) | _BV(COM0B1) | _BV(WGM01) | _BV(WGM00);
TCCR0B = _BV(CS00);
OCR0A = 180;
OCR0B = 50;
pinMode(button_pin,INPUT);
pinMode(A_1A,OUTPUT);
pinMode(A_1B,OUTPUT);
pinMode(13,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
//analogWrite(A_1A,130);
//analogWrite(A_1B,0);
//delay(100);
if (digitalRead(button_pin)==HIGH) {
i=i+1;
while(digitalRead(button_pin)==HIGH){}
}
if (digitalRead(button_pin)==LOW && i%3==0) {
digitalWrite(13,HIGH);
analogWrite(A_1A,150);
digitalWrite(A_1B,0);
delay(20);
}
if (digitalRead(button_pin)==LOW && i%3==1) {
digitalWrite(13,LOW);
analogWrite(A_1A,255);
digitalWrite(A_1B,0);
delay(20);
}
if (digitalRead(button_pin)==LOW && i%3==2) {
digitalWrite(13,LOW);
digitalWrite(A_1A,0);
digitalWrite(A_1B,0);
delay(20);
}
}