1

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. I'm getting phantom reading, that means,when i'm scrolling main menu, diode and dc motor starts working, when they shouldn't. Dc motor or led diode should work only when i enter their control menu, not when i'm in main menu.

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;
 }
}
int 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) < 800) {}
} ```
5
  • what have you done to debug your code? ... i see a few serial.print statements ... use more of them to trace the program flow
    – jsotola
    Dec 26 '20 at 17:41
  • yeah, i have used some to track phantom readings. But still can't find where is the problem
    – Jānis
    Dec 26 '20 at 17:45
  • please do not make an assumption that the problem is caused by phantom reading ... making such an assumption will hinder you in detecting the actual cause .... please present the sketch with the debugging code included and add the content of the serial monitor to your post
    – jsotola
    Dec 26 '20 at 17:57
  • ok, tnx for feedback
    – Jānis
    Dec 26 '20 at 18:00
  • a couple of observations ... waitKey() should be named waitKeyRelease() ...... the MotorControl() function does not wait for key to be released ... the UP or DOWN key is still pressed when the function is first executed
    – jsotola
    Dec 26 '20 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.