Hi,
I'm having problem to control the motors.This is my programming:
const int buttonPin1 = 2; // the number of the pushbutton pin
const int buttonPin2= 3; // the number of the pushbutton pin
const int buttonPin3= 4; // the number of the pushbutton pin
const int buttonPin4= 5; // the number of the pushbutton pin
const int motora_pwm = 6;
const int motora_dir = 7;
const int motorb_pwm = 8;
const int motorb_dir = 9;
// variables will change:
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;// variable for reading the pushbutton status
void setup() { // initialize the motor pin as an output:
pinMode(motora_pwm, OUTPUT);
pinMode(motora_dir, OUTPUT);
pinMode(motorb_pwm, OUTPUT);
pinMode(motorb_dir, OUTPUT); // initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState1= digitalRead(buttonPin1);
buttonState2= digitalRead(buttonPin2);
buttonState3= digitalRead(buttonPin3);
buttonState4= digitalRead(buttonPin4);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState1== HIGH) {
digitalWrite(motora_pwm, HIGH);
digitalWrite(motora_dir, LOW);
digitalWrite(motorb_pwm, HIGH);
digitalWrite(motorb_dir, LOW);
}
if (buttonState2== HIGH) {
digitalWrite(motora_pwm, HIGH);
digitalWrite(motora_dir, HIGH);
digitalWrite(motorb_pwm, HIGH);
digitalWrite(motorb_dir, HIGH);
}
if (buttonState3 == HIGH) {
digitalWrite(motora_pwm, HIGH);
digitalWrite(motora_dir, LOW);
digitalWrite(motorb_pwm, LOW);
digitalWrite(motorb_dir, LOW);
}
if (buttonState4 == HIGH) {
digitalWrite(motora_pwm, LOW);
digitalWrite(motora_dir, LOW);
digitalWrite(motorb_pwm, HIGH);
digitalWrite(motorb_dir, LOW);
}
else {
// turn motor off:
digitalWrite(motora_pwm, LOW);
digitalWrite(motora_dir, LOW);
digitalWrite(motorb_pwm, LOW);
digitalWrite(motorb_dir, LOW);
}
}
Moderator edit: Code now with added code tags tm
My problem is the assigned output will always give 0.5-1.0V even though I put it as LOW. When I connect the output to the motor,
the motor will keep running since there is a voltage at the output. How to make the output 0V when its being set to LOW?
Thanks
Syahmi Salim
I guess my duemilanove is damaged. I already change to leonardo. This is my coding :
// constants won't change. They're used here to
// set pin numbers:
int buttonPin1 = 2;
int motora_pwm = 3;
int motora_dir = 5;
// variables will change:
int buttonState1 = 0;
void setup() {
// initialize the motor pin as an output:
pinMode(motora_pwm, OUTPUT);
pinMode(motora_dir, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState1= digitalRead(buttonPin1);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState1== HIGH) {
digitalWrite(motora_pwm,HIGH);
analogWrite(motora_dir,191);
}
}
Moderator edit: Code tags again.
By the way, I'm using this motor driver http://cytron.com.my/viewProduct.php?pcode=MD10C&name=Enhanced%2010Amp%20DC%20Motor%20Driver.
Attached is the user manual. According to the user manual, pin DIR on MD10C is for the direction for the motor. I'm using pwm output on DIR pin. It supposed to change the rotation when I put the DIR output to 191(equal to 75% of the duty cycle) right? As I put the value of DIR pin to 191 (equal to 75% of the duty cycle), the rotation of the motor wouldn't change.Anyone can help me?
Thanks
Ok. I'm confused. Why are you applying a digital HIGH to the pwm input, and pwm to the digital direction input? Writing HIGH to pwm is basically saying 'full speed'.
From the manual:
For sign-magnitude PWM operation, 2 control signals are used
to control the speed and direction of the motor. PWM is feed to the PWM pin to control
the speed while DIR pin is used to control the direction of the motor.
You should be doing digitalWrite() to the dir pin, and analogWrite() to the pwm pin.
Okay,done.Everything just fine.But the motor spin too slow. When PWM and DIR is high, the voltage at PWM
is only 0.7V while DIR only 2.3V. If the PWM is high and DIR is low, the PWM pin is 3.2V. So, any suggestion to
boost the voltage of PWM and DIR pin?
Thanks.
I have another problem.. Why is the there's always output even though i never connect the input?
I measure the output voltage and i get 2.5V even though there's no input. Anybody can explain?