DC Motor Change direction with push button l298

Hello,

Ihave a dc motor, which is meant to drive forward for 30s when pushing a push button. When this is finished and the button is pushed again, it is meant to drive the same time in the other direction.
I realised this by an l298 and with an if and else if. But the first rotation does not happen.

I gave the variable "ort" ehis is 0 in the setup. This means the starting plave, position 0. When the button is detected it is meant to dwitch to 1, so later it can drive to start position again.
I included the built in LED to look it the if-part works and the led shines for 30s but the motor does not start. In the next attempt the motor runs in the backwards direction, but never forwards.

Sorry, but I cant upload data, here is my Code:

int taster=2; 
int tasterstatus=0; 
int ort = 0;
int ena = 9;
int in1 = 8;
int in2 = 7;

void setup() {
  pinMode(taster, INPUT); 
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(ena, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  
  digitalWrite(ena, 255);
}

void loop() {
  tasterstatus=digitalRead(taster); 
  
  if (tasterstatus == HIGH){
    if (ort == 0) {
      ort ++;
      digitalWrite(LED_BUILTIN, HIGH);
      digitalWrite(in1, HIGH);
      digitalWrite(in2, LOW);
      delay(30000);
      digitalWrite(LED_BUILTIN, LOW);


      digitalWrite(in1, LOW);
      digitalWrite(in2, LOW);
    }
    else if (ort == 1){
      ort --;
      digitalWrite(in1, LOW);
      digitalWrite(in2, HIGH);
      delay(30000);
      digitalWrite(in1, LOW);
      digitalWrite(in2, LOW);
    }
  }
}

digitalWrite() needs HIGH or LOW, not a number

What data did you want to upload?

Hello plunderstueck
The IDE provides a lot of examples to get started.
To prevent the ussage of the delay function the BLINKWITHOUTDELAY example is good start with design of a time keeper. To read and take action on buttons the State Change Detection example can be used too.
Take a view.
Have a nice day and enjoy coding in C++.
Дайте миру шанс!
p.s. I love Plunderstücke for breakfast.

Thank you!
But I do not think that this is the problem here because it works in the forward direction, but I will set it to HIGH.

referring to the upload I thought about my circuit diagram.

Thank you for the answer, so you suggest the delay function as the reason why the motor does not run in both directions?

No, I agree it is not the problem, but it is incorrect, so I thought I should tell you.

I think that is what we need to see, because I can't see a reason why your code would cause the problem you described.

Yes, you are right.
I dismantled everything and reassembled it and now it works.

Thank you guys anyways, I think I will close this here.

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