Not leaving Case 0

After a kind soul helped me get the code “running”, I attempted to make this code work. I’m running the code off of an Arduino MEGA 2560, and using some NEMA 17 stepper motors and two TB6600 motor drivers as an extruder and a pouring mechanism, but I’ve come to discover that the program seems to not want to move past case 0. The system is supposed to allow for the user to put in a numerical value, hit A, then move on to the motor/snipping tool. Does anyone know how to get the code to continue? If so, I’d be extremely grateful. Thank you!

#include <Keypad.h>
#include <max6675.h>
#include <LiquidCrystal_I2C.h>
const byte ROWS = 4;
const byte COLS = 4;
//Assign key values in 4x4 matrix
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {22, 23, 24, 25};  //connect to the row pinouts of the keypad
byte colPins[COLS] = {26, 27, 28, 29};  //connect to the column pinouts of the keypad

int PUL1 = 7;                  //define Pulse pin 1
int DIR1 = 6;                  //define Direction pin 1
int ENA1 = 2;                  //define Enable Pin 1
int PUL2 = 9;                  //define Pulse pin 2
int DIR2 = 8;                  //define Direction pin 2
int ENA2 = 3;                  //define Enable Pin 2
int SNP = 32;                 //defines Snipping pin
int HEA = 33;                  //defines Heating pin

int snipLength = 25600;              //Set to snip after each 8 revolutions

int v = 0;                         // Sets initial volume value to 0 until changed
unsigned long m = (v / 0.0033);    //Identifies the amount of revolutions needed to
unsigned long t = (m / 0.0003125); //Indicates number of motogr turns to reach volume
int n = 0;                         //determines placement of numbers on LCD board
long snip = 25600;                 // number of rotations before snipping is needed
int dn = 0;                        //sets up a function for once the dispensing is finished
long r = 1600;
float temp1 = 0;

LiquidCrystal_I2C lcd(0x27, 20, 4);
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
String inputString;
long inputInt;
int thermoDO = 10;
int thermoCS = 11;
int thermoCLK = 12;
MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);

char key = "";
int state = 0;
float requiredTemperature = 700; //degrees Fahrenheit
float currentTemperature = 0; //degrees Fahrenheit
long requiredHeatingTime =  180000; //3 minutes, in milliseconds
long timeStamp = millis();


void setup() {
  pinMode (PUL1, OUTPUT);              //set pin to output for motor control
  pinMode (DIR1, OUTPUT);              //set pin to output for motor control
  pinMode (ENA1, OUTPUT);              //set pin to output for motor control
  pinMode (PUL2, OUTPUT);              //set pin to output for motor control
  pinMode (DIR2, OUTPUT);              //set pin to output for motor control
  pinMode (ENA2, OUTPUT);              //set pin to output for motor control
  pinMode (SNP, OUTPUT);               //Set pin for actuator control
  pinMode (HEA, OUTPUT);
  Serial.begin(9600);
  inputString.reserve(10); // maximum number of digit for a number is 10, change if needed

  Serial.println("MAX6675 test");
  // wait for MAX chip to stabilize
  delay(300);

  lcd.init();

  // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Input the volume of ");
  lcd.setCursor(0, 1);
  lcd.print("the final product!");

}

