# Controlling a dc motor (increase & decrease)

So this is one of my first Arduino projects for school.
My brother already helped me to write the code but now it’s only trouble shooting and I can’t seem to find the problem. It only increases to the maximum (200) and when this point is reached, then it will only decrease to 0 again.

What I want it to do is that it can increase and decrease at any moment (to change the speed at your preference)

Can somebody help me out?

``````const int buttplus= 11; // increase speed
const int buttmin =12; //decrease speed
int speed1=0;
int stap =50;  //number that needs to be added
int butt =0;
int snl=0;
int guard= false;
int guardmain= false;

void setup() {
pinMode(buttplus,INPUT);
pinMode(buttmin, INPUT);
Serial.begin(9600);

}

int regel(int snl,int butt){
//snl=speed1;
int buttstat=digitalRead(butt);
if(buttstat==false){
guard=false;
}
//       INCREASE SPEED
if(buttstat==HIGH && butt==11 && guard== false ){
snl+=stap;
guard=true;
}
//      DECREASE SPEED
if(buttstat==HIGH && butt==12 && guard== false ){
snl-=stap;
guard=true;
}
// Serial.print(buttstat);
delay(10);
return(snl);

}

void loop() {
int buttstatplus=digitalRead(buttplus);
int buttstatmin=digitalRead(buttmin);

if(buttstatplus==false && buttstatmin==false);
guardmain=false;

if(speed1<50 && speed1>=0 &&  guardmain==false){
butt=11;
speed1= regel(speed1,butt);
guardmain=true;
}
if(speed1>=0 && speed1>150  && guardmain==false)  //speed goes from 0 - 200
butt=12;
speed1=regel(speed1,butt);
guardmain=true;

Serial.println(speed1);
delay(10);
}
``````

I am so sorry! I uploaded the wrong code.. My brain is mush at the moment..
I'll put the right code up right now!

And please place then in code tags. Also, press ctrl+T in the IDE before you copy it. And replacing the Dutch names with English ones will make it easier for a lot here to help you.

Damn, to late [/edit]

Okay, let's start with some tips:

• Short variable names look easy for a lot of newbies but they are terrible. You forget what they do, how you made them short etc. So "snl" is just terrible. Just call it "snelheid" or for the sake of internationalization, "speed". Same for "buttplus" => "buttonPlusPin". Directly clear what it does. Can't say that for "guard" and "guardmain"...

• What's connected to butt? Because you read it with a digitalRead() but you also assign (seem to us) random values to it...

• Do you use external pull down resistors?

• Explain exactly what you want to do. What is all the guard crap etc.

• You don't debounce, you don't check for state change. So a short press of a button will result in a large change in speed.

• The guard functions as a debounce function, so if you press the button, it only increases one time with 50.
If I would remove the guard and the mainguard it goes straight from 0-200 and from 200-0.

• I do use 2.2K resistors for the buttons which are set-up in a similair way as this picture:

• I believe butt stand for buttmin and buttplus at the same time so it reads both but I am not sure. My brother helped me with this but I don’t really know his thought behind this. Not so smart of me I know.

dinomell:

• The guard functions as a debounce function, so if you press the button, it only increases one time with 50.
If I would remove the guard and the mainguard it goes straight from 0-200 and from 200-0.

Would be better off just implementing state change and debounce a normal way. Or, keep it easy and grab a library to do it for you.

dinomell:

• I do use 2.2K resistors for the buttons which are set-up in a similair way as this picture:

If you connect the button between GND and the pin and use pinMode(pin, INPUT_PULLUP) you don’t need external resistors. Hate that tutorial page

dinomell:

• I believe butt stand for buttmin and buttplus at the same time so it reads both but I am not sure. My brother helped me with this

What a hard way of doing things…

New code to increase / decrease speed in steps. Have a look. Much simpler

``````#include <Bounce2.h>

const byte MinusButtonPin = 11;
const byte PlusButtonPin = 12;

Bounce minusButton;
Bounce plusButton;

int speed = 0;
const byte SpeedStep = 50;

const byte SpeedMin = 0;
const byte SpeedMax = 200;

void setup() {
//Connect them between GND and the pin
minusButton.attache(MinusButtonPin, INPUT_PULLUP);
plusButton.attache(PlusButtonPin, INPUT_PULLUP);

Serial.begin(9600);

}

void loop(){
//read the buttons
minusButton.update();
plusButton.update();

//check if we need to act on them
if(minusButton.fell()){
speed -= SpeedStep;
}
else if(plusButton.fell()){
speed += SpeedStep;
}

//Check if we overshot the min max
if(speed < SpeedMin){
speed = SpeedMin;
}
else if(speed > SpeedMax){
speed = SpeedMax;
}

Serial.println(speed);
}
``````

I think I got it!

Thank you for sharing your code though! It looks a lot more easily than I had before

Hi,

Another funny thing that can happen is if you dont debounce your buttons it will look like a repeat button press to the program and so it will go 50 50 50 50 50 all the way to the end with seemingly just one press

So i assume you have debounced any buttons.

Yes I did! It works perfectly how I want it right now. I couldn't thank everyone who tried to help me enough!
Now it begins to get more fun for me!