0

I tried using this code to output AC power from one H-bridge and then another, switching every second, but when I connected a motor to it, it just seemed to output direct current. This is the code I was using (by the way, the motor controller I'm using has a max frequency of 20 kHz, so that's not the problem as far as I know):

int EnablePin = 8;
int duty;
int PWMPinA;
int PWMPinB;

const byte CPin = 0;  // analog input channel
int CRaw;      // raw A/D value
float CVal;    // adjusted Amps value

void setup() {                
  pinMode(EnablePin, OUTPUT);     
  pinMode(PWMPinA, OUTPUT);
  pinMode(PWMPinB, OUTPUT);
}

void loop() {

  digitalWrite(EnablePin, HIGH);

  for(duty = 0; duty <= 20000; duty += 1){

    PWMPinA=11;  // Timer2
    PWMPinB=3;
    analogWrite(PWMPinA, 255);
    analogWrite(PWMPinB, 0);
    delayMicroseconds(50);

    if(PWMPinA == 11) {
      PWMPinA = 3;
      PWMPinB = 11;
    } else {
      PWMPinA = 11;
      PWMPinB = 3;
    }

  }
  for(duty = 20000; duty>=0; duty -= 1){

    PWMPinA=6;  // Timer2
    PWMPinB=5;
    analogWrite(PWMPinA, 255);
    analogWrite(PWMPinB, 0);
    delayMicroseconds(50); 

    if(PWMPinA == 6) {
      PWMPinA = 5;
      PWMPinB = 6;
    } else {
      PWMPinA = 6;
      PWMPinB = 5;
    }
  }
}
  • ..... Your outputing PWM signals to 4 difference pins? its very confusing to be changing the pin assigned to a variable rather then having them all defined and changing which one you are using. Need to post a schematic of what you have done if you want any help – Chad G Dec 7 '18 at 22:06
  • 1
    I tried that and it seems to work. Don't know why I didn't think of that considering it's simpler, but thank you! – Tom Dec 7 '18 at 22:34
0

Sounds like you have it sorted already but the first major problem with your original code is that you use PwmPinA and PwmPinB in setup(), before they are assigned any values.

I'd be surprised if the compiler didn't warn about that. Never ignore compiler warnings! :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.