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);
}
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.
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 whetherx 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 ;
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'.
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)
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?
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.
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