DC motor/LED diode-controll

Hello everyone, my project is to control led diode and dc motor, test shield buttons, and check system run time. Testing buttons and run time i have done.
Deal is with led and dc motor control.
Issue is there that when i run code-diode and motor starts to work.
But they should start working only when i'm in in motor control menu or led control menu.

DC and diode part

void MotorControl() {
  while (readKeypad() !=  btnSELECT) {
    if (readKeypad() == btnUP) {
      speed1 <= maxspeed;
      speed1 += Upstep;
      lcd.setCursor(5, 2);
      lcd.print("                ");
      lcd.setCursor(5, 2);
      lcd.print(speed1);
      delay(100);
      analogWrite(mtor, speed1);
    }
    if (readKeypad() == btnDOWN) {
      speed1 <= maxspeed;
      speed1 -= Upstep;
      lcd.setCursor(5, 2);
      lcd.print("                ");
      lcd.setCursor(5, 2);
      lcd.print(speed1);
      delay(100);
      analogWrite(mtor, speed1);
    }
  }
}
//____________________________
void LEDControl() {
  while (readKeypad() != btnSELECT) {
    if (readKeypad() ==  btnUP) {
      speed1 < 255;
      speed1 += Upstep;
      lcd.setCursor(5, 2);
      lcd.print("                ");
      lcd.setCursor(5, 2);
      lcd.print(speed1);
      delay(100);
      analogWrite(led, speed1);
    }
    if (readKeypad() == btnDOWN) {
      speed1 > 255;
      speed1 -= Upstep;
      lcd.setCursor(5, 2);
      lcd.print("                ");
      lcd.setCursor(5, 2);
      lcd.print(speed1);
      delay(100);
      analogWrite(led, speed1);
    }
  }
}
//____________________________

All code.

#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3
#define btnSELECT 4
#define btnNONE   5

int masterKey = A0;
int keypadKey = 0;
int buttonPress = 0;

char push;

byte menu = 1;
byte menuOld = 1;
byte menuAll = 4;

//_______LED/MOTOR_________________
int speedall;
int mtor = 5,
    led = 6,
    error = 7;
const int Upstep = 3;
const int maxspeed = 255;

void setup() {
  pinMode(led, OUTPUT);
  pinMode(mtor, OUTPUT);
  lcd.begin(16, 2);
  mainMenu();
  delay(100);
  Serial.begin(9600);
}

void loop()
{
  push = readKeypad();
  menuContr();
  if (push == btnSELECT) //enter selected menu
  {
    waitKey();
    switch (menu) {
      case 1:
        lcd.begin(16, 2);
        lcd.setCursor(0, 0);
        lcd.print("Push the buttons");
        ButtonTest();
        break;
      case 2:
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Speed 0 to 255:");
        MotorControl();
        break;
      case 3:
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Brightness 0 to 255:");
        LEDControl();
        break;
      case 4:
        SystemTime();
        break;
    }
    mainMenu();
    waitKey();
  }
  delay(10);
}
//____________________________
void ButtonTest() {
  while (readKeypad() != btnSELECT) {
    lcd.setCursor(9, 1);
    lcd.print(millis() / 1000);
    lcd.setCursor(0, 1);
    int lcd_key = readKeypad();
    switch (lcd_key) {
      case btnRIGHT:
        lcd.print("RIGHT ");
        break;
      case btnLEFT:
        lcd.print("LEFT   ");
        break;
      case btnUP:
        lcd.print("UP    ");
        break;
      case btnDOWN:
        lcd.print("DOWN  ");
        break;
      case btnSELECT:
        lcd.print("SELECT");
        break;
      case btnNONE:
        lcd.print("NONE  ");
        break;
    }
  }
}
//____________________________
void MotorControl() {
  while (readKeypad() !=  btnSELECT) {
    if (readKeypad() == btnUP) {
      speedall <= maxspeed;
      speedall += Upstep;
      lcd.setCursor(5, 2);
      lcd.print("                ");
      lcd.setCursor(5, 2);
      lcd.print(speedall);
      delay(100);
      analogWrite(mtor, speedall);

    }
    else if (readKeypad() == btnDOWN) {
      speedall <= maxspeed;
      speedall -= Upstep;
      Serial.print(speedall);
      lcd.setCursor(5, 2);
      lcd.print("                ");
      lcd.setCursor(5, 2);
      delay(100);
      analogWrite(mtor, speedall);
    }
  }
}
//____________________________
void LEDControl() {

  while (readKeypad() != btnSELECT) {
    if (readKeypad() ==  btnUP) {
      speedall < 255;
      speedall += Upstep;

      lcd.setCursor(5, 2);
      lcd.print("                ");
      lcd.setCursor(5, 2);
      lcd.print(speedall);
      delay(100);
      analogWrite(led, speedall);

    }
    else if (readKeypad() == btnDOWN) {
      speedall > 255;
      speedall -= Upstep;

      lcd.setCursor(5, 2);
      lcd.print("                ");
      lcd.setCursor(5, 2);
      lcd.print(speedall);
      delay(100);
      analogWrite(led, speedall);

    }
  }
}
//____________________________
void SystemTime() {
  lcd.clear();
  while (readKeypad() != btnSELECT) {
    lcd.setCursor(6, 0);
    lcd.print("TIME");
    lcd.setCursor(7, 1);
    lcd.print(millis());
    lcd.print("s");
    delay(100);
    lcd.clear();
  }
}
//______________________________

