UP, STOP, DOWN controll dc motor help

Hi! Im trying to write a code that will let me controll a dc motor connected w/ a L293.
I want it to go up, down and stop. Also i dont want it to go in the opposite direction without going trough the stop button. I also have to buttons that simulate end stops in both directions so that the controller know when to stop. I use pull down swithches. I know my circuit is right. But im new in programing, so i really need help!

I have written this code, but nothing works. And its probably a new beginner fail. Ill post the code under!

Thanks!!!

const int upButtonPin = 4; // Port opp
const int stopButtonPin = 5; // Stopp port
const int downButtonPin = 6; // Port ned
const int endUpButtonPin = 3; // Endestoppbryter Topp
const int endDownButtonPin = 7; // Endestoppbryter Bunn
const int enablePin = 9; // Enable L239d (1)
const int controlPin1 = 2; // Input 1 L239d (2)
const int controlPin2= 3; // Input 2 L239d (7)
int upButtonState = 0; 
int stopButtonState = 0;  
int downButtonState = 0;
int endUpButtonState = 0;
int endDownButtonState = 0;
int motorEnabled = 0;
int motorDirection = 1;

void setup() {
  // put your setup code here, to run once:
pinMode(upButtonPin, INPUT);
pinMode(stopButtonPin, INPUT);
pinMode(downButtonPin, INPUT);
pinMode(endUpButtonPin, INPUT);
pinMode(endDownButtonPin, INPUT);
pinMode(enablePin, OUTPUT);
pinMode(controlPin1, OUTPUT);
pinMode(controlPin2, OUTPUT);
digitalWrite(enablePin, LOW);
}

void loop() {
  // put your main code here, to run repeatedly:
upButtonState = digitalRead(upButtonPin);
delay(10);
stopButtonState = digitalRead(stopButtonPin);
delay(10);
downButtonState = digitalRead(downButtonPin);
delay(10);
endUpButtonState = digitalRead(endUpButtonPin);
delay(10);
endDownButtonState = digitalRead(endDownButtonPin);

if (stopButtonState == HIGH){
digitalWrite(enablePin, LOW);
digitalWrite(downButtonState,LOW);
digitalWrite(upButtonState,LOW);
}
if (upButtonState == HIGH && downButtonState == LOW &&
endUpButtonState == LOW) {
digitalWrite(stopButtonPin, LOW);  
digitalWrite(controlPin2, LOW);
digitalWrite(enablePin, HIGH);
digitalWrite(controlPin1, HIGH);
}
if (upButtonState == LOW && downButtonState == HIGH &&
endDownButtonState == LOW) { 
digitalWrite(controlPin1, LOW);
digitalWrite(enablePin,HIGH);
digitalWrite(controlPin2,HIGH);
}
if (endUpButtonState == HIGH) { 
digitalWrite (stopButtonState, HIGH);
}
if (endDownButtonState == HIGH) { 
digitalWrite (stopButtonState, HIGH);

 }
}

Nothing works? Have you run simpler custom sketches, or library example sketches, to individually test all the subsystems on your project (switches, motor, etc.)?

I will check that out! Thanks!
Do the code look okay for you? Like its not some obvious mistakes?

Why do you delay 10 milliseconds after every individual button read?

digitalWrite(stopButtonPin, LOW);

Why?

digitalWrite (stopButtonState, HIGH);Again, why?

aarg:
Why do you delay 10 milliseconds after every individual button read?

Is it unnecessary? Read something about it, to give it some time between the reads.

Havardkofoed:
Is it unnecessary? Read something about it, to give it some time between the reads.

Read what about it where? Why do you need time between reads?

several issues

button switches can connect a pin to ground or 5V. but when they are not pressed, the input floats. typically the pin is configured as INPUT_PULLUP so that the internal pull-up resistor pulls the pin HIGH when the button is not pressed and the button connect the pin to ground, making it LOW. LOW means the switch it pushed.

there's no need (though no harm) is repeatedly setting the output pins to move or stop the motor. code typically recognizes that a button state has changed and it pressed. there's just a single event recognized when the button changes state and some action taken. code need to keep track of the previous button state

along with above there's no need to check that the other buttons are not pressed.

and finally, only one action need be taken. So instead of a series of "if"s, may if, else if and maybe else

TheMemberFormerlyKnownAsAWOL:

digitalWrite(stopButtonPin, LOW);

Why?

digitalWrite (stopButtonState, HIGH);

Again, why?

I see now that this maybe should have been different..
Is it a more efficient way to make the buttons only work one at a time?

aarg:
Read what about it where? Why do you need time between reads?

bad English, my bad! Meant that I have been reading something about that delay between reads. Maybe i dont need time between them? You probably know better than me... :smiley:

gcjr:
[...]
button switches can connect a pin to ground or 5V. but when they are not pressed, the input floats.
[...]

Right, so please post a system wiring diagram, schematic and/or clear photos of your wiring.

Havardkofoed:
Is it a more efficient way to make the buttons only work one at a time?

Is this your code, or someone else's?

digitalWrite (stopButtonState, HIGH); Think about the range of values “stopButtonState” can contain.
What is connected to those two pins?

aarg:
Right, so please post a system wiring diagram, schematic and/or clear photos of your wiring.

look at the attachment under! :smiley:

aarg:
Is this your code, or someone else’s?

It is mine, therefore some different and weird solutions… only have few simple projects before this one.

Skjermbilde 2020-02-26 kl. 22.19.25.png

Please, not a Fritzing nightmare. It is too tiny and messed up to read.

Havardkofoed:
It is mine, therefore some different and weird solutions... only have few simple projects before this one.

Then perhaps you can explain your reasoning about the delays... if you are trying to debounce the keys, it would be good for you to research debouncing so you clearly understand what is required.

aarg:
Then perhaps you can explain your reasoning about the delays... if you are trying to debounce the keys, it would be good for you to research debouncing so you clearly understand what is required.

My only reason meant for the delay was to prevent a lag or crash, but it maybe overkill on this one?
When I check my notes her now, I see that i have written it as an reminder with the serialmonitor, so this one is just an mixup, I see that I probably dont need it :sweat_smile:

is this better? Just to see where the different pins goes. I can try to draw it if its not better.
Thanks again! You all are very helpful :slight_smile:

Skjermbilde 2020-02-26 kl. 22.48.35.png

Have you fixed the problems mentioned in reply #4?