Why won't my switch cases move?

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

unsigned long T;

// Analog Pins
const int button = 45;
const int buzzer = 43;
const int pin5V = A1;
const int pin3V = A0;
const int pinAREF = A7;
const int pinRST = A6;
const int pinU = A3;
const int pinV = A2;

// Digital Pins
int dp[18] = {2, 3, 4, 5, 6, 7, 39, 41, 16, 17, 18, 23, 25, 27, 29, 31, 35, 37};

byte state = 0;
char a;

void setup() {
  pinMode(button, INPUT_PULLUP);
  pinMode(buzzer, OUTPUT);

  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.clear();
  
  Serial3.begin(9600);
  Serial.begin(9600);
  
}

void loop() {
  switch (state) {
    case 0:
      lcd.setCursor(0, 0);
      lcd.print(" PLS PRESS THE  ");
      lcd.setCursor(0, 1);
      lcd.print("     BUTTON     ");
      if (digitalRead(button) == LOW) {
        lcd.clear();
        delay(200);
        state++;
      }
      break;

    case 1:
      Serial.println("Current State: 1");
      
      float valpin5V = 0.0;
      float valpin3V = 0.0;
      float valpinAREF = 0.0;
      float valpinRST = 0.0;
      float valpinU = 0.0;
      float valpinV = 0.0;
      
      for (int i = 0; i < 10; i++) {
        valpin5V += analogRead(pin5V);
        valpin3V += analogRead(pin3V);
        valpinAREF += analogRead(pinAREF);
        valpinRST += analogRead(pinRST);
        valpinU += analogRead(pinU);
        valpinV += analogRead(pinV);
        delay(200);
      }
    
      valpin5V /= 10;
      valpin3V /= 10;
      valpinAREF /= 10;
      valpinRST /= 10;
      valpinU /= 10;
      valpinV /= 10;
      delay(200);
    
      valpin5V = valpin5V*(5.0/1023);
      valpin3V = valpin3V*(5.0/1023);
      valpinAREF = valpinAREF*(5.0/1023);
      valpinRST = valpinRST*(5.0/1023);
      valpinU = valpinU*(5.0/1023);
      valpinV = valpinV*(5.0/1023);
      delay(500);

      Serial.print(F("valpin5V = "));;
      Serial.println(valpin5V);
      Serial.print(F("valpin3V = "));;
      Serial.println(valpin3V);
      Serial.print(F("valpinAREF = "));;
      Serial.println(valpinAREF);
      Serial.print(F("valpinRST = "));;
      Serial.println(valpinRST);
      Serial.print(F("valpinU = "));;
      Serial.println(valpinU);
      Serial.print(F("valpinV = "));;
      Serial.println(valpinV);
      delay(500);
      
      if (valpin5V < 4.5) {
        lcd.clear();
        lcd.println("Error in pin 5V");
        state = 100;
        break;
      }
      delay(200);
      
      if (valpin3V < 2.5 || valpin3V > 3.5) {
        lcd.clear();
        lcd.println("Error in pin 3V");
        state = 100;
        break;
      }
      delay(200);

      if (valpinAREF < 2.5 || valpin3V > 3.5) {
        lcd.clear();
        lcd.println("Error in pin AREF");
        state = 100;
        break;
      }
      delay(200);

      if (valpinRST < 4.5) {
        lcd.clear();
        lcd.println("Error in pin RST");
        state = 100;
        break;
      }
      delay(200);

      if (valpinU < 4.5) {
        lcd.clear();
        lcd.println("Error in pin U");
        state = 100;
        break;
      }
      delay(200);

      if (valpinV < 4.5) {
        lcd.clear();
        lcd.println("Error in pin V");
        state = 100;
        break;
      }
      delay(200);

      Serial.println("Analog Tests Done");
      lcd.println("Analog Tests Done");
      delay(500);
      state++;

    case 2:
      Serial.println("Current State: 2");
      
      handleSerialData('T');
      delay(1000);
      handleSerialData('a');
      delay(1000);
      handleSerialData('b');
      delay(1000);
      handleSerialData('~');
      
      delay(500);
      lcd.clear();
      state++;

    case 3:
      Serial.println("Current State: 3");
      Serial.print("You sent: ");
      Serial.write("H");
      Serial.println("");
      delay(500);
      
      for (int i = 0; i < 18; i++) {
        pinMode(dp[i], INPUT);
        if (digitalRead(dp[i]) == LOW) {
          Serial.print("Error in pin ");
          Serial.println(dp[i]);
          lcd.clear();
          lcd.print("error in pin "); lcd.print(dp[i]);
          delay(1000);
          state = 100;
        }
      }
      
      lcd.clear();
      delay(1000);
      state++;

      
    case 4:
      Serial.println("Current State: 4");
      Serial.print("You sent: ");
      Serial.write("L");
      Serial.println("");
      delay(500);
      
      for (int i = 0; i < 18; i++) {
        pinMode(dp[i], INPUT);
        if (digitalRead(dp[i]) == HIGH) {
          Serial.print("Error in pin ");
          Serial.println(dp[i]);
          lcd.clear();
          lcd.print("error in pin "); lcd.print(dp[i]);
          delay(1000);
          state = 100;
        }
      }
      
      lcd.clear();
      delay(1000);
      state++;

    case 5:
      Serial.println("Current State: 5");
      Serial3.write("I");
      Serial.println("");
      delay(200);
      if (digitalRead(button) == LOW) {
        lcd.clear();
        lcd.print("TEST SUCCESS");
        digitalWrite(buzzer, HIGH);
        delay(1000);
        digitalWrite(buzzer, LOW);
        delay(1000);
        Serial.println("Going back to State 0");
        state = 0;
      }

    case 100:
      Serial.println("Current State: error state");
      while (digitalRead(button) == HIGH) {
        digitalWrite(buzzer, HIGH);
        delay(500);
        digitalWrite(buzzer,LOW);
        delay(500);
      }
      if (digitalRead(button) == LOW) {
        digitalWrite(buzzer, LOW);
        delay(500);
        Serial.println("Going back to State 0");
        state = 0;
      }
  }
}

