Implementing working program to an if statement

Hey guys,

I have made a program to run a DC motor (6 volt) by a H-Bridge (L298) using PWM. The DC motor program is tested and runs fine. Below is working code

int motor1Pin1_PWM = 9;    // pin 5 on L298
int motor1Pin2 = 8;        // pin 7 on L298
int vss_hbridge = 10;    
int ena_hbridge = 11;

void setup() {

  // set all the other pins you're using as outputs:
  pinMode(motor1Pin1_PWM, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(vss_hbridge, OUTPUT);
  pinMode(ena_hbridge, OUTPUT);

}
void loop() //////////// Running loop
{
  forward();
  delay(10000);
  motor_stop();


  reverse();
  delay(10000);
  motor_stop();
}

//////////////////////////////////
void motor_stop()
{
  digitalWrite(ena_hbridge, LOW);
  delay(500);
}

void forward()
{    //motor will turn in one direction:
  analogWrite(motor1Pin1_PWM, 127);   // set pin 5 on L298 0.5 duty cucle
  digitalWrite(motor1Pin2, LOW);  // set pin 7 on L298 low
  
  digitalWrite(vss_hbridge, HIGH);
  digitalWrite(ena_hbridge, HIGH);
}

void reverse()
{  //motor will turn in the opposite direction:
  analogWrite(motor1Pin1_PWM, 127);   // set pin 5 on L298 0.5 duty cucle
  digitalWrite(motor1Pin2, HIGH);   // set pin 7 on L298 high
  
  digitalWrite(vss_hbridge, HIGH);
  digitalWrite(ena_hbridge, HIGH);
}

the problem is if i call the forward function for the DC motor nothing happens. I am really unsure of what is going on. below is the if code im using. I have called other functions from the same if code block so im sure it isnt syntax or anything silly like that. Im just stumped on this one :-?

else if (incomingByte == 108){
        forward();
        delay(500);
        motor_stop();
        delay(500);
}

any help would be appreciated

Cheers

Michael

You need to show the whole sketch, not just the 'else' clause.

(I've been programming over 30 years, and I've no idea what character 108 is - will you next week?)

sorry

here is the code i have transferred the working DC motor control

#include <Servo.h>      // include the servo library

Servo servoMotor;       // creates an instance of the servo object to control a servo

int servoPin = 6;       // Control pin for servo motor.

int ledPin =  13;    // LED connected to digital pin 13
int incomingByte = 0;      // for incoming serial data

int duty = 102;          //Duty cycle for DC motor (0 to 256 range)
int motor1Pin1_PWM = 9;    // pin 5 on L298
int motor1Pin2 = 8;        // pin 7 on L298
int vss_hbridge = 10;    
int ena_hbridge = 11;

// The setup() method runs once, when the sketch starts

void setup()   {                
  // initialize the digital pin as an output:
  pinMode(ledPin, OUTPUT);
  pinMode(motor1Pin1_PWM, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(vss_hbridge, OUTPUT);
  pinMode(ena_hbridge, OUTPUT);
  // attaches the servo on pin 9 to the servo object
  servoMotor.attach(servoPin);  
  Serial.begin(9600);
}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()                     
{
  if (Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();
    if(incomingByte == 105){
      // tell servo to go to position in variable 'pos'  
      servoMotor.write(69);           
      delay(15);
      digitalWrite(ledPin, HIGH);
    }
    else if(incomingByte == 111){
      
      // tell servo to go to position in variable 'pos'  
      servoMotor.write(115);           
      delay(15);
      digitalWrite(ledPin, LOW);
    }
    else if (incomingByte == 108){
        duty = 102;        //duty cycle is 40 percent
        forward();
        motor_stop();
        delay(500);
    }
    else if (incomingByte == 109){
      duty = 154;        //duty cycle is 60 percent
      forward();
      delay(1000);
      motor_stop();
      delay(500);
    }
    else if (incomingByte == 110){
      duty = 205;        //duty cycle is 80 percent
      forward();
      delay(1000);
      motor_stop();
      delay(500);
    }
    else if (incomingByte == 121){
      duty = 256;        //duty cycle is 100 percent
      forward();
      delay(1000);
      motor_stop();
      delay(500);
    }
  }
}

//////////////////////////////////
void motor_stop()
{
  digitalWrite(ena_hbridge, LOW);
}

void forward()
{    //motor will turn in one direction:
  analogWrite(motor1Pin1_PWM, duty);   // set pin 5 on L298 0.5 duty cucle
  digitalWrite(motor1Pin2, LOW);      // set pin 7 on L298 low
  
  digitalWrite(vss_hbridge, HIGH);
  digitalWrite(ena_hbridge, HIGH);
          delay(10000);
}

void reverse()
{  //motor will turn in the opposite direction:
  analogWrite(motor1Pin1_PWM, duty);   // set pin 5 on L298 0.5 duty cucle
  digitalWrite(motor1Pin2, HIGH);     // set pin 7 on L298 high
  
  digitalWrite(vss_hbridge, HIGH);
  digitalWrite(ena_hbridge, HIGH);
}

108 is the decimal representation of the char "l" as i am sending the chars serially from a visual studio form on button press. If you look at the code you can see a servo that is controlled by an if statement. I have this servo moving wheels on a car left and right so am unsure why the motor wont work.

Thanks

Michael

It isn't your problem, but setting the duty cycle by means of a global is not a good idea - much better to use function parameters:

void forward(byte duty)
{    //motor will turn in one direction:
  analogWrite(motor1Pin1_PWM, duty);     digitalWrite(motor1Pin2, LOW);      // set pin 7 on L298 low
  
  digitalWrite(vss_hbridge, HIGH);
  digitalWrite(ena_hbridge, HIGH);
          delay(10000);
}

//then:
forward (127);  // go forward @ 50%

108 is the decimal representation of the char "l"

So, why not compare against the character 'l' ?

 if (incomingByte == 'l')

Hey.

I made those changes.

Its been a while since ive coded and im starting to remember little things like that (byte duty) thanks. They are heaps neater and a little bit smarter.

Do you have any idea about the other problem?

Do you have any idea about the other problem?

The other problem being that the motor doesn't work? A simple sketch, where loop tries to run the motor each direction for a few seconds, would be much simpler to debug. It's possible that its a hardware problem.

All this code, though, makes it hard to tell.

The code for the DC motor works. its the first lot of code. its just that when i try to add the working code to an if statement the code no longer works.

I jus cant figure out why

 if (incomingByte == 121){
      duty = 256;        //duty cycle is [glow]0[/glow] percent

Could that be it?

/Duty cycle for DC motor (0 to 256 range)

Close...but no cigar.

haha, yeah. typo. is it 257?

Anyway i just built the code up again around the working DC motor code.

It seems that when i add in the line

servoMotor.attach(servoPin); // attaches servo pin 6 to servo object

the DC motor does not work anymore.

the servo pin is int servoPin = 6; so giving it pin 6 on arduino which is connected to the servo and the pins used to control the DC motor are 8,9,10,11.

Is there something going on with the servo code that is affecting the pins that my DC motor is using?

ill change physical pins then write back

haha, yeah. typo. is it 257?

Colder, colder.

haha, yeah. typo. is it 257?

No. Try 255.

Post your updated, code, too.

258?

Ha no its :slight_smile:

0 - 255 (256)

ok just trying some different things out.

thanks

Hey,

So i physically swapped the control wire for the servo and the pin for the DC motor PWM control. Also i swapped their pin outs in the arduino software.

It works now. So im not really sure whats going on in the Servo.h library but there may be some pre DEFINED use of pin 9??

im not to sure of this though as i couldn't find the sketch for it, so maybe wrong

here is the updated code

//Arduino Control
#include <Servo.h>      // include the servo library

Servo servoMotor;       // creates an instance of the servo object to control a servo

int servoPin = 9;       // Control pin for servo motor.

int ledPin =  13;    // LED connected to digital pin 13
int incomingByte = 0;      // for incoming serial data


int motor1Pin1_PWM = 6;    // pin 5 on L298
int motor1Pin2 = 8;        // pin 7 on L298
int vss_hbridge = 10;      
int ena_hbridge = 11;

// The setup() method runs once, when the sketch starts

void setup()   {                
  // initialize the digital pin as an output:
  pinMode(ledPin, OUTPUT);
  pinMode(motor1Pin1_PWM, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(vss_hbridge, OUTPUT);
  pinMode(ena_hbridge, OUTPUT);
  // attaches the servo on pin 9 to the servo object
  servoMotor.attach(servoPin);  
  Serial.begin(9600);
}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()                     
{
  if (Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();
    if(incomingByte == 'i'){
      // tell servo to go to position in variable 'pos'  
      servoMotor.write(69);           
      delay(15);
      digitalWrite(ledPin, HIGH);
    }
    else if(incomingByte == 'o'){
      
      // tell servo to go to position in variable 'pos'  
      servoMotor.write(115);           
      delay(15);
      digitalWrite(ledPin, LOW);
    }
    else if (incomingByte == 'l'){
        forward(170);
        motor_stop();
        delay(500);
    }
    else if (incomingByte == 'm'){
      forward(154);
      delay(1000);
      motor_stop();
      delay(500);
    }
    else if (incomingByte == 'n'){
      forward(205);
      delay(1000);
      motor_stop();
      delay(500);
    }
    else if (incomingByte == 'y'){
      forward(255);
      delay(1000);
      motor_stop();
      delay(500);
    }
  }
}

//////////////////////////////////
void motor_stop()
{
  digitalWrite(ena_hbridge, LOW);
}

void forward(byte duty)
{    //motor will turn in one direction:
  analogWrite(motor1Pin1_PWM, duty);   // set pin 5 on L298 0.5 duty cycle
  digitalWrite(motor1Pin2, LOW);      // set pin 7 on L298 low
  
  digitalWrite(vss_hbridge, HIGH);
  digitalWrite(ena_hbridge, HIGH);
          delay(10000);
}

void reverse(byte duty)
{  //motor will turn in the opposite direction:
  analogWrite(motor1Pin1_PWM, duty);   // set pin 5 on L298 0.5 duty cycle
  digitalWrite(motor1Pin2, HIGH);     // set pin 7 on L298 high
  
  digitalWrite(vss_hbridge, HIGH);
  digitalWrite(ena_hbridge, HIGH);
}
forward(256);

?

Whooops, Changed.

Can i ask another question?

Yes but start another thread if it is not related.

ok. will start new one then :slight_smile: