When designing a basic program to test an H bridge I built, however, when using analogWrite to control the speed of my motor, the code locks and the outputs won’t turn off. From doing some research this appears to have been a problem with the Leonardo as well. Something about the basic Arduino code not cancelling the PWM when digitalWrite is called.
~working code~
int gogo;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(A0, OUTPUT);
pinMode(A1, OUTPUT);
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
pinMode(A16, INPUT_PULLUP);
pinMode(A15, INPUT_PULLUP);
pinMode(A14, INPUT_PULLUP);
}
void loop() {
//free spinning, all MOSFET off. Clear any stray code since I don't know what I'm doing.
digitalWrite(A3, LOW);
digitalWrite(A2, LOW);
digitalWrite(A1, LOW);
digitalWrite(A0, LOW);
gogo = analogRead(A5)/4;
Serial.print("Throttle is set to ");
Serial.print(gogo);
Serial.println(" of 255.");
//brake
if (!digitalRead(16)){
digitalWrite(A3, LOW);
digitalWrite(A1, LOW);
digitalWrite(A2, HIGH);
digitalWrite(A0, HIGH);
}
//clockwise rotation
if (!digitalRead(14)){
digitalWrite(A2, LOW);
digitalWrite(A1, LOW);
digitalWrite(A3, HIGH);
digitalWrite(A0, HIGH);
}
//counter clockwise rotation
if (!digitalRead(15)){
digitalWrite(A3, LOW);
digitalWrite(A0, LOW);
digitalWrite(A2, HIGH);
digitalWrite(A1, HIGH);
}
delay(5);
}
~end working code~
~code that should work but doesn't?~
int gogo;
void setup() {
Serial.begin(9600);
pinMode(A0, OUTPUT);
pinMode(A1, OUTPUT);
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
pinMode(A16, INPUT_PULLUP);
pinMode(A15, INPUT_PULLUP);
pinMode(A14, INPUT_PULLUP);
}
void loop() {
digitalWrite(A3, LOW);
digitalWrite(A2, LOW);
digitalWrite(A1, LOW);
digitalWrite(A0, LOW);
gogo = analogRead(A5)/4;
Serial.print("Throttle is set to ");
Serial.print(gogo);
Serial.println(" of 255.");
if (!digitalRead(16)){
digitalWrite(A3, LOW);
digitalWrite(A1, LOW);
digitalWrite(A2, HIGH);
digitalWrite(A0, HIGH);
}
if (!digitalRead(14)){
digitalWrite(A2, LOW);
digitalWrite(A1, LOW);
analogWrite(A3, gogo);
analogWrite(A0, gogo);
}
if (!digitalRead(15)){
digitalWrite(A3, LOW);
digitalWrite(A0, LOW);
analogWrite(A2, gogo);
analogWrite(A1, gogo);
}
delay(5);
}
~end sad code~
If it is indeed my code that is just wrong, my apologies. But I do believe this has to do with the way the Artemis handles analogWrite.