Stap 7: Controle van de snelheid van de motoren met een Arduino
Schets hieronder controle van de snelheid van twee DC-motoren.
Verwijder de jumper van ENA & ENB, ENA & ENB is dan aangesloten op de digitale 9 en digitale 10 respectievelijk.
#define FORWARD 8 #define BACKWARD 2 #define STOP 5 #define PWM_PIN_1 9 //speed control pin for motor1 #define PWM_PIN_2 10 ///speed control pin for motor2 int Motor[2][2] = //two dimensional array { {4 , 5}, //input pin to control Motor1--> Motor[0][0]=4, Motor[0][1]=5 {6 , 7}, //input pin to control Motor2--> Motor[1][0]=6, Motor[1][1]=7 }; void setup() { pinMode(PWM_PIN_1, OUTPUT); pinMode(PWM_PIN_2, OUTPUT); pinMode(Motor[0][0], OUTPUT); pinMode(Motor[0][1], OUTPUT); pinMode(Motor[1][0], OUTPUT); pinMode(Motor[1][1], OUTPUT); motor_run(0, FORWARD); //motor1 forward motor_run(1, BACKWARD); //motor2 backward } void loop() { //decelerate for (int motor_speed=255; motor_speed>=0; motor_speed--) { analogWrite(PWM_PIN_1, motor_speed); analogWrite(PWM_PIN_2, motor_speed); delay(20); //delay 20 milliseconds } //accelerate for (int motor_speed=0; motor_speed<=255; motor_speed++) { analogWrite(PWM_PIN_1, motor_speed); analogWrite(PWM_PIN_2, motor_speed); delay(20); //delay 20 milliseconds } } void motor_run(int motor, int movement) { switch (movement) { case FORWARD: digitalWrite(Motor[motor][0], HIGH); digitalWrite(Motor[motor][1], LOW); break; case BACKWARD: digitalWrite(Motor[motor][0], LOW); digitalWrite(Motor[motor][1], HIGH);; break; case STOP: digitalWrite(Motor[motor][0], LOW); digitalWrite(Motor[motor][1], LOW);; break; } }