0

I am a beginner with C++ but used to be good with BASIC (2 decades ago lol). I'm building a peristaltic metered doser that allows you to input desired mL on a 4x4 keypad and then when 'fill' button is pressed it doses the desired mLs through the peristaltic.

I'm still drafting/printing the peristaltic housing and all of that but in the meantime I want to get as much of the coding done as possible. It's still incomplete, but one of the functions I want the doser to have is a 'PRIME/FLUSH' button.

Originally I wanted the button to run the pump as long as the button is held down to prime the tubing with solution or flush the tubing with DI water. But I haven't had luck figuring out how to use a 4x4 button in momentary switch mode.

A workaround I had was to have the button be an on/off button. So press it once, pump turns on, press it again, pump turns off. Since the pump isn't printed yet I've been using the onboard LED as a tester for the coding (hence the LED_BUILTIN).

The issue, from my research I figured what I would need was an if/else statement. When the button is pressed (in this case the 'a' button), it reads the LED_BUILTIN status and if its HIGH it turns it off and if its LOW it turns it on.

But the 'if' part is working, it turns it on, but the 'else' part is not. When the LED is already on and the 'a' button is pressed, it stays on and doesn't rewrite to LOW.

Code below, any advice?

#include <KeypadShield.h>

KeypadShield keypad;

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

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

char Data[15];
byte data_count = 0;

int x;

char customKey;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);

  keypad.begin();
  lcd.begin(20,4); 
    lcd.setCursor(4,0);
    lcd.print("Let's Fill!");
    lcd.setCursor(4,1);
    lcd.print("Designed by");
    lcd.setCursor(4,2);
    lcd.print("JP");
    lcd.setCursor(5,3);
    lcd.print("2019 v1.0");

    delay (2000);

    lcd.clear();

    lcd.setCursor(1,0);
    lcd.print("Enter volume in mL");
    lcd.setCursor(3,1);
    lcd.print("on keypad then");
    lcd.setCursor(5,2);
    lcd.print("press DONE");
    lcd.setCursor(0,3);
    lcd.print("mL:");
}

void softReset()
{
asm volatile ("  jmp 0");
}

void loop() 
{
    customKey = keypad.getNextKeypress();

    if (customKey) 
    {
      if (customKey == 'a') 
      {
        if (digitalRead(LED_BUILTIN == LOW)) 
        {
          digitalWrite(LED_BUILTIN, HIGH);
          lcd.clear();
          lcd.setCursor(5,1);
          lcd.print("FLUSHING");
        } 
        else
        {
          digitalWrite(LED_BUILTIN, LOW);
          lcd.clear();
          lcd.setCursor(1,0);
          lcd.print("Enter volume in mL");
          lcd.setCursor(3,1);
          lcd.print("on keypad then");
          lcd.setCursor(5,2);
          lcd.print("press DONE");
          lcd.setCursor(0,3);
          lcd.print("mL:");
        }
      }

      if (customKey == 'd')
      {
       softReset();
      }

      if (customKey != 'p' && customKey != 'a' && customKey != 'b' && customKey != 'c' && customKey != 'd' && customKey != 's')
      {
        Data[data_count] = customKey; // store char into data array
        lcd.setCursor(data_count+3,4); // move cursor to show each new char
        lcd.print(Data[data_count]); // print char at said cursor
        data_count++; // increment data array by 1 to store new char, also keep track of the number of chars entered
        x = atoi (Data); //convert string to integer 'x' for use in equations
      }

     if (customKey == 'p')
      {
       lcd.clear();
       lcd.setCursor(7,0);
        lcd.print(x);
        lcd.setCursor(Data[data_count]+13,0);
        lcd.print("mL");
        lcd.setCursor(0,1);
        lcd.print("FILL --> fill");
        lcd.setCursor(0,2);
        lcd.print("RESET --> change mL");
        lcd.setCursor(0,3);
        lcd.print("FLUSH --> finish");

      }
    }
};
  • 2
    digitalRead(LED_BUILTIN) == LOW move the ). (you read a LED?) – Juraj Feb 22 at 16:31
  • Holy crow... thanks Juraj. Brain fart right there... that fixed the issue! And yes, the LED right now is just a placeholder for my peristaltic pump (once I finish drafting and printing the housing). I'm just using it to make sure everything works. Thanks again! – Jay Feb 22 at 16:34
  • the pump can be read? – Juraj Feb 22 at 16:35
  • @Juraj I was thinking I could read whether or not the pump was on or off, or some variation of the concept – Jay Feb 22 at 16:42
  • 1
    by accident on Uno you will read the pin state last set, but better have a state variable – Juraj Feb 22 at 16:45
7

(Heh... before even seeing the code I guessed you'd done this...)

You are comparing a pin number, not the results of digitalRead(), and then passing the results of that comparison to digitalRead():

if (digitalRead(LED_BUILTIN == LOW)) 

It should read:

if (digitalRead(LED_BUILTIN) == LOW) 

Note the order of the brackets: LED_BUILTIN is passed to digitalRead(), and then the result of calling that function is compared to the value LOW.

  • 1
    Thanks Majenko! Major brain fart on my part... but that solved the issue. haha. Spend so much time looking for the big problems that the little ones just stay right under your nose... – Jay Feb 22 at 16:36
  • 1
    @Jay Absolutely. It's called "Code Blindness". – Majenko Feb 22 at 16:37

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.