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);
}
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.
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.
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.
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.
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?
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);
}