Arduino 5:馬達控制--使用IRF520控制轉速
延續上一篇的利用IRF520控制1W的LED,並且加上[可變電阻]直接控制轉速
- // named constants for the switch and motor pins
- const int motorPin = 9; // the number of the motor pin
- const int potentialPin= 0; // potential meter
- int value = 0;
- void setup() {
- // initialize the motor pin as an output:
- pinMode(motorPin, OUTPUT);
- // initialize the switch pin as an input:
- // pinMode(potentialPin, INPUT); No need declare Analog
- }
- void loop(){
-
- value = analogRead(potentialPin);
- int intensity = map(value, 0, 1023, 0, 255);
-
- analogWrite( motorPin , intensity);
- }
多加個[開關],只有在按下[開關]時,馬達才會運轉
- // named constants for the switch and motor pins
- const int switchPin = 2; // the number of the switch pin
- const int motorPin = 9; // the number of the motor pin
- const int potentialPin= 0; // potential meter
- int switchState = 0; // variable for reading the switch's status
- void setup() {
- // initialize the motor pin as an output:
- pinMode(motorPin, OUTPUT);
- // initialize the switch pin as an input:
- pinMode(switchPin, INPUT);
- // pinMode(potentialPin, INPUT); No need declare Analog
- }
- void loop(){
- // read the state of the switch value:
- switchState = digitalRead(switchPin);
-
- int value = analogRead(potentialPin);
- int intensity= map(value, 0, 1023, 0,255);
- // check if the switch is pressed.
- if (switchState == HIGH) {
- // turn motor on:
- // digitalWrite(motorPin, HIGH);
- analogWrite(motorPin, intensity);
- }
- else {
- // turn motor off:
- digitalWrite(motorPin, LOW);
- }
- }
沒有留言:
張貼留言