How to continuously check the program ?

Hi there, i just started learning arduino and i need help with this program. I managed to make my speed increment work but it looks ugly like hell especially the 30+ checkspeed part . Can anyone help me find a better method?

//Incrementing Motor Speed Control

const int IN1 = 2;
const int IN2 = 3;
const int ENA = 5;
const int but4 = 8;
// motor pins
boolean lastButton = LOW; //Variable containing the previous
//button state
boolean currentButton = LOW; //Variable containing the current
//button state

int speedIncrement = 0;
int buttonState4 = 0; // variable for reading the pushbutton status

void setup()
{
Serial.begin(9600);
pinMode (IN1, OUTPUT);
pinMode (IN2, OUTPUT);
pinMode (ENA, OUTPUT);
pinMode (but4, INPUT_PULLUP); //Set button as an input
}

/*

  • Debouncing Funtion
  • Pass it the previous button state,
  • and get back the current debounced button state.
    */
    boolean debounce(boolean last)
    {
    boolean current = digitalRead(but4); //Read the button state
    if (last != current) //if it's different
    {
    delay(5); //wait 5ms
    current = digitalRead(but4); //read it again
    }
    return current;
    }

/*
*Motor Speed Selection
*Pass a number for the Motor speed and set it
*/

void setSpeed(int increment)

{

//speed one
if (increment == 1)
{
analogWrite(ENA, 110);
Serial.println("b1");
}

else if (increment == 2)
{
analogWrite(ENA, 150);
Serial.println("b2");
}
else if (increment == 3)
{
analogWrite(ENA, 180);
Serial.println("b3");
}
else if (increment == 4)
{
analogWrite(ENA, 200);
Serial.println("b4");
}
else if (increment == 5)
{
analogWrite(ENA, 255);
Serial.println("b5");
}

//OFF
else
{
analogWrite(ENA, 0);
Serial.println("b6");
}

}

void loop()
{
buttonState4 = digitalRead(but4);
if (buttonState4 == LOW) {
Serial.println("0");
checkspeed();
checkspeed();
checkspeed();
checkspeed();
checkspeed();
checkspeed();
checkspeed();
checkspeed();
checkspeed();
checkspeed();
checkspeed();
checkspeed();
checkspeed();
checkspeed();
checkspeed();
checkspeed();
checkspeed();
checkspeed();
checkspeed();
checkspeed();
checkspeed();
checkspeed();
checkspeed();
checkspeed();
checkspeed();
checkspeed();
checkspeed();
checkspeed();
delay(100);
checkspeed();
checkspeed();
checkspeed();
checkspeed();
}
else {

}
}

void checkspeed() {
currentButton = debounce(lastButton); //read debounce state
if (lastButton == LOW && currentButton == HIGH) //if it was pressed
{
speedIncrement++; //Increment motor speedaxzx

}
lastButton = currentButton; //reset button value

//if cycled through all speed increments
//reset the speed to 0
if (speedIncrement == 6) speedIncrement = 0;

setSpeed(speedIncrement);

}

checkspeed(); does not need to be called like that.

Since loop repeats over and over, call checkspeed(); once in loop.

Examine how switch/case works.

Use CTRL T to format the sketch.
Please use code tags.
Use the </> icon in the posting menu.

[code] Paste sketch here. [/code]

.

Hi there sorry about that am new here. if i do checkspeed() only 1 time and i pressed the button it does not go to

else if (increment == 2) and will stuck at if (increment == 1) until i press the button for about 5 times. However when i put the 30+ checkspeed(); then it can work.

To start with, do you know how to use switch/case instead of if/else if

Do you understand what is happening in this section of code?
Note: This sketch is not complete as it stands. (more to follow)

//Incrementing Motor Speed Control

const byte IN1  = 2;
const byte IN2  = 3;
const byte ENA  = 5;
const byte but4 = 8;

const byte heartBeatLED = 13;

//motor pins
byte lastButton     = HIGH;    //Variable containing the previous

//button state
byte currentButton  = HIGH;    //Variable containing the current

byte speedIncrement = 0;
byte buttonState4   = 0;        // variable for reading the pushbutton status

unsigned long currentMillis;
unsigned long heartBeatMillis;
unsigned long lastMillis;

const unsigned long heartBeatDelay = 250UL;

//***************************************************************************
void setup()
{
  Serial.begin(9600);

  pinMode (heartBeatLED, OUTPUT);
  pinMode (IN1,  OUTPUT);
  pinMode (IN2,  OUTPUT);
  pinMode (ENA,  OUTPUT);
  pinMode (but4, INPUT_PULLUP);      //Set button as an input


} //END of      s e t u p ( )

//***************************************************************************
void loop()
{
  currentMillis = millis(); //for milli second timing

  //***************************
  //HeartBeat LED, should toggle every heartBeatDelay milliseconds if code is nonblocking
  if (currentMillis - heartBeatMillis >= heartBeatDelay)
  {
    //reset timing
    heartBeatMillis = heartBeatMillis + heartBeatDelay;

    //Toggle heartBeatLED
    digitalWrite(heartBeatLED, !digitalRead(heartBeatLED));
  }


  //***************************
  //check every 50ms
  if (currentMillis - lastMillis >= 50)
  {
    checkspeed();

    //reset timing
    lastMillis = lastMillis + 50;
  }


  //***************************
  //Other nonblocking code



} //END of       l o o p  ( )

xxfuffmxx:
Hi there sorry about that am new here. if i do checkspeed() only 1 time and i pressed the button it does not go to

else if (increment == 2) and will stuck at if (increment == 1) until i press the button for about 5 times. However when i put the 30+ checkspeed(); then it can work.

You will have to analyse why a single call of checkSpeed does not work.