1

I'm working on a research project that allows me to press a button on a matrix that then outputs a parameter like temperature, humidity etc. But I cannot find out what is wrong with the buttons, when I press them nothing is happening. Thank you very much! I've attached what the connection between the Arduino and the keypad looks like.

#include <EEPROM.h>
#include <GravityTDS.h>
#include "DHT.h"
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <Servo.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Keypad.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define DHTPIN 7
#define DHTTYPE DHT11   // DHT 11 
#define SensorPin A0            //pH meter Analog output to Arduino Analog Input 0
#define Offset 0.00            //deviation compensate
#define LED 13
#define samplingInterval 20
#define printInterval 800
#define ArrayLenth  40    //times of collection
#define PUMPPIN 9
#define ONE_WIRE_BUS 4
int pHArray[ArrayLenth];   //Store the average value of the sensor feedback
int pHArrayIndex = 0;
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
Servo myservo;
GravityTDS gravityTds;
DHT dht(DHTPIN, DHTTYPE);

char incomingByte;
String inputString;
int pump = 9; // Output for pump Control
double avg;
int h;
float t;
float tdsValue;
float temperature = t;
static unsigned long samplingTime = millis();
static unsigned long printTime = millis();
static float pHValue, voltage;

const byte ROWS = 4;
const byte COLS = 4;

char hexaKeys[ROWS][COLS] = {
  {'A', 'B', 'C', 'D'},
  {'E', 'F', 'G', 'H'},
  {'I', 'J', 'K', 'L'},
  {'M', 'N', 'O', 'P'}
};

byte rowPins[ROWS] = {44, 42, 40, 38};
byte colPins[COLS] = {36, 34, 32, 30};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void setup() {

  lcd.begin();
  pinMode(pump, OUTPUT); //sets the digital pin as output
  digitalWrite(pump, LOW); // Initial state of the pump
  Serial.begin(9600);
  gravityTds.setPin(A1);
  gravityTds.setAref(5.0);
  gravityTds.setAdcRange(1024);
  gravityTds.begin();
  dht.begin();
  sensors.begin();
}

double averagearray(int* arr, int number) {
  int i;
  int max, min;
  long amount = 0;
  if (number <= 0) {
    Serial.println("Error number for the array to avraging!/n");
    return 0;
  }
  else if (number < 5) { //less than 5, calculated directly statistics
    for (i = 0; i < number; i++) {
      amount += arr[i];
    }
    avg = amount / number;
    return avg;
  } else {
    if (arr[0] < arr[1]) {
      min = arr[0]; max = arr[1];
    }
    else {
      min = arr[1]; max = arr[0];
    }
    for (i = 2; i < number; i++) {
      if (arr[i] < min) {
        amount += min;      //arr<min
        min = arr[i];
      } else {
        if (arr[i] > max) {
          amount += max;  //arr>max
          max = arr[i];
        } else {
          amount += arr[i]; //min<=arr<=max
        }
      }//if
    }//for
    avg = (double)amount / (number - 2);
  }//if
  return avg;
}

void loop() {
  lcd.print("working so far. test1");
  delay(5000);
  char customKey = customKeypad.getKey();
  int pumptime = 1000;
  switch (customKey) {
    case 'A':
      // pump
      digitalWrite(pump, HIGH); // on pump
      delay(pumptime);
      digitalWrite(pump, LOW);//off pump
      break;
    case 'B':
      // add pump time
      pumptime += 500;
      break;
    case 'C':
      // less pump time
      pumptime -= 500;
      break;
    case 'D':
      // reset pump time
      pumptime = 1000;
      break;
    case 'E':
      //temp
      sensors.requestTemperatures();
      lcd.setCursor(0, 0);
      lcd.print("Celsius temperature: ");
      lcd.print(sensors.getTempCByIndex(0));
      lcd.print((char)223);
      lcd.print("C");
      delay(5000);
      lcd.clear();
      break;
    case 'F':
      // Temperature & humidity
      h = dht.readHumidity();
      t = dht.readTemperature();
      lcd.setCursor(0, 0);
      lcd.print("Humidity:");
      lcd.print(h);
      lcd.print("%");
      lcd.setCursor(0, 1);
      lcd.print("Temperature:");
      lcd.print(t);
      lcd.print((char)223);
      lcd.print("C");
      delay(5000);
      lcd.clear();
      break;
    case 'G':
      // TDS
      h = dht.readHumidity();
      t = dht.readTemperature();
      temperature = t;
      gravityTds.setTemperature(temperature);
      gravityTds.update();
      tdsValue = gravityTds.getTdsValue();
      lcd.setCursor(0, 0);
      lcd.print("TDS: ");
      lcd.print(tdsValue);
      lcd.print("ppm");
      delay(5000);
      lcd.clear();
      delay(5000);
      lcd.clear();
      break;
    case 'H':
      // pH
      lcd.clear();
      lcd.setCursor(0, 0);
      if (millis() - samplingTime > samplingInterval)
      {
        pHArray[pHArrayIndex++] = analogRead(SensorPin);
        if (pHArrayIndex == ArrayLenth)pHArrayIndex = 0;
        voltage = averagearray(pHArray, ArrayLenth) * 5.0 / 1024;
        pHValue = 3.5 * voltage + Offset;
        samplingTime = millis();
      }
      if (millis() - printTime > printInterval)  //Every 800 milliseconds, print a numerical, convert the state of the LED indicator
      {
        lcd.print("Voltage:");
        lcd.print(voltage, 2);
        lcd.print("    pH value: ");
        lcd.println(pHValue, 2);
        digitalWrite(LED, digitalRead(LED) ^ 1);
        printTime = millis();
      }
      delay(5000);
      lcd.clear();
      break;
    case 'I':
      // test buttons
      lcd.print("buttons are working");
      delay(5000);
      lcd.clear();
      break;
    case 'J':
      //
      lcd.print("buttons are working");
      delay(5000);
      lcd.clear();
      break;
    case 'K':
      //
      lcd.print("buttons are working");
      delay(5000);
      lcd.clear();
      break;
    case 'L':
      //
      lcd.print("buttons are working");
      delay(5000);
      lcd.clear();
      break;
    case 'M':
      //
      lcd.print("buttons are working");
      delay(5000);
      lcd.clear();
      break;
    case 'N':
      //
      lcd.print("buttons are working");
      delay(5000);
      lcd.clear();
      break;
    case 'O':
      //
      lcd.print("buttons are working");
      delay(5000);
      lcd.clear();
      break;
    case 'P':
      //
      lcd.print("buttons are working");
      delay(5000);
      lcd.clear();
      break;
  }//switch
  lcd.print("working so far. test2");
  myservo.write(0);
  delay(10000);
  myservo.write(90);

  delay(1800000);

}

enter image description here

2
  • start by reducing your code ... read a keypress and print the result ... nothing else
    – jsotola
    Mar 10 at 5:24
  • i suspect that you are not pressing the button in the millisecond window after the 30 minute delay ... are you actually waiting 30 minutes for the window when keypress is accepted?
    – jsotola
    Mar 10 at 5:27
1

Imho, your delay function in loop() is a root cause. First, you should remove the below code

  myservo.write(0);
  delay(10000);
  myservo.write(90);

  delay(1800000);

Then,please reduce delay(5000) -> delay(200). Build and execute your code. Press and hold a single button and take a look what happen in LCD. At least, you able to confirm the keypad hardware works well or not.

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.