I’m trying to get my PWM-control working. Here’s my code:
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;
int start = 2;
int inputValue = 1;
int pwmM1 = 5;
int pwmM2 = 6;
int dir1M1 = 4;
int dir2M1 = 3;
int dir1M2 = 8;
int dir2M2 = 9;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
pinMode(start, INPUT);
pinMode(pwmM1, OUTPUT);
pinMode(pwmM2, OUTPUT);
pinMode(dir1M1, OUTPUT);
pinMode(dir2M1, OUTPUT);
pinMode(dir1M2, OUTPUT);
pinMode(dir2M2, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
delay(5000);
while(1){
digitalWrite(dir1M1, LOW);
digitalWrite(dir2M1, HIGH);
analogWrite(pwmM1, 255);
delay(1000);
digitalWrite(dir2M1, LOW);
digitalWrite(dir1M1, HIGH);
delay(1000);
}
}
The idea is that after 5 seconds, one motor should start rotating in one direction for 1 second and then change direction and rotate for another second and then repeat. In reality it does whatever the hell it wants. It starts out fine the first cycle but then the motor mostly rotate in the same direction all the time but not always. Can someone please tell me what the hell is going on?