Automated Ramp using the L298N isnt working

So I had the idea of writing a code that lowers a Ramp (aka Spins a Motor using the L298N Board) when a button is Pressed and I have two limit switches attached that should stop the motor from spinning in each direction, I am not capable of getting the motor to spin in one direction, then stop when the Limitswitch is detected and when the main button is pressed again it should turn in the other direction until that other limit switch is triggered. (I wired the limit switches up like normal buttons)
Here is the code (the Servo can be ignored for now)
Hope someone can help me or bump me in the right direction

#include <Servo.h>

Servo Linearservo;
int limit1 = 6;
int limit2 = 4;
const int Button =2;
int in1 = 11;
int in2 = 9;
int buttonState = 0;
int lim1state = 0;
int lim2state = 0;

void setup() {
    Linearservo.attach(12);
   pinMode(in1,OUTPUT);
    pinMode(in2,OUTPUT);
    pinMode(Button,INPUT);
    pinMode(4,INPUT);
    pinMode(6,INPUT);
    
}

void loop() {
 
  
  buttonState = digitalRead(Button);
  lim1state = digitalRead(limit1);
  lim2state = digitalRead(limit2);
    
  if (buttonState == HIGH) {
    if (lim1state = HIGH){
      while(lim2state = LOW);{
        Linearservo.write(180);
      digitalWrite(in1, HIGH);
       digitalWrite(in2, LOW);
      
      Linearservo.write(0);
       }
    }
     else if  (lim2state = HIGH) {
      while(lim1state = LOW);{
        digitalWrite(in2, HIGH);
         digitalWrite(in1, LOW);
      } 
    }
    
  }
  else digitalWrite(in2, LOW);
         digitalWrite(in1, LOW);
}

You need to remember what state it's in - going "up" or going "down". You have assigned no variable for that.

How can i do that?

How is a normal button wired? Post a schematic, please. Hand drawn, photographed and posted is fine. Include all pin names/numbers, components, their part numbers and/or values and power supplies.

From the if structure reference:

Beware of accidentally using the single equal sign (e.g. if (x = 10) ). The single equal sign is the assignment operator, and sets x to 10 (puts the value 10 into the variable x). Instead use the double equal sign (e.g. if (x == 10) ), which is the comparison operator, and tests whether x is equal to 10 or not. The latter statement is only true if x equals 10, but the former statement will always be true.

The same for the while structure, = is assignment, == for comparison. And the semicolon will make everything after the while execute unconditionally. Lose the ;

1 Like

First, your 'if else' logic is completely broken. You may not need it, because you will have to re-write it mostly anyway when you code a proper state machine for it.

For example, the last line in loop() is:

  else digitalWrite(in2, LOW);
  digitalWrite(in1, LOW); // this one
}
So the sketch will endlessly write a LOW to pin 'in1'.

Two BIG problems there.

Remember "==" for comparisons.
Are you sure about that semicolon?
You're not updating the loop control, so you'll get stuck, unless you have an interrupt to update the variable (no, I'm not suggesting you should have an interrupt)

Yeah, your code is a train wreck. But that is the least of your problems because there is no working design behind it.

Investigate the state machine.

I have seen quite a few posts lately, mostly from self proclaimed "beginners", doing that (I am talking about the semicolon). Where does that come from? Are there some bad tutorials doing the rounds?


hope this is semy understandable

If you don't use INPUT_PULLUP your input pins need external pull-up or pull-down resistors. Which did you use?

A pull-up resistor connects the input pin to +5V. The button connects the input pin to GND. The input reads HIGH when the switch is not closed and LOW when the switch is closed.

A pull-down resistor connects the input pin to GND. The button connects the input pin to +5V. The input reads HIGH when the switch is closed and LOW when the switch is not closed.

what do you mean with that ? (yes I am still a complete noob when it comes to coding)

10k, its a pull down i think

most of my errors happen when i forget them so I might have added one subconsciously :sweat_smile:

There are a lot more than one error. It really looks like you don't understand C language syntax.

i am defenetly not that familiar with them, that's why I'm asking for help here

This is not a place to learn C syntax from almost scratch. It's just not feasible to convey an orderly and complete lesson that way. Please look at examples, tutorials and reference sites for C/C++ to learn. Spend some time on it.

It comes from a failure to follow normal learning methods, closing your eyes and coding blindly, in the hopes that something working will come out of the random throwing of mud.

"Oh, it seems that every line ends with a semicolon, I guess this one must also..."

well I was trying to make this project and wasn't capable of writing the code myself, I thought the forum exists to get help from more knowledgeable people like you

i mean you gotta start somewhere, cant get everything right the moment you start