void handleSerialData(char data) {
  Serial3.write(data);
  if (data == '~') {
    delay(1000);
    state = 0;
  }
  else {
    T = millis();//start time
    while (millis() - T < 10) {
      if (Serial3.available()) {
        a = Serial3.read();
        lcd.clear();
        lcd.print(a);
        lcd.println(" sent back.");
        
        if (a == '~') {
          lcd.println("Returning to State 0");
          delay(1000); // Display the message for 1 second
          lcd.clear(); // Clear the screen before proceeding
          state = 0;
        }
      }
      else {
        Serial.println("Serial communication not available.");
      }
    }
  }
}

Hello gohanhango

Welcome to the worldbest Arduino forum ever.

If you expect a realtime behaiviour of the program you have to get rid of the delay() function. This function blocks the execution.

	Line  51:         delay(200);
	Line  71:         delay(200);
	Line  80:       delay(200);
	Line  88:       delay(200);
	Line 216:         delay(1000);
	Line 218:         delay(1000);
	Line 225:       delay(200);
	Line 227:       delay(200);

Design and code your own none-blocking timer function by using the BlinkWithoutOut example of the IDE.

Have a nice day and enjoy coding in C++.

1 Like

Because you have code like this:

      for (int i = 0; i < 18; i++) {
        pinMode(dp[i], INPUT);
        if (digitalRead(dp[i]) == HIGH) {
//stuff deleted
          state = 100;
        }
      }
// More stuff deleted (but still in the same "case" branch
      state++;

So, you (unecessarily) set the state to 100 x 18 times in the loop then increment it, so it will be 101 at the end of this.

that code structure seems to be repeated several times.

Also, you must include a break statement at the end of each case, otherwise control will just flow into the next case branch.

that is you need to do this:


    case 2:
      Serial.println("Current State: 2");
      
// Stuff deleted
      state++;

     break;       // You need this unless you want execution to continue into case #3.

    case 3:
      Serial.println("Current State: 3");
      Serial.print("You sent: ");
      Serial.write("H");
// stuff deleted.

Why do you say it doesn't?

All of the cases 2, 3, 4, and 5 will drop through to 100 - also case 1, in certain circumstances.

BTW: rather than just giving your states meaningless Magic Number codes, it would be better to give them meaningful names; eg, using an enum...

Changed up the code a bit, included breaks on every end of cases and now it really doesn't come over to the next case when i declare for example that state = 2 on case 1 even though it properly printed on the serial monitor "Analog Tests Done".

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

unsigned long T;

// Analog Pins
const int button = 45;
const int buzzer = 43;
const int pin5V = A1;
const int pin3V = A0;
const int pinAREF = A7;
const int pinRST = A6;
const int pinU = A3;
const int pinV = A2;

