硬件準(zhǔn)備
震動(dòng)傳感器:1個(gè)
紅黃綠LED燈:各一個(gè)
旋鈕電位器:1個(gè)
直流電機(jī):1個(gè)
1K電阻:1個(gè)
220歐電阻:3個(gè)
杜邦線:若干文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-722972.html
硬件連線
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-722972.html
軟件程序
const int analogInPin = A0;//PWM輸入引腳
const int analogOutPin = 3;//PWM輸出引腳
const int vibPin = 4;//震動(dòng)傳感器
const int RLed = 10;
const int YLed = 9;
const int GLed = 8;
int sensorValue = 0;//電位器電壓值
int outputValue = 0;//模擬量輸出裝(PWM)
int vibValue = 0;//震動(dòng)傳感器值
void setup() {
Serial.begin(9600);
pinMode(analogOutPin,OUTPUT);
pinMode(RLed,OUTPUT);
pinMode(YLed,OUTPUT);
pinMode(GLed,OUTPUT);
}
void loop() {
//讀取模擬值
sensorValue = analogRead(analogInPin);
vibValue = digitalRead(vibPin);
//變換數(shù)據(jù)區(qū)間
outputValue = map(sensorValue, 0, 1023, 0, 255);
//判斷速度,相應(yīng)的燈亮
if (outputValue < 110) {
digitalWrite(GLed, HIGH);
digitalWrite(YLed, LOW);
digitalWrite(RLed, LOW);
Serial.println("綠燈亮");
delay(1000);
} else if (outputValue < 180) {
digitalWrite(GLed, LOW);
digitalWrite(YLed, HIGH);
digitalWrite(RLed, LOW);
Serial.println("黃燈亮");
delay(1000);
} else if (outputValue < 255) {
digitalWrite(GLed, LOW);
digitalWrite(YLed, LOW);
digitalWrite(RLed, HIGH);
Serial.println("紅燈亮");
delay(1000);
}else{
Serial.println("都不亮");
}
Serial.print("震動(dòng)值:");
Serial.println(vibValue);
//輸出對(duì)應(yīng)的PWM值
if (vibValue == 1) {
analogWrite(analogOutPin, outputValue);
Serial.println("電機(jī)轉(zhuǎn)動(dòng)");
}
else {
analogWrite(analogOutPin, 0);
}
//打印結(jié)果到串口監(jiān)視器
Serial.print("sensor = ");
Serial.println(sensorValue);
Serial.print("output = ");
Serial.println(outputValue);
Serial.println("");
delay(2);
}
到了這里,關(guān)于【Arduino32】PWM控制直流電機(jī)速度的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!