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