// Digital Pins
int dp[18] = {2, 3, 4, 5, 6, 7, 39, 41, 16, 17, 18, 23, 25, 27, 29, 31, 35, 37};

byte state = 0;
char a;

void setup() {
  pinMode(button, INPUT_PULLUP);
  pinMode(buzzer, OUTPUT);

  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.clear();
  
  Serial3.begin(9600);
  Serial.begin(9600);
  
}

void loop() {
  switch (state) {
    case 0:
      lcd.setCursor(0, 0);
      lcd.print(" PLS PRESS THE  ");
      lcd.setCursor(0, 1);
      lcd.print("     BUTTON     ");
      if (digitalRead(button) == LOW) {
        lcd.clear();
        state = 1;
      }
      break;

    case 1:
      Serial.println("Current State: 1");
      
      float valpin5V = 0.0;
      float valpin3V = 0.0;
      float valpinAREF = 0.0;
      float valpinRST = 0.0;
      float valpinU = 0.0;
      float valpinV = 0.0;
      
      for (int i = 0; i < 10; i++) {
        valpin5V += analogRead(pin5V);
        valpin3V += analogRead(pin3V);
        valpinAREF += analogRead(pinAREF);
        valpinRST += analogRead(pinRST);
        valpinU += analogRead(pinU);
        valpinV += analogRead(pinV);
      }
    
      valpin5V /= 10;
      valpin3V /= 10;
      valpinAREF /= 10;
      valpinRST /= 10;
      valpinU /= 10;
      valpinV /= 10;
    
      valpin5V = valpin5V*(5.0/1023);
      valpin3V = valpin3V*(5.0/1023);
      valpinAREF = valpinAREF*(5.0/1023);
      valpinRST = valpinRST*(5.0/1023);
      valpinU = valpinU*(5.0/1023);
      valpinV = valpinV*(5.0/1023);

      Serial.print(F("valpin5V = "));;
      Serial.println(valpin5V);
      Serial.print(F("valpin3V = "));;
      Serial.println(valpin3V);
      Serial.print(F("valpinAREF = "));;
      Serial.println(valpinAREF);
      Serial.print(F("valpinRST = "));;
      Serial.println(valpinRST);
      Serial.print(F("valpinU = "));;
      Serial.println(valpinU);
      Serial.print(F("valpinV = "));;
      Serial.println(valpinV);
      
      if (valpin5V < 4.5) {
        lcd.clear();
        lcd.println("Error in pin 5V");
        state = 100;
        break;
      } else if (valpin3V < 2.5 || valpin3V > 3.5) {
        lcd.clear();
        lcd.println("Error in pin 3V");
        state = 100;
        break;
      } else if (valpinAREF < 2.5 || valpin3V > 3.5) {
        lcd.clear();
        lcd.println("Error in pin AREF");
        state = 100;
        break;
      } else if (valpinRST < 4.5) {
        lcd.clear();
        lcd.println("Error in pin RST");
        state = 100;
        break;
      } else if (valpinU < 4.5) {
        lcd.clear();
        lcd.println("Error in pin U");
        state = 100;
        break;
      } else if (valpinV < 4.5) {
        lcd.clear();
        lcd.println("Error in pin V");
        state = 100;
        break;
      }
      else { 
        Serial.println("Analog Tests Done");
        lcd.println("Analog Tests Done");
        state = 2;
      }
      break;

    case 2:
      Serial.println("Current State: 2");
      
      handleSerialData('T');
      delay(1000);
      handleSerialData('a');
      delay(1000);
      handleSerialData('b');
      delay(1000);
      handleSerialData('~');
      
      lcd.clear();
      state = 3;
      break;

    case 3:
      Serial.println("Current State: 3");
      Serial.print("You sent: ");
      Serial.write("H");
      Serial.println("");
      
      for (int i = 0; i < 18; i++) {
        pinMode(dp[i], INPUT);
        if (digitalRead(dp[i]) == LOW) {
          Serial.print("Error in pin ");
          Serial.println(dp[i]);
          lcd.clear();
          lcd.print("error in pin "); lcd.print(dp[i]);
          delay(1000);
          state = 100;
          break;
        }
      }
      
      lcd.clear();
      state = 4;
      break;

      
    case 4:
      Serial.println("Current State: 4");
      Serial.print("You sent: ");
      Serial.write("L");
      Serial.println("");
      
      for (int i = 0; i < 18; i++) {
        pinMode(dp[i], INPUT);
        if (digitalRead(dp[i]) == HIGH) {
          Serial.print("Error in pin ");
          Serial.println(dp[i]);
          lcd.clear();
          lcd.print("error in pin "); lcd.print(dp[i]);
          delay(1000);
          state = 100;
          break;
        }
      }
      
      lcd.clear();
      state = 5;
      break;

    case 5:
      Serial.println("Current State: 5");
      Serial3.write("I");
      Serial.println("");
      if (digitalRead(button) == LOW) {
        lcd.clear();
        lcd.print("TEST SUCCESS");
        digitalWrite(buzzer, HIGH);
        delay(1000);
        digitalWrite(buzzer, LOW);
        delay(1000);
        Serial.println("Going back to State 0");
        state = 0;
        break;
      }
      break;

    case 100:
      Serial.println("Current State: error state");
      while (digitalRead(button) == HIGH) {
        digitalWrite(buzzer, HIGH);
        delay(500);
        digitalWrite(buzzer,LOW);
        delay(500);
      }
      if (digitalRead(button) == LOW) {
        digitalWrite(buzzer, LOW);
        delay(500);
        Serial.println("Going back to State 0");
        state = 0;
        break;
      }
      break;
  }
}

