4x3 keypad via pcf8574. How can I reboot on "*"

If this was a button I'd wire a reset. Unfortunately I'm using a pcf8574 to wire a keypad to an arduino nano 33 iot (needed the io, and I couldn't get the keypad to work properly when directly connected).

I need the arduino to reset stopping all current functions when I hit *. I can't seem to get this working properly. This does compile and my motors work great. My issue is just this one problem.

Restated: How do I break out of my program at any point on "*"?

//
//    FILE: test.ino
//  AUTHOR: Aaron Paluzzi
// VERSION: 0.1.0
// PURPOSE: .
//
// HISTORY:
// 2021-04-26 0.1.0 initial version


// PCF8574
//    pin p0-p3 rows
//    pin p4-p7 columns
// 4x4 or smaller keypad.

#include "Wire.h"
#include "I2CKeyPad.h"

const uint8_t KEYPAD_ADDRESS = 0x20;

I2CKeyPad keyPad;
const int buttonPin = 12;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin
int motor1 = 2;
int motor2 = 3;
int motor3 = 4;
int bigmotor = 5;

int toggle1 = 0;
int toggleP = 0;

uint32_t start, stop;
uint32_t lastKeyPressed = 0;
int keyRead;


// These values are for setting the pipe length and speed of the pipe per second in a digital sense (on off).  Volume is calculated automatically.
// Chem pumps
float blueMlPerSec = 1
float bluePipeInnerDiametermm = 1
float bluePipetubelength = 1
//Water pump
float waterMlPerSec = 1
float waterPipeInnerDiametermm = 1
float waterPipetubeLength = 1


//Ignore these values as they are auto calculated.  Required infrequently to prime blue pumps
float blueMlToPrime = ((bluePipeInnerDiametermm * bluePipeInnerDiametermm * 3.14 *bluePipetubelength) / 4000);  //Auto calculates the total length of pipe to prime
float waterMlToPrime = ((waterPipeInnerDiametermm * waterPipeInnerDiametermm * 3.14 * waterPipetubeLength) / 4000); //Auto calculates the total length of pipe to prime

//Formulas

//Formula1
float blueChem1Form1Ml = 1;
float blueChem2Form1Ml = 0;
float blueChem3Form1Ml = 0;
float waterForm1 = 0;

//Formula2
float blueChem1Form2Ml = 1;
float blueChem2Form2Ml = 0;
float blueChem3Form2Ml = 0;
float waterForm2 = 0;

//Formula3
float blueChem1Form3Ml = 0;
float blueChem2Form3Ml = 1;
float blueChem3Form3Ml = 1;
float waterForm3 = 0;

//Formula4
float blueChem1Form4Ml = 0;
float blueChem2Form4Ml = 1;
float blueChem3Form4Ml = 1;
float waterForm4 = 1;

//Formula5
float blueChem1Form5Ml = 0;
float blueChem2Form5Ml = 1;
float blueChem3Form5Ml = 1;
float waterForm5 =1;

//Formula6
float blueChem1Form6Ml = 0;
float blueChem2Form6Ml = 1;
float blueChem3Form6Ml = 1;
float waterForm6 = 1;

//Formula7
float blueChem1Form7Ml = 0;
float blueChem2Form7Ml = 0;
float blueChem3Form7Ml = 0;
float waterForm7 = 0;

//Formula8
float blueChem1Form8Ml = 0;
float blueChem2Form8Ml = 0;
float blueChem3Form8Ml = 0;
float waterForm8 = 0;

//Formula9
float blueChem1Form9Ml = 0;
float blueChem2Form9Ml = 1.65;
float blueChem3Form9Ml = 1.65;
float waterForm9 = 500;

//Formula0
float blueChem1Form0Ml = 0;
float blueChem2Form0Ml = 0;
float blueChem3Form0Ml = 0;
float waterForm0 = 916;


float mixMaster (float blChem1, float blChem2, float blChem3, float blPerSec, float water, float waterPerSec, int waterCtrl)
{

         if (toggle1 == 0)
          {
            toggle1 = 1;
            Serial.print("Chemical selected.");
            Serial.print("\n");
            
            if (blChem1 !=0)
            {
//              Serial.print("Chem 1 pumping.");
//              Serial.print("\n");
//              digitalWrite(motor1, HIGH);              
//              Serial.print("Pump on for ");
//              Serial.print("\t");
//              Serial.print((blChem1 / blPerSec));
//              Serial.print("sec \n");
              delay(1000 * (blChem1 / blPerSec));
              digitalWrite(motor1, LOW);
//              Serial.print("Chem 1 end.");
//              Serial.print("\n");
            } else {
              Serial.print("Chem 1 skip \n");
            }
            
            if (blChem2 !=0)
            {
//              Serial.print("Chem 2 pumping.");
//              Serial.print("\n");
              digitalWrite(motor2, HIGH);
              delay(1000 * (blChem2 / blPerSec));
              digitalWrite(motor2, LOW);
//              Serial.print("Chem 2 end.");
//              Serial.print("\n");
            } else {
              Serial.print("Chem 2 skip \n");
            }
            
            if (blChem3 !=0)
            {
//              Serial.print("Chem 3 pumping.");
//              Serial.print("\n");
              digitalWrite(motor3, HIGH);
               delay(1000 * (blChem3 / blPerSec));
              digitalWrite(motor3, LOW);
//              Serial.print("Chem 3 end.");
//              Serial.print("\n");
            } else {
              Serial.print("Chem 3 skip \n");
            }
            if (water !=0)
            {
//              Serial.print("Water pumping.");
//              Serial.print("\n");
              analogWrite(bigmotor, waterCtrl);
//              Serial.print("Pump on for ");
//              Serial.print("\t");
//              Serial.print((water / waterPerSec));
//              Serial.print("sec \n");
              delay(1000 * (water / waterPerSec));
              analogWrite(bigmotor, 0);
//              Serial.print("Water end.");
//              Serial.print("\n");
            }
                                  
            Serial.print("ML of Chemicals used");
            Serial.print("\t");
            Serial.print(blChem1);
            Serial.print("\t");
            Serial.print(blChem2);
            Serial.print("\t");
            Serial.print(blChem3);

            toggle1 = 0;
           } 
        
      }


void setup()
{
  Serial.begin(115200);
  Serial.println(__FILE__);

  Wire.begin();
  Wire.setClock(400000);
  if (keyPad.begin(KEYPAD_ADDRESS) == false)
  {
    Serial.println("\nERROR: cannot communicate to keypad.\nPlease reboot.\n");
    while(1);
  }

  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  // set initial LED state
  //digitalWrite(ledPin, ledState);
  // initialize motor as an output.
  pinMode(motor1, OUTPUT);
  pinMode(motor2, OUTPUT);
  pinMode(motor3, OUTPUT);
  pinMode(bigmotor, OUTPUT);
}

//Reset function
void(* resetFunc) (void) = 0;//declare reset function at address 0


void loop()
{
  uint32_t now = millis();
  char keys[] = "123A456B789C*0#DNF";  // N = Nokey, F = Fail

  if (now - lastKeyPressed >= 370)
  {
    lastKeyPressed = now;

    start = micros();
    uint8_t idx = keyPad.getKey();
    stop = micros();

    
    if (String(keys[idx]) != "N")
    {
      Serial.print(keys[idx]);
      Serial.print("\t");
      Serial.println(stop - start);

      
      if (String(keys[idx]) == "1")
      {
        mixMaster (blueChem1Form1Ml, blueChem2Form1Ml, blueChem3Form1Ml, blueMlPerSec, waterForm1, waterMlPerSec, 255);
      } 
      
      if (String(keys[idx]) == "2")
      {
        mixMaster (blueChem1Form2Ml, blueChem2Form2Ml, blueChem3Form2Ml, blueMlPerSec, waterForm2, waterMlPerSec, 255);
      } 
            
      if (String(keys[idx]) == "3")
      {
          mixMaster (blueChem1Form3Ml, blueChem2Form3Ml, blueChem3Form3Ml, blueMlPerSec, waterForm3, waterMlPerSec, 255); 
      } 
            
      if (String(keys[idx]) == "4")
      {
          mixMaster (blueChem1Form4Ml, blueChem2Form4Ml, blueChem3Form4Ml, blueMlPerSec, waterForm4, waterMlPerSec, 255);        
      }

      if (String(keys[idx]) == "5")
      {
          mixMaster (blueChem1Form5Ml, blueChem2Form5Ml, blueChem3Form5Ml, blueMlPerSec, waterForm5, waterMlPerSec, 255);
      }

      if (String(keys[idx]) == "6")
      {
          mixMaster (blueChem1Form6Ml, blueChem2Form6Ml, blueChem3Form6Ml, blueMlPerSec, waterForm6, waterMlPerSec, 255);
      }

      if (String(keys[idx]) == "7")
      {
          mixMaster (blueChem1Form7Ml, blueChem2Form7Ml, blueChem3Form7Ml, blueMlPerSec, waterForm7, waterMlPerSec, 255);
      }

      if (String(keys[idx]) == "8")
      {
          mixMaster (blueChem1Form8Ml, blueChem2Form8Ml, blueChem3Form8Ml, blueMlPerSec, waterForm8, waterMlPerSec, 255);
      }
      
      if (String(keys[idx]) == "9")
      {
          mixMaster (blueChem1Form9Ml, blueChem2Form9Ml, blueChem3Form9Ml, blueMlPerSec, waterForm9, waterMlPerSec, 255);
      } 
      if (String(keys[idx]) == "0")
      {
          mixMaster (blueChem1Form0Ml, blueChem2Form0Ml, blueChem3Form0Ml, blueMlPerSec, waterForm0, waterMlPerSec, 255);
      } 
      if (String(keys[idx]) == "*")
      {
          resetFunc(); //call reset 
      } 
      
      if (String(keys[idx]) == "#")
      {
        Serial.println("Priming pump lines");
        mixMaster (blueMlToPrime, blueMlToPrime, blueMlToPrime, blueMlPerSec, waterMlToPrime, waterMlPerSec, 255);
        Serial.println("Priming Complete");
      }
            
    }
  }
}

 

// -- END OF FILE --

Hi, @kaplah
The mixMaster () function has very large delays.
Delay stops processing completely, preventing any further action from being detected while it lasts.
Redo the routines with delay using millis ();

Are you using the analog pins for a purpose?
In modules with Atmeaga, they can be used as digital pins, I believe that in nano 33 IOT, they can also be used.
I'll see if I can find out in the datasheet deo nano 33 IOT.

RV mineirin

Several comments beyond using millis() in place of delay(). Check out the Blink Without Delay example in the IDE (File->examples->02.Digital->Blink Without Delay)

You should also check out the several things at the same time tutorial

Look into arrays... Your code could be vastly simplified since all you ever do is call mixMaster() with different parameters.

Also, it is very inefficient to convert a single char to a String just to compare it to another single char String. Just do the comparison directly (notice the single quotes, not double quotes)

//    if (String(keys[idx]) != "N")
    if (keys[idx]) != 'N')
    {
    ...

You are also missing some trailing ';' on a few of your lines

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