void mainMenu() {
  lcd.clear();
  lcd.setCursor(0, 0);
  switch (menu) {
    case 1:
      Serial.print(speedall);
      lcd.print("Button test");
      break;
    case 2:
      Serial.print(speedall);
      lcd.print("Motor contr");
      break;
    case 3:
      Serial.print(speedall);
      lcd.print("LED contr");
      break;
    case 4:
      Serial.print(speedall);
      lcd.print("Syst run time");
      break;
  }
}
//____________________________
void executeAction() {
  switch (menu) {
    case 1:
      ButtonTest();
      break;
    case 2:
      MotorControl();
      break;
    case 3:
      LEDControl();
      break;
    case 4:
      SystemTime();
      break;
  }
}
void menuContr() {
  waitKey();
  if (push == btnUP) {
    menu++;
    if (menu > menuAll)
      menu = 1;
  }
  else if (push == btnDOWN) {
    menu--;
    if (menu == 0)
      menu = menuAll;
  }
  if (menu != menuOld) {
    mainMenu();
    menuOld = menu;
  }
}
char readKeypad() {
  keypadKey = analogRead(masterKey);
  if (keypadKey > 1000) return btnNONE;     // about 1023
  if (keypadKey < 50)   return btnRIGHT;    // about 0
  if (keypadKey < 195)  return btnUP;       // about 99
  if (keypadKey < 380)  return btnDOWN;     // about 255
  if (keypadKey < 555)  return btnLEFT;     // about 409
  if (keypadKey < 790)  return btnSELECT;   // about 640
  return btnNONE;
}
void waitKey() {
  while ( analogRead(masterKey) < 700) {}
}

[/code]

Your code doesn't compile.

@wildbill fixed

wildbill:
Your code doesn't compile.

fixed

You're getting a compiler warning to the effect that this does nothing:

      speed1 <= maxspeed;

I suspect that your problem though is that you're getting phantom results from your analogRead when you aren't pressing anything, which is probably because it's floating.

I suggest that you add some Serial.prints to tell you what it's getting.

I wouldn't expect printing to change anything in this case, just give you a clue of where and what the problem is.

wildbill:
I wouldn't expect printing to change anything in this case, just give you a clue of where and what the problem is.

okay, i found that when i am in specific menu pages im getting phantom results, coul'd you give me some kind clue how to deal with them?

Sounds like a wiring issue to me. How are your buttons wired up?

I'm using lcd keypad shield, so quite hard to explain wiring
https://create.arduino.cc/projecthub/electropeak/using-1602-lcd-keypad-shield-w-arduino-w-examples-e02d95

I added shematic to post, but i dont think that its possible to understand wiring

Where are you getting phantom results?

Basically fixing it is going to take adding serial.prints until you narrow down where the issue is.

When i go on each of main menu "pages"

So what happens in ButtonTest?

wildbill:
So what happens in ButtonTest?

Work fine, test each button, if its pressed shows on lcd

So debugged my code, its looks like that up buttons all the time gives signal

#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3
#define btnSELECT 4
#define btnNONE   5

int masterKey = A0;
int keypadKey = 0;
int buttonPress = 0;


char push;

byte menu = 1;
byte menuOld = 1;
byte menuAll = 4;


//_______LED/MOTOR_________________
int speedall;
int mtor = 5,
    led = 6;
const int Upstep = 3;
const int maxspeed = 255;


const int buttonPin = 7;
int buttonState = LOW;

void setup() {
  pinMode(led, OUTPUT);
  pinMode(mtor, OUTPUT);
  pinMode(buttonPin, OUTPUT);
  lcd.begin(16, 2);
  mainMenu();
  delay(100);
  Serial.begin(9600);
}

void loop()
{
  Serial.print ("\n");
  Serial.print ("main menu");
  Serial.print ("\n");
  Serial.print (keypadKey);
  Serial.print ("\n");
  Serial.print(masterKey);
  if (buttonState == HIGH) {
    Serial.print ("Button START");
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("ERROR");
  }
  buttonState = analogRead(buttonPin);
  push = readKeypad();
  menuContr();
  if (push == btnSELECT) //enter selected menu
  {
    waitKey();
    switch (menu) {
      case 1:
        Serial.print ("\n");
        Serial.print ("enter 1");
        lcd.begin(16, 2);
        lcd.setCursor(0, 0);
        lcd.print("Push the buttons");
        ButtonTest();
        break;
      case 2:
        Serial.print ("\n");
        Serial.print ("ENTER 2");
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Speed 0 to 255:");
        MotorControl();
        break;
      case 3:
        Serial.print ("\n");
        Serial.print ("ENTER 3");
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Brightness 0 to 255:");
        LEDControl();
        break;
      case 4:
        Serial.print ("\n");
        Serial.print ("ENTER 4");
        SystemTime();
        break;
    }
    mainMenu();
    waitKey();
    delay(100);
  }

}

