So I made project to roll up/down window roller blind with DC motor using Arduino UNO3 + L298N H-Bridge controller. I have 4 buttons connected to Arduino digital pins. Pin 6 for rolling blind up and 7 for rolling it down. After blind rolls up to top it triggers button on pin 11 which stops motor. Same goes for bottom on pin 12. And the issue with code (I suppose) is that it works but motor barely spins. If I remove "else" sections then motor spins at full speed, but blind doesn't stop. Where is the issue? It's my first project on Arduino and I only familiar with Basic and Turbo Pascal language a bit.
Please post the code both with and without the "else" sections.
To post code and/or error messages:
- Use CTRL-T in the Arduino IDE to autoformat your complete code.
- Paste the complete autoformatted code between code tags (the </> button)
so that we can easily see and deal with your code.
3) Paste the complete error message between code tags (the </> button)
~~ so that we can easily see and deal with your messages.~~
4) If you already posted without code tags, you may add the code tags by
~~ editing your post.~~ Do not change your existing posts in any other way.
You may make additional posts as needed. - Please provide links to any libraries that are used
(look for statements in your code that look like #include ). Many libraries
are named the same but have different contents.
Before posting again, you should read the three locked topics at the top of the Programming Questions forum, and any links to which these posts point.
If your project involves wiring, please provide a schematic and/or a wiring diagram and/or a clear photograph of the wiring.
Good Luck!
Sorry totally forgot about code
With "else"
const int pwm = 9 ;
const int in_1 = 2 ;
const int in_2 = 3 ;
const int UpButton = 7 ;
const int DownButton = 6 ;
const int Upper = 11 ;
const int Lower = 12 ;
int UpButtonState = 0 ;
int DownButtonState = 0 ;
int UpperState = 0 ;
int LowerState = 0 ;
void setup() {
pinMode(pwm, OUTPUT) ;
pinMode(in_1, OUTPUT) ;
pinMode(in_2, OUTPUT) ;
pinMode(UpButton, INPUT_PULLUP) ;
pinMode(DownButton, INPUT_PULLUP) ;
pinMode(Upper, INPUT_PULLUP) ;
pinMode(Lower, INPUT_PULLUP) ;
}
void loop() {
MoveUp() ;
MoveDown() ;
}
void MoveUp() {
UpButtonState = digitalRead(UpButton) ;
UpperState = digitalRead(Upper) ;
if (UpButtonState == LOW && UpperState == HIGH) {
digitalWrite(in_1, HIGH) ;
digitalWrite(in_2, LOW) ;
analogWrite(pwm, 255) ;
}
else {
digitalWrite(in_1, HIGH) ;
digitalWrite(in_2, HIGH) ;
analogWrite(pwm, 255) ;
}
}
void MoveDown() {
DownButtonState = digitalRead(DownButton) ;
LowerState = digitalRead(Lower) ;
if (DownButtonState == LOW && LowerState == HIGH) {
digitalWrite(in_1, LOW) ;
digitalWrite(in_2, HIGH) ;
analogWrite(pwm, 255) ;
}
else {
digitalWrite(in_1, HIGH) ;
digitalWrite(in_2, HIGH) ;
analogWrite(pwm, 255) ;
}
}
Without "else"
const int pwm = 9 ;
const int in_1 = 2 ;
const int in_2 = 3 ;
const int UpButton = 7 ;
const int DownButton = 6 ;
const int Upper = 11 ;
const int Lower = 12 ;
int UpButtonState = 0 ;
int DownButtonState = 0 ;
int UpperState = 0 ;
int LowerState = 0 ;
void setup() {
pinMode(pwm, OUTPUT) ;
pinMode(in_1, OUTPUT) ;
pinMode(in_2, OUTPUT) ;
pinMode(UpButton, INPUT_PULLUP) ;
pinMode(DownButton, INPUT_PULLUP) ;
pinMode(Upper, INPUT_PULLUP) ;
pinMode(Lower, INPUT_PULLUP) ;
}
void loop() {
MoveUp() ;
MoveDown() ;
}
void MoveUp() {
UpButtonState = digitalRead(UpButton) ;
UpperState = digitalRead(Upper) ;
if (UpButtonState == LOW && UpperState == HIGH) {
digitalWrite(in_1, HIGH) ;
digitalWrite(in_2, LOW) ;
analogWrite(pwm, 255) ;
}
}
void MoveDown() {
DownButtonState = digitalRead(DownButton) ;
LowerState = digitalRead(Lower) ;
if (DownButtonState == LOW && LowerState == HIGH) {
digitalWrite(in_1, LOW) ;
digitalWrite(in_2, HIGH) ;
analogWrite(pwm, 255) ;
}
}
It's because of the fact you run both MoveUp() and MoveDown() each time through loop().
Let's say the up button is pressed. MoveUp() runs, top part of if passes, motor turns upwards. Immediately though, MoveDown() runs. Top part of if fails. That's cool if there's no else, and loop() runs MoveUp() again. But when the else is there, the else fails and the motor brakes until MoveUp() runs again.
You could use "else if" to explicitly check for the motion button and the limit switch to be pressed at the same time and only brake then.
Or, and I'd probably go this way, check the motion button in loop() and only run the correct one of MoveUp() and MoveDown() in the first place. Then in either of those, turn the motor unless the limit switch is pressed.
kenwood120s:
It's because of the fact you run both MoveUp() and MoveDown() each time through loop().Let's say the up button is pressed. MoveUp() runs, top part of if passes, motor turns upwards. Immediately though, MoveDown() runs. Top part of if fails. That's cool if there's no else, and loop() runs MoveUp() again. But when the else is there, the else fails and the motor brakes until MoveUp() runs again.
You could use "else if" to explicitly check for the motion button and the limit switch to be pressed at the same time and only brake then.
Thanks man! Now that you said where the problem is, it looks so obvious Will try to bend my mind and google to overcome that code after sleep. I'm still need to learn a lot, so it will be a challenge. And in near future bluetooth control will come.
I see a state machine in your future.
I agree with the replies above, especially reply #6. Also, I noticed that in some cases the same thing happens regardless of whether the 'if' or the 'else' executes. Those statements should be pulled outside of the 'if...else' just to save a few bytes and to make the logic simpler and more clear.
kenwood120s:
Or, and I'd probably go this way, check the motion button in loop() and only run the correct one of MoveUp() and MoveDown() in the first place. Then in either of those, turn the motor unless the limit switch is pressed.
You mean like that? Anyway, problem solved, code works, roller blind works
const int pwm = 9 ;
const int in_1 = 2 ;
const int in_2 = 3 ;
const int UpButton = 7 ;
const int DownButton = 6 ;
const int Upper = 11 ;
const int Lower = 12 ;
int UpButtonState = 0 ;
int DownButtonState = 0 ;
int UpperState = 0 ;
int LowerState = 0 ;
void setup() {
pinMode(pwm, OUTPUT) ;
pinMode(in_1, OUTPUT) ;
pinMode(in_2, OUTPUT) ;
pinMode(UpButton, INPUT_PULLUP) ;
pinMode(DownButton, INPUT_PULLUP) ;
pinMode(Upper, INPUT_PULLUP) ;
pinMode(Lower, INPUT_PULLUP) ;
}
void loop() {
UpButtonState = digitalRead(UpButton) ;
UpperState = digitalRead(Upper) ;
DownButtonState = digitalRead(DownButton) ;
LowerState = digitalRead(Lower) ;
if (UpButtonState == LOW && UpperState == HIGH) {
MoveUp();
}
else if (DownButtonState == LOW && LowerState == HIGH) {
MoveDown();
}
else {
digitalWrite(in_1, LOW) ;
digitalWrite(in_2, LOW) ;
analogWrite(pwm, 0) ;
}
}
void MoveUp() {
digitalWrite(in_1, HIGH) ;
digitalWrite(in_2, LOW) ;
analogWrite(pwm, 255) ;
}
void MoveDown() {
digitalWrite(in_1, LOW) ;
digitalWrite(in_2, HIGH) ;
analogWrite(pwm, 255) ;
}
Hi, is the last code you posted working? I need to do something similar, but cannot code at all.