void handleSerialData(char data) {
  Serial3.write(data);
  if (data == '~') {
    Serial.println("Going back to State 0");
    state = 0;
  }
  else {
    T = millis();//start time
    while (millis() - T < 10) {
      if (Serial3.available()) {
        a = Serial3.read();
        lcd.clear();
        lcd.print(a);
        lcd.println(" sent back.");
        
        if (a == '~') {
          lcd.println("Returning to State 0");
          delay(1000);
          lcd.clear();
          state = 0;
        }
      }
      else {
        Serial.println("Serial communication not available.");
      }
    }
  }
}

add a print immediately above the switch to print what state it thinks it's in ...

Post some examples of the output you get

Thought of that around line 129, and here are the results...

Current State: 1
valpin5V = 5.00
valpin3V = 3.33
valpinAREF = 3.28
valpinRST = 5.00
valpinU = 5.00
valpinV = 5.00
Analog Tests Done
1
2

Is it perhaps because i initialized state as byte?

Edit: Read on the documentation that switch takes either an int or char as parameter and I tried int but still the same.

      else { 
        Serial.println("Analog Tests Done");
        lcd.println("Analog Tests Done");
        Serial.println(state);
        state = 2;
        Serial.println(state);
      }
      break;

For any case where you declare a variable with an initialializer, enclose all the statements in that case within braces. Without that, the compiler sees any subsequent case as within the scope of the variable, but without a valid value because the initialization has been skipped, causing the compiler to skip over all subsequent cases.

1 Like

I concur with @david_2018 wrap case 1 in {}..

good luck.. ~q

Yes, that's a tough one to find... unless you have compiler warnings cranked up, viz:

/var/folders/gz/t92bgl156gdf2066kn0xpc7w0000gq/T/arduino_modified_sketch_330454/
sketch_aug01a.ino:60:13: note:   crosses initialization of 'float valpinV'
       float valpinV = 0.0;
             ^~~~~~~

So... go the the IDE and turn up all the verbosity and warnings level and stuff.

Then heed the red ink. Warnings are just that, but should be run to ground and eliminated, even if you know what you are doing.

Just for those cases (see what I did there?) where you didn't even have an idea you were doing something so bad.

If you don't know what a warning means, you def cannot decide to just ignore it.

a7

Here is your code from post #5 with some small changes and added debug-printing whenever the variable state changes its value

// MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START *
// a detailed explanation how these macros work is given in this tutorial
// https://forum.arduino.cc/t/comfortable-serial-debug-output-short-to-write-fixed-text-name-and-content-of-any-variable-code-example/888298