void loop() {

  switch (state) {
    case 0: //waiting for a command on the Keypad
      while (state == 0) {
        key = keypad.getKey();

        if (key >= '0' && key <= '9') {     // only act on numeric keys
          lcd.setCursor(n, 2);
          lcd.print(key);
          n = n + 1;
          inputString += key;               // append new character to input string
        }
        else if (key == 'A') {
          if (n > 0) {
            inputInt = inputString.toInt(); // YOU GOT AN INTEGER NUMBER
            v = inputInt; //Sets the number of steps for the project
            m = (v / 0.003262);
            t = (m / 0.00033);
            inputString = "";               // clear input
            lcd.setCursor(0, 0);
            lcd.print("                   ");
            Serial.println ("0,0");
            lcd.setCursor(0, 0);
            lcd.print("                   ");
            Serial.println ("0,1");
            lcd.setCursor(0, 2);
            lcd.print("                   ");
            Serial.println ("0,2");
            lcd.setCursor(0, 0);
            lcd.print("Now processing:");
            lcd.setCursor(17, 0);
            lcd.print(v);
            lcd.setCursor(0, 1);
            lcd.print("cubic inches of tin.");
            lcd.setCursor(0, 2);
            lcd.print("Please wait...");
          }
        }
        else if (key == 'B') {
          inputString = "";                 // clear input
          lcd.setCursor(0, 2);
          lcd.print("                  ");
          n = 0;
        }
      }
      state = 1;
      Serial.println("State 1");
      break;

    case 1: //extruding filament
      snip = 25600;
      if (t <= 0) {
        digitalWrite(ENA1, LOW);
      }
      else if (t > 0) {
        if (snip <= 0) {
          digitalWrite(SNP, HIGH);
          delay(1500);
          digitalWrite(SNP, LOW);
          delay(1500);
          snip = 25600;
        }
        else if (snip > 0) {
          digitalWrite(ENA1, HIGH);      //enables motor operation
          digitalWrite(DIR1, LOW);      //
          digitalWrite(PUL1, HIGH);      //write high pulse
          delayMicroseconds(50);       //delay motorS seconds
          digitalWrite(PUL1, LOW);       //write low pulse
          delayMicroseconds(50);       //delay motorT seconds
          t = t - 1;
          snip = snip - 1;
        }
      }
      state = 2;
      break;

    case 2: //turn on the heater
      digitalWrite(HEA, HIGH);
      state = 3;
      break;

    case 3: //monitor the temperature, turn state to 4 once heat is achieved
      // DO STUFF
      lcd.backlight();
      lcd.setCursor(0, 3);
      lcd.print("Temperature:");
      temp1 = 0;
      while (temp1 < 90) {
        currentTemperature = thermocouple.readFahrenheit();
        lcd.setCursor(13, 3);
        lcd.print(currentTemperature);
        delay(250); // For the MAX6675 to update, you must delay AT LEAST 250ms between reads!
      }
      state = 4;
      break;

    case 4: //wait for appropriate time
      delay(60000);
      state = 5;
      break;

    case 5: //Turning off heating element
      digitalWrite(HEA, LOW);
      r = 1600;
      break;

    case 6: //open the gate and pour the material into the mold
      if (r > 800 && r <= 1600) {
        digitalWrite(ENA2, HIGH);     //Enables motor operation
        digitalWrite(DIR2, HIGH);      //Sets direction
        digitalWrite(PUL2, HIGH);     //High pulse
        delayMicroseconds(50);       //Delays motor pulse
        digitalWrite(PUL2, LOW);      //Low pulse
        delayMicroseconds(50);       //Delays motor pulse
        r = r - 1;
      }
      state = 7;
      break;

    case 7:
      delay (60000);
      if (r <= 800 && r > 0) {
        delay (5);
        digitalWrite(ENA2, HIGH);     //Enables motor operation
        digitalWrite(DIR2, LOW);      //Sets direction
        digitalWrite(PUL2, HIGH);     //High pulse
        delayMicroseconds(50);       //Delays motor pulse
        digitalWrite(PUL2, LOW);      //Low pulse
        delayMicroseconds(50);       //Delays motor pulse
        r = r - 1;
      }
      state = 8;
      break;

    case 8:
      delay (60000);
      lcd.setCursor(0, 0);
      lcd.print("                   ");
      lcd.setCursor(0, 1);
      lcd.print("                   ");
      lcd.setCursor(0, 2);
      lcd.print("                   ");
      lcd.setCursor(0, 3);
      lcd.print("                   ");
      lcd.setCursor(0, 0);
      lcd.print("Please wait as the");
      lcd.setCursor(0, 1);
      lcd.print("metal cools.");
      lcd.setCursor(0, 2);
      lcd.print("Please wait 3 min.");
      delay (180000);

      state = 9;
      break;

    case 9:
      lcd.setCursor(0, 0);
      lcd.print("                   ");
      lcd.setCursor(0, 1);
      lcd.print("                   ");
      lcd.setCursor(0, 2);
      lcd.print("                   ");
      lcd.setCursor(0, 3);
      lcd.print("                   ");
      lcd.setCursor(0, 0);
      lcd.print("You may now remove");
      lcd.setCursor(0, 1);
      lcd.print("your completed mold.");
      lcd.setCursor(0, 2);
      lcd.print("Thank you for using");
      lcd.setCursor(0, 3);
      lcd.print("Prometheum Smelter!");
      delay(30000);

      state = 0; //Return to beginning of program
      break;
  }
}

Change things a little so Case 0 is case 10 instead.
I recall I had some problem reading a keypad and Case 0 being an issue.

Hello,
In the while loop at case 0 there is no codethat changes the value of the variable case.
So you are in an infinite loop, processing input but the program never leaves.
Are you sure your code is reliable?
Best Regards,
Johi.

Looks state = 1 needs to move up into the if { } and else if { } sections?

There’s no need for the while loop - if state 0 is detecting a key and doesn’t see one, simple return and loop() will go round again, still in state 0.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.