Offline
Newbie
Karma: 0
Posts: 11
|
 |
« on: January 31, 2013, 03:12:57 am » |
Hey, I have a problem. I want to set the two motors to different speed. I'm using an H-Bridge. Here's my sketch: #define STATE_IDLE 0 #define STATE_INIT 1 #define STATE_RUNNING 2 #define STATE_EXITING 3
const int in1Pin = 5; // H-Brücken-Eingangspins const int in2Pin = 4; const int in3Pin = 3; // H-Brücken-Eingangspins const int in4Pin = 2; const int en1Pin = 6; const int en2Pin = 7; const int buttonPin = 8;
int buttonState = 0; int Zustand;
void setup() { Zustand = STATE_IDLE; pinMode(buttonPin, INPUT); pinMode(in1Pin, OUTPUT); pinMode(in2Pin, OUTPUT); pinMode(in3Pin, OUTPUT); pinMode(in4Pin, OUTPUT); }
void loop() { switch( Zustand ) { case STATE_IDLE: Idle(); break; case STATE_INIT: Init(); break; case STATE_RUNNING: Run(); break; case STATE_EXITING: Exiting(); break; } }
void Idle() { buttonState = digitalRead(buttonPin); if (buttonState == HIGH) { Zustand = STATE_INIT; } }
void Init() { delay(1000); Zustand = STATE_RUNNING; }
void Run() { analogWrite(en1Pin,70); analogWrite(en2Pin,100); digitalWrite(in1Pin,LOW); digitalWrite(in2Pin,HIGH); delay(1500); digitalWrite(in1Pin,LOW); digitalWrite(in2Pin,LOW); digitalWrite(in3Pin,HIGH); digitalWrite(in4Pin,LOW); delay(5000); digitalWrite(in3Pin,LOW); digitalWrite(in4Pin,LOW); digitalWrite(in2Pin,LOW); digitalWrite(in1Pin,HIGH); delay(1500); digitalWrite(in1Pin,LOW); digitalWrite(in2Pin,LOW); Zustand = STATE_EXITING; } void Exiting() { delay(1000); Zustand = STATE_IDLE; } But when I press the button, just one motor runs in the speed, given with 'en1Pin'. Without trying to set the speed, the motors run correctly. Hope you can help and sorry for my bad English. Greets, Mathis
|
|
|
|
« Last Edit: January 31, 2013, 07:09:19 am by Mats2006 »
|
Logged
|
|
|
|
|
East Anglia (UK)
Offline
Edison Member
Karma: 55
Posts: 1601
May all of your blinks be without delay
|
 |
« Reply #1 on: January 31, 2013, 03:34:35 am » |
Your code looks OK analogWrite(en1Pin,70); //speed A analogWrite(en2Pin,100); //speed B digitalWrite(in1Pin,LOW); //1-2 run at speed A digitalWrite(in2Pin,HIGH); delay(1500); digitalWrite(in1Pin,LOW); //1-2 stop digitalWrite(in2Pin,LOW); digitalWrite(in3Pin,HIGH); //3-4 run at speed B digitalWrite(in4Pin,LOW); delay(5000); digitalWrite(in3Pin,LOW); //3-4 stop digitalWrite(in4Pin,LOW); digitalWrite(in2Pin,LOW); //1-2 run at speed A digitalWrite(in1Pin,HIGH); delay(1500); digitalWrite(in1Pin,LOW); //1-2 stop digitalWrite(in2Pin,LOW); I have annotated it to help me understand what I think you are trying to do Can you confirm that the names you have given the pins correspond to the actual inputs on the H bridges ?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
God Member
Karma: 9
Posts: 839
|
 |
« Reply #2 on: January 31, 2013, 03:36:02 am » |
Do you have two H bridges, or are you somehow trying to use one ?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 11
|
 |
« Reply #3 on: January 31, 2013, 03:45:21 am » |
First, thanks for your replies.
I've connected the en1Pin to the ENB and the en2Pin to the ENA, the INPUT pins are correctly connected. And yes I try to use one H-Bridge. And without trying to regular the speed, it works well.
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Offline
Edison Member
Karma: 55
Posts: 1601
May all of your blinks be without delay
|
 |
« Reply #4 on: January 31, 2013, 04:10:00 am » |
What H bridge are you using ? Can you provide a link to what you are using ? If it has ENA and ENB inputs then it is almost certainly 2 H bridges, even if they are in 1 chip, possibly an L293 or similar.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 11
|
 |
« Reply #5 on: January 31, 2013, 04:33:21 am » |
Yes its an L293.
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Offline
Edison Member
Karma: 55
Posts: 1601
May all of your blinks be without delay
|
 |
« Reply #6 on: January 31, 2013, 05:49:01 am » |
Did you build the circuit yourself or buy it ready made ? If you built it yourself what is the circuit ? If ready made is there a link to the product ?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 11
|
 |
« Reply #7 on: January 31, 2013, 06:00:57 am » |
|
|
|
|
« Last Edit: January 31, 2013, 06:24:45 am by Mats2006 »
|
Logged
|
|
|
|
|
Malaysia
Offline
Sr. Member
Karma: 7
Posts: 385
|
 |
« Reply #8 on: January 31, 2013, 07:40:18 am » |
for this kind of h bridge i would like to use functions actually void Motor(boolean Dir, int Speed) { int Stop; if (Dir ==Stop) { digitalWrite(Pin1,LOW); digitalWrite(Pin2,LOW); } else { digitalWrite(Pin1,Dir); digitalWrite(Pin2,!Dir); analogWrite(EnPin,Speed); } }
for dir you could specify HIGH and LOW,
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 143
Posts: 19379
I don't think you connected the grounds, Dave.
|
 |
« Reply #9 on: January 31, 2013, 08:05:33 am » |
void Motor(boolean Dir, int Speed) { int Stop; if (Dir ==Stop) Really bad idea. Please rethink.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Malaysia
Offline
Sr. Member
Karma: 7
Posts: 385
|
 |
« Reply #10 on: January 31, 2013, 08:33:53 am » |
your write AWOL, Its should be and int instead of a Boolean
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 11
|
 |
« Reply #11 on: January 31, 2013, 08:37:15 am » |
But where can I set the speed there?
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Offline
Edison Member
Karma: 55
Posts: 1601
May all of your blinks be without delay
|
 |
« Reply #12 on: January 31, 2013, 08:40:45 am » |
I am all in favour of using functions but unless the inline code works when the inputs to the H bridge are set correctly then a function is not going to help.
To make things even easier to understand I have written a small class to control a motor attached to an H bridge but that does depend on the basic code working, of course.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 143
Posts: 19379
I don't think you connected the grounds, Dave.
|
 |
« Reply #13 on: January 31, 2013, 08:41:06 am » |
your write AWOL, Its should be and int instead of a Boolean No, think again. What is the value of "Stop"?
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Malaysia
Offline
Sr. Member
Karma: 7
Posts: 385
|
 |
« Reply #14 on: January 31, 2013, 08:47:13 am » |
ok How about this void Motor1(boolean Pin1, boolean Pin2, int Speed) { digitalWrite(in1Pin,Pin1); digitalWrite(in2Pin,Pin2); analogWrite(en1Pin,Speed); } void Motor2(boolean Pin1, boolean Pin2, int Speed) { digitalWrite(in3Pin,Pin1); digitalWrite(in4Pin,Pin2); analogWrite(en2Pin,Speed); }
|
|
|
|
|
Logged
|
|
|
|
|
|