#define dbg(myFixedText, variableName) \
  Serial.print( F(#myFixedText " "  #variableName"=") ); \
  Serial.println(variableName);

#define dbgi(myFixedText, variableName,timeInterval) \
  { \
    static unsigned long intervalStartTime; \
    if ( millis() - intervalStartTime >= timeInterval ){ \
      intervalStartTime = millis(); \
      Serial.print( F(#myFixedText " "  #variableName"=") ); \
      Serial.println(variableName); \
    } \
  }

#define dbgc(myFixedText, variableName) \
  { \
    static long lastState; \
    if ( lastState != variableName ){ \
      Serial.print( F(#myFixedText " "  #variableName" changed from ") ); \
      Serial.print(lastState); \
      Serial.print( F(" to ") ); \
      Serial.println(variableName); \
      lastState = variableName; \
    } \
  }

#define dbgcf(myFixedText, variableName) \
  { \
    static float lastState; \
    if ( lastState != variableName ){ \
      Serial.print( F(#myFixedText " "  #variableName" changed from ") ); \
      Serial.print(lastState); \
      Serial.print( F(" to ") ); \
      Serial.println(variableName); \
      lastState = variableName; \
    } \
  }
// MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END *



#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

unsigned long T;

// Analog Pins
const int button = 45;
const int buzzer = 43;
const int pin5V = A1;
const int pin3V = A0;
const int pinAREF = A7;
const int pinRST = A6;
const int pinU = A3;
const int pinV = A2;

// Digital Pins
int dp[18] = {2, 3, 4, 5, 6, 7, 39, 41, 16, 17, 18, 23, 25, 27, 29, 31, 35, 37};

byte state = 0;
char a;

void setup() {
  pinMode(button, INPUT_PULLUP);
  pinMode(buzzer, OUTPUT);

  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.clear();

  Serial3.begin(9600);
  Serial.begin(9600);
}

void loop() {

  dbgc("Top of loop",state);
  
  switch (state) {
    case 0:
      lcd.setCursor(0, 0);
      lcd.print(" PLS PRESS THE  ");
      lcd.setCursor(0, 1);
      lcd.print("     BUTTON     ");
      if (digitalRead(button) == LOW) {
        lcd.clear();
        state = 1;
      }
      break;

    case 1:
      //Serial.println("Current State: 1");

      float valpin5V = 0.0;
      float valpin3V = 0.0;
      float valpinAREF = 0.0;
      float valpinRST = 0.0;
      float valpinU = 0.0;
      float valpinV = 0.0;

      for (int i = 0; i < 10; i++) {
        valpin5V += analogRead(pin5V);
        valpin3V += analogRead(pin3V);
        valpinAREF += analogRead(pinAREF);
        valpinRST += analogRead(pinRST);
        valpinU += analogRead(pinU);
        valpinV += analogRead(pinV);
      }

      valpin5V /= 10;
      valpin3V /= 10;
      valpinAREF /= 10;
      valpinRST /= 10;
      valpinU /= 10;
      valpinV /= 10;

      valpin5V = valpin5V * (5.0 / 1023);
      valpin3V = valpin3V * (5.0 / 1023);
      valpinAREF = valpinAREF * (5.0 / 1023);
      valpinRST = valpinRST * (5.0 / 1023);
      valpinU = valpinU * (5.0 / 1023);
      valpinV = valpinV * (5.0 / 1023);

      Serial.print(F("valpin5V = "));;
      Serial.println(valpin5V);
      Serial.print(F("valpin3V = "));;
      Serial.println(valpin3V);
      Serial.print(F("valpinAREF = "));;
      Serial.println(valpinAREF);
      Serial.print(F("valpinRST = "));;
      Serial.println(valpinRST);
      Serial.print(F("valpinU = "));;
      Serial.println(valpinU);
      Serial.print(F("valpinV = "));;
      Serial.println(valpinV);

      if (valpin5V < 4.5) {
        lcd.clear();
        lcd.println("Error in pin 5V");
        state = 100;
        break;
      } else if (valpin3V < 2.5 || valpin3V > 3.5) {
        lcd.clear();
        lcd.println("Error in pin 3V");
        state = 100;
        break;
      } else if (valpinAREF < 2.5 || valpin3V > 3.5) {
        lcd.clear();
        lcd.println("Error in pin AREF");
        state = 100;
        break;
      } else if (valpinRST < 4.5) {
        lcd.clear();
        lcd.println("Error in pin RST");
        state = 100;
        break;
      } else if (valpinU < 4.5) {
        lcd.clear();
        lcd.println("Error in pin U");
        state = 100;
        break;
      } else if (valpinV < 4.5) {
        lcd.clear();
        lcd.println("Error in pin V");
        state = 100;
        break;
      }
      else {
        Serial.println("Analog Tests Done");
        lcd.println("Analog Tests Done");
        state = 2;
      }
      break;

    case 2:
      //Serial.println("Current State: 2");

      handleSerialData('T');
      delay(1000);
      handleSerialData('a');
      delay(1000);
      handleSerialData('b');
      delay(1000);
      handleSerialData('~');

      lcd.clear();
      state = 3;
      break;

    case 3:
      //Serial.println("Current State: 3");
      Serial.print("You sent: ");
      Serial.write("H");
      Serial.println("");

      for (int i = 0; i < 18; i++) {
        pinMode(dp[i], INPUT);
        if (digitalRead(dp[i]) == LOW) {
          Serial.print("Error in pin ");
          Serial.println(dp[i]);
          lcd.clear();
          lcd.print("error in pin "); lcd.print(dp[i]);
          delay(1000);
          state = 100;
          break;
        }
      }

      lcd.clear();
      state = 4;
      break;


    case 4:
      //Serial.println("Current State: 4");
      Serial.print("You sent: ");
      Serial.write("L");
      Serial.println("");

      for (int i = 0; i < 18; i++) {
        pinMode(dp[i], INPUT);
        if (digitalRead(dp[i]) == HIGH) {
          Serial.print("Error in pin ");
          Serial.println(dp[i]);
          lcd.clear();
          lcd.print("error in pin "); lcd.print(dp[i]);
          delay(1000);
          state = 100;
          break;
        }
      }

      lcd.clear();
      state = 5;
      break;

    case 5:
      //Serial.println("Current State: 5");
      Serial3.write("I");
      Serial.println("");
      if (digitalRead(button) == LOW) {
        lcd.clear();
        lcd.print("TEST SUCCESS");
        digitalWrite(buzzer, HIGH);
        delay(1000);
        digitalWrite(buzzer, LOW);
        delay(1000);
        Serial.println("Going back to State 0");
        state = 0;
        break;
      }
      break;

    case 100:
      //Serial.println("Current State: error state");
      while (digitalRead(button) == HIGH) {
        digitalWrite(buzzer, HIGH);
        delay(500);
        digitalWrite(buzzer, LOW);
        delay(500);
      }
      if (digitalRead(button) == LOW) {
        digitalWrite(buzzer, LOW);
        delay(500);
        Serial.println("Going back to State 0");
        state = 0;
        break;
      }
      break;
  }
  dbgc("bottom below switch(state)",state);
}


void handleSerialData(char data) {
  Serial3.write(data);
  if (data == '~') {
    Serial.println("Going back to State 0");
    state = 0;
  }
  else {
    T = millis();//start time
    while (millis() - T < 10) {
      if (Serial3.available()) {
        a = Serial3.read();
        lcd.clear();
        lcd.print(a);
        lcd.println(" sent back.");

        if (a == '~') {
          lcd.println("Returning to State 0");
          delay(1000);
          lcd.clear();
          state = 0;
        }
      }
      else {
        Serial.println("Serial communication not available.");
      }
    }
  }
}

best regards Stefan

Tried out your debugging code and here is the result:

"bottom below switch(state)" state changed from 0 to 1
"Top of loop" state changed from 0 to 1
valpin5V = 5.00
valpin3V = 3.36
valpinAREF = 3.32
valpinRST = 5.00
valpinU = 5.00
valpinV = 5.00
Analog Tests Done
"bottom below switch(state)" state changed from 1 to 2
"Top of loop" state changed from 1 to 2

It skipped case 2 to 5 altogether

I think this one is right, those voltage checks prevented the code from proceeding. Commented them out and it moved to the next case. Thanks.

Edit: not really, my bad, it works now. I wondery why.

Edited the code a bunch, I think it works much better now. It just depends on the DUT now. Thank you.

Edit: included the actual code here.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

unsigned long T;

// Analog Pins
const int button = 45;
const int buzzer = 43;
const int pin5V = A1;
const int pin3V = A0;
const int pinAREF = A7;
const int pinRST = A6;
const int pinU = A3;
const int pinV = A2;

// Digital Pins
int dp[18] = {2, 3, 4, 5, 6, 7, 39, 41, 16, 17, 18, 23, 25, 27, 29, 31, 35, 37};

byte state = 0;
char a;

void setup() {
  pinMode(button, INPUT_PULLUP);
  pinMode(buzzer, OUTPUT);

  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.clear();
  
  Serial3.begin(9600);
  Serial.begin(9600);

  Serial.println("Initial State: 0");
}

void loop() {
  switch (state) {
    case 0: {
      lcd.setCursor(0, 0);
      lcd.print(" PLS PRESS THE  ");
      lcd.setCursor(0, 1);
      lcd.print("     BUTTON     ");
      if (digitalRead(button) == LOW) {
        lcd.clear();
        state = 1;
      }
      break;
    }

    case 1: {
      Serial.println("Current State: 1");
      
      float valpin5V = 0.0;
      float valpin3V = 0.0;
      float valpinAREF = 0.0;
      float valpinRST = 0.0;
      float valpinU = 0.0;
      float valpinV = 0.0;
      
      for (int i = 0; i < 10; i++) {
        valpin5V += analogRead(pin5V);
        valpin3V += analogRead(pin3V);
        valpinAREF += analogRead(pinAREF);
        valpinRST += analogRead(pinRST);
        valpinU += analogRead(pinU);
        valpinV += analogRead(pinV);
      }
    
      valpin5V /= 10;
      valpin3V /= 10;
      valpinAREF /= 10;
      valpinRST /= 10;
      valpinU /= 10;
      valpinV /= 10;
    
      valpin5V = valpin5V*(5.0/1023);
      valpin3V = valpin3V*(5.0/1023);
      valpinAREF = valpinAREF*(5.0/1023);
      valpinRST = valpinRST*(5.0/1023);
      valpinU = valpinU*(5.0/1023);
      valpinV = valpinV*(5.0/1023);

      Serial.print(F("valpin5V = "));;
      Serial.println(valpin5V);
      Serial.print(F("valpin3V = "));;
      Serial.println(valpin3V);
      Serial.print(F("valpinAREF = "));;
      Serial.println(valpinAREF);
      Serial.print(F("valpinRST = "));;
      Serial.println(valpinRST);
      Serial.print(F("valpinU = "));;
      Serial.println(valpinU);
      Serial.print(F("valpinV = "));;
      Serial.println(valpinV);
      
      if (valpin5V < 4.5) {
        lcd.clear();
        lcd.println("Error in pin 5V");
        state = 100;
        break;
      } else if (valpin3V < 2.5 || valpin3V > 3.5) {
        lcd.clear();
        lcd.println("Error in pin 3V");
        state = 100;
        break;
      } else if (valpinAREF < 2.5 || valpin3V > 3.5) {
        lcd.clear();
        lcd.println("Error in pin AREF");
        state = 100;
        break;
      } else if (valpinRST < 4.5) {
        lcd.clear();
        lcd.println("Error in pin RST");
        state = 100;
        break;
      } else if (valpinU < 4.5) {
        lcd.clear();
        lcd.println("Error in pin U");
        state = 100;
        break;
      } else if (valpinV < 4.5) {
        lcd.clear();
        lcd.println("Error in pin V");
        state = 100;
        break;
      }
      
      Serial.println("Analog Tests Done");
      lcd.println("Analog Tests Done");
      state = 2;
      break;
    }

    case 2: {
      Serial.println("Current State: 2");

      Serial.print("Data being sent: ");
      Serial.print("T");
      Serial3.write('T');
      Serial.println("");
      T = millis();//start time
      while (millis() - T < 100) {
        if (Serial3.available()) {
          a = Serial3.read();
          Serial.print(a);
          Serial.println(" sent back.");
          lcd.clear();
          lcd.print(a);
          lcd.println(" sent back.");
        }
        else {
          Serial.println("Serial communication not available.");
        }
      }
      delay(10);

      Serial.print("Data being sent: ");
      Serial.print("a");
      Serial3.write('a');
      Serial.println("");
      T = millis();//start time
      while (millis() - T < 100) {
        if (Serial3.available()) {
          a = Serial3.read();
          Serial.print(a);
          Serial.println(" sent back.");
          lcd.clear();
          lcd.print(a);
          lcd.println(" sent back.");
        }
        else {
          Serial.println("Serial communication not available.");
        }
      }
      delay(10);

      Serial.print("Data being sent: ");
      Serial.print("b");
      Serial3.write('b');
      Serial.println("");
      T = millis();//start time
      while (millis() - T < 100) {
        if (Serial3.available()) {
          a = Serial3.read();
          Serial.print(a);
          Serial.println(" sent back.");
          lcd.clear();
          lcd.print(a);
          lcd.println(" sent back.");
        }
        else {
          Serial.println("Serial communication not available.");
        }
      }
      delay(10);

      Serial.print("Data being sent: ");
      Serial.print("c");
      Serial3.write('c');
      Serial.println("");
      T = millis();//start time
      while (millis() - T < 100) {
        if (Serial3.available()) {
          a = Serial3.read();
          Serial.print(a);
          Serial.println(" sent back.");
          lcd.clear();
          lcd.print(a);
          lcd.println(" sent back.");
        }
        else {
          Serial.println("Serial communication not available.");
        }
      }
      delay(10);
      
      lcd.clear();
      state = 3;
      break;
    }

    case 3: {
      Serial.println("Current State: 3");
      Serial.print("Data being sent: ");
      Serial.print("H");
      Serial3.write('H');
      Serial.println("");

      int hnum = 0;
      for (byte i = 0; i < 18; i++) {
        pinMode(dp[i], INPUT);
        if (digitalRead(dp[i]) == LOW) {
          Serial.print("Error in pin ");
          Serial.println(dp[i]);
          lcd.clear();
          lcd.print("error in pin "); lcd.print(dp[i]);
          delay(200);
        }
        else {
          hnum++;
          Serial.print("NO error in pin ");
          Serial.println(dp[i]);
        }
      }
      if (hnum == 18) {
        lcd.clear();
        state = 4;
        break;
      } else {
        lcd.clear();
        state = 100;
        break;
      }
    }
      
    case 4:{
      Serial.println("Current State: 4");
      Serial.print("Data being sent: ");
      Serial.print("L");
      Serial3.write('L');
      Serial.println("");

      int lnum = 0;
      for (byte i = 0; i < 18; i++) {
        pinMode(dp[i], INPUT);
        if (digitalRead(dp[i]) == HIGH) {
          Serial.print("Error in pin ");
          Serial.println(dp[i]);
          lcd.clear();
          lcd.print("error in pin "); lcd.print(dp[i]);
          delay(200);
        }
        else {
          lnum++;
          Serial.print("NO error in pin ");
          Serial.println(dp[i]);
        }
      }
      if (lnum == 18) {
        lcd.clear();
        state = 5;
        break;
      } else {
        lcd.clear();
        state = 100;
        break;
      }
    }

    case 5: {
      Serial.println("Current State: 5");
      Serial.print("Data being sent: ");
      Serial.print("I");
      Serial3.write('I');
      Serial.println("");
      lcd.clear();
      lcd.print("TEST SUCCESS");
      Serial.println("TEST SUCCESS");
      digitalWrite(buzzer, HIGH);
      delay(500);
      digitalWrite(buzzer, LOW);
      delay(500);
      Serial.println("Going back to State 0");
      state = 0;
      break;
    }
    
    case 100: {
      Serial.println("Current State: error state");
      lcd.clear();
      lcd.print(" ERROR OCCURRED ");
      lcd.setCursor(0, 1);
      lcd.print("  Press Reset   ");
      
      // Wait for the button to be pressed
      while (digitalRead(button) == HIGH) {
        digitalWrite(buzzer, HIGH);
        delay(500);
        digitalWrite(buzzer, LOW);
        delay(500);
      }
      while (digitalRead(button) == LOW) {
          digitalWrite(buzzer, LOW);
          lcd.clear();
          Serial.println("Going back to State 0");
          state = 0;
          delay(500);
          break;
      }
    }
  }
}

If you want an analysis what the reason is
you would have to post your actual sketch

that reply is not on your comment, your debugging code works. And thanks for that :smiley: .

At some point when you are happy with what you have and perhaps likely to stop or slow the seeking of help here you should…

…post a formatted nice version of the code.

This will help us, and it will help others who are following along and it will help any who stumble upon this thread searching for help with their projects.

It isn't a rule or anything, but should be considered part of the implicit contract that might operate over these fora.

HTH and TIA

a7

you're right, my bad. will edit my comment :smiley: .

Hi, @gohanhango

Please do not go back and edit posts, any update please add as a new post.
Editing old posts disrupts the flow of the thread and makes it difficult to follow.

Thanks... Tom.. :smiley: :+1: :coffee: :australia:

Hello @TomGeorge

Will do next time but for comment #14, I didn't include the code previously or before it was edited. I think it is coherent with the thread.

I'll just include "Edit: here is the updated code" as to not confuse anyone.