//____________________________
void ButtonTest() {
  while (readKeypad() != btnSELECT) {

    lcd.setCursor(9, 1);
    lcd.print(millis() / 1000);
    lcd.setCursor(0, 1);
    int lcd_key = readKeypad();
    Serial.print ("\n");
    Serial.print (lcd_key);

    switch (lcd_key) {
      case btnRIGHT:
        lcd.print("RIGHT ");
        break;
      case btnLEFT:
        lcd.print("LEFT   ");
        break;
      case btnUP:
        lcd.print("UP    ");
        break;
      case btnDOWN:
        lcd.print("DOWN  ");
        break;
      case btnSELECT:
        lcd.print("SELECT");
        break;
      case btnNONE:
        lcd.print("NONE  ");
        break;
    }
  }
}
//____________________________
void MotorControl() {
  while (readKeypad() !=  btnSELECT) {
    if (readKeypad() == btnUP) {
      speedall <= maxspeed;
      speedall += Upstep;
      Serial.print ("\n");
      Serial.print(masterKey);
      lcd.setCursor(5, 2);
      lcd.print("                ");
      lcd.setCursor(5, 2);
      lcd.print(speedall);
      delay(100);
      analogWrite(mtor, speedall);

    }
    else if (readKeypad() == btnDOWN) {
      speedall <= maxspeed;
      speedall -= Upstep;
      Serial.print(speedall);
      lcd.setCursor(5, 2);
      lcd.print("                ");
      lcd.setCursor(5, 2);
      delay(100);
      analogWrite(mtor, speedall);
    }
  }
}
//____________________________
void LEDControl() {

  while (readKeypad() != btnSELECT) {
    if (readKeypad() ==  btnUP) {
      speedall < 255;
      speedall += Upstep;

      lcd.setCursor(5, 2);
      lcd.print("                ");
      lcd.setCursor(5, 2);
      lcd.print(speedall);
      delay(100);
      analogWrite(led, speedall);
    }
    else if (readKeypad() == btnDOWN) {
      speedall > 255;
      speedall -= Upstep;
      lcd.setCursor(5, 2);
      lcd.print("                ");
      lcd.setCursor(5, 2);
      lcd.print(speedall);
      delay(100);
      analogWrite(led, speedall);

    }
  }
}
//____________________________
void SystemTime() {
  lcd.clear();
  while (readKeypad() != btnSELECT) {
    Serial.print("\n");
    Serial.print(masterKey);
    lcd.setCursor(6, 0);
    lcd.print("TIME");
    lcd.setCursor(7, 1);
    lcd.print(millis());
    lcd.print("s");
    delay(100);
    lcd.clear();
  }
}
//______________________________

void mainMenu() {
  lcd.clear();
  lcd.setCursor(0, 0);
  switch (menu) {
    case 1:
      Serial.print(speedall);
      lcd.print("Button test");
      break;
    case 2:
      Serial.print(speedall);
      lcd.print("Motor contr");
      break;
    case 3:
      Serial.print(speedall);
      lcd.print("LED contr");
      break;
    case 4:
      Serial.print(speedall);
      lcd.print("Syst run time");
      break;
  }
}
//____________________________
void executeAction() {
  switch (menu) {
    case 1:
      Serial.print("\n");
      Serial.print(masterKey);
      ButtonTest();
      break;
    case 2:
      Serial.print("\n");
      Serial.print(masterKey);
      MotorControl();
      break;
    case 3:
      Serial.print("\n");
      Serial.print(masterKey);
      LEDControl();
      break;
    case 4:
      Serial.print("\n");
      Serial.print(masterKey);
      SystemTime();
      break;
  }
}
void menuContr() {
  waitKey();
  if (push == btnUP) {
    menu++;
    if (menu > menuAll)
      menu = 1;
  }
  else if (push == btnDOWN) {
    menu--;
    if (menu == 0)
      menu = menuAll;
  }
  if (menu != menuOld) {
    mainMenu();
    menuOld = menu;
  }
}
char readKeypad() {
  keypadKey = analogRead(masterKey);
  if (keypadKey < 100)   return btnRIGHT;    // about 0
  if (keypadKey < 200)  return btnUP;       // about 99
  if (keypadKey < 400)  return btnDOWN;     // about 255
  if (keypadKey < 600)  return btnLEFT;     // about 409
  if (keypadKey < 800)  return btnSELECT;   // about 640
  else return btnNONE;
}
void waitKey() {
  while ( analogRead(masterKey) < 800) {}
}