4 DC-Motoren mit verschiedenen Bedingungen ansteuern

Guten Tag,
ich versuche seit einigen Tagen mit meinem Arduino Mega 4 DC-Motoren mithilfe eines Joysticks anzusteuern.
als Motortreiber verwende ich 2-mal den “l298n”. An einen l298n sind jeweils 2 DC-Motoren angeschlossen.
Ich lese über die analogen pins die Position des Joysticks auf der X/-Y Achse aus und bestimme davon ausgehend wie sich die Motoren drehen sollen.
Problem an der Sache, ist die Tatsache das nur eine der Bedingungen die Motoren tatsächlich drehen lässt. Die andere Bedingung wird zwar erfüllt, aber die Motoren machen keine Anstalten sihc drehen zu wollen.

Code:

` //Definition Joystick-Achsen

#define joyStickX A0

#define joyStickY A1

//Pindefinition M1/M2

int M1 = 2;

int M1B = 51;

int M1A = 53;

int M2B = 47;

int M2A = 49;

int M2 = 3;

//Pindefiniton M3/M4

int M4 = 8;

int M4A = 43;

int M4B = 45;

int M3A = 39;

int M3B = 41;

int M3 = 9;

void setup() {

  Serial.begin(9600);  //Beginn der Seriellen Kommunikation

  pinMode(M1, OUTPUT);

  pinMode(M1B, OUTPUT);

  pinMode(M1A, OUTPUT);

  pinMode(M2B, OUTPUT);

  pinMode(M2A, OUTPUT);

  pinMode(M2, OUTPUT);

  pinMode(M4, OUTPUT);

  pinMode(M4A, OUTPUT);

  pinMode(M4B, OUTPUT);

  pinMode(M3A, OUTPUT);

  pinMode(M3B, OUTPUT);

  pinMode(M3, OUTPUT);

}

void loop() {

int x = analogRead(joyStickX);

int y = analogRead(joyStickY);

if(x == 516 && y == 0)

{

  Serial.println(1);

digitalWrite(M1A, HIGH);

digitalWrite(M1B, LOW);

analogWrite(M1, 255);

digitalWrite(M2A, HIGH);

digitalWrite(M2B, LOW);

analogWrite(M2, 255);

digitalWrite(M3A, HIGH);

digitalWrite(M3B, LOW);

analogWrite(M3, 255);

digitalWrite(M4A, HIGH);

digitalWrite(M4B, LOW);

analogWrite(M4, 255);

}

if(x == 516 && y == 1023)

{

  Serial.println(2);

digitalWrite(M1A, LOW);

digitalWrite(M1B, HIGH);

analogWrite(M1, 255);

digitalWrite(M2A, LOW);

digitalWrite(M2B, HIGH);

analogWrite(M2, 255);

digitalWrite(M3A, LOW);

digitalWrite(M3B, HIGH);

analogWrite(M3, 255);

digitalWrite(M4A, LOW);

digitalWrite(M4B, HIGH);

analogWrite(M4, 255);

}

else

{

digitalWrite(M1A, LOW);

digitalWrite(M1B, LOW);

analogWrite(M1, 255);

digitalWrite(M2A, LOW);

digitalWrite(M2B, LOW);

analogWrite(M2, 255);

digitalWrite(M3A, LOW);

digitalWrite(M3B, LOW);

analogWrite(M3, 255);

digitalWrite(M4A, LOW);

digitalWrite(M4B, LOW);

analogWrite(M4, 255);

}

}`

Weiß irgendjemand wo das Problem liegt? An einer zu hohen Stromaufnahme der Motoren aknn es nicht liegen, da sie ja bei einer der Bedingungen jeweils Problemfrei drehen.
An der Bedingung selber liegt es auch nicht, da habe ich jeweils hin und her getauscht und völlig andere bedingungen probiert.
Verdrahtungsfehler liegen auch nicht vor.
Danke im Vorraus.

#define m1 8
#define m2 9
#define EN1 2
#define m3 10
#define m4 11
#define EN2 3
#define m5 12
#define m6 13
#define EN3 4
#define m7 14
#define m8 15
#define EN4 5
I think you better had the enable pin too , to vary the speed of the for motor.

Hay,

M1, M2, M3, M4 are the enable Pins to vary the speed of the Motors.

okay are you using any motor driver
we usually have two logic pins and an enable pin where speed of the motor can be vary as for your wish.

I am using two “L298n”. Each L298n controls two of the motors.

so a L298n can drive two motor with adjustable speed by varying the input of the enable pins connected to PWM pins of whatever arduino board are being use .
Hopefully i already have done a project almost similar to your’s.

yes, i know. The enable Pins M1,M2,M3 and M4 are connected to PWM-Pins of my arduino Mega.
The Problem i have is that the motors do run if I fulfill one condition. But if the other condition is fullfilled, the motors dont run. I already changed the conditions a few times but the motors will only run on one condition.

` ```
if(x == 516 && y == 0)

{

  Serial.println(1);

digitalWrite(M1A, HIGH);

digitalWrite(M1B, LOW);

analogWrite(M1, 255);

digitalWrite(M2A, HIGH);

digitalWrite(M2B, LOW);

analogWrite(M2, 255);

digitalWrite(M3A, HIGH);

digitalWrite(M3B, LOW);

analogWrite(M3, 255);

digitalWrite(M4A, HIGH);

digitalWrite(M4B, LOW);

analogWrite(M4, 255);

}

The code above works fine.

if(x == 516 && y == 1023)

{

  Serial.println(2);

digitalWrite(M1A, LOW);

digitalWrite(M1B, HIGH);

analogWrite(M1, 255);

digitalWrite(M2A, LOW);

digitalWrite(M2B, HIGH);

analogWrite(M2, 255);

digitalWrite(M3A, LOW);

digitalWrite(M3B, HIGH);

analogWrite(M3, 255);

digitalWrite(M4A, LOW);

digitalWrite(M4B, HIGH);

analogWrite(M4, 255);

}
But this does not work at all. It doesnt matter if i switch the conditions or the direction/Speed of the motors.

why don’t you try in this way
void forward()
{
digitalWrite(m1,HIGH);
digitalWrite(m2,LOW);
analogWrite(EN1,255);
digitalWrite(m3,HIGH);
digitalWrite(m4,LOW);
analogWrite(EN2,255);
digitalWrite(m5,HIGH);
digitalWrite(m6,LOW);
analogWrite(EN3,255);
digitalWrite(m7,HIGH);
digitalWrite(m8,LOW);
analogWrite(EN4,255);
}
void backward()
{
digitalWrite(m1,LOW);
digitalWrite(m2,HIGH);
analogWrite(EN1,255);
digitalWrite(m3,LOW);
digitalWrite(m4,HIGH);
analogWrite(EN2,255);
digitalWrite(m5,LOW);
digitalWrite(m6,HIGH);
analogWrite(EN3,255);
digitalWrite(m7,LOW);
digitalWrite(m8,HIGH);
analogWrite(EN4,255);
delay(1000);
}
use delay if required or else remove it

I fixed it. The problem was that the “else” command only related to the second condition. That means even if the first condition is true, the “else” condition set the values of the motors to LOW because the second condition wasnt true.

Thanks for your help :slight_smile:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.