SparkFun Forums 

Where electronics enthusiasts find answers.

What are we doing right with Artemis? What are we doing wrong? Let us know here!
User avatar
By rhysonce
#217393
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~
Code: Select all
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.
User avatar
By liquid.soulder
#217612
Hmmm - digitalWrite does indeed not disable pwm output. This is because PWM output comes from a different peripheral. I think you need to reconfigure the pin to a normal digital output with pinMode(pin, OUTPUT); when you want it to stop being PWM
 Topic permissions

You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

long long title how many chars? lets see 123 ok more? yes 60

We have created lots of YouTube videos just so you can achieve [...]

Another post test yes yes yes or no, maybe ni? :-/

The best flat phpBB theme around. Period. Fine craftmanship and [...]

Do you need a super MOD? Well here it is. chew on this

All you need is right here. Content tag, SEO, listing, Pizza and spaghetti [...]

Lasagna on me this time ok? I got plenty of cash

this should be fantastic. but what about links,images, bbcodes etc etc? [...]