ok here is my full program, and i need just cu change serial.read to customKeypad.getKey(), but it's not working, i tried with simple program, but every time i get as return '1'
//#include <Arduino.h>
#include <Keypad.h>
//ADC range
#define ADC_MIN 0
#define ADC_MAX 1023
//Voltage range
#define VOLT_MIN 0
#define VOLT_MAX 5
//Temperature define
#define TEMPERATURE_PIN A3
//temperature range
#define TEMP_MIN -40
#define TEMP_MAX 125
//Light define
#define LDR_PIN A0
//light range
#define LIGHT_MIN 10
#define LIGHT_MAX 100
//PIR define
#define PIR_PIN 5
//Ultrasonic define
#define TRIG_PIN 4
#define ECHO_PIN 3
//PHOTO define
#define AMB_LIGHT_PIN A1
#define AMB_LIGHT_MIN 0
#define AMB_LIGHT_MAX 100
//Gas define
#define GAS_PIN A2
#define GAS_MIN 0
#define GAS_MAX 100
//Led define
#define RED_LED_PIN 6
#define GREEN_LED_PIN 7
enum {SENSOR_TEMP, SENSOR_LIGHT, SENSOR_PIR, SENSOR_ULTRASONIC, AMB_LIGHT_SENSOR, GAS_SENSOR, SENSOR_NR_OF };
typedef struct Sensor
{
float (*get)();
char name[20];
char unitName[10];
char button;
unsigned int limit;
} sensor;
//-------------------------------------------------------------------------------
//keypad configuration
const byte ROWS = 3;
const byte COLS = 3;
char hexaKeys[ROWS][COLS] =
{
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'}
};
byte rowPins[ROWS] = {13, 12, 11};
byte colPins[COLS] = {10, 9, 8};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
//-------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
//Get functions for each sensor
float GetTemperature(void)
{
int inputData = analogRead(TEMPERATURE_PIN);
float celsius = map(((inputData - 20) * 3.04), ADC_MIN, ADC_MAX, TEMP_MIN, TEMP_MAX);
return celsius;
}
float GetLight(void)
{
int inputData = analogRead(LDR_PIN);
float voltage = map(inputData, ADC_MIN, ADC_MAX, VOLT_MIN, VOLT_MAX);
float light = map(voltage, VOLT_MIN, VOLT_MAX, LIGHT_MIN, LIGHT_MAX);
return light;
}
float GetPIR(void)
{
int sensorState = digitalRead(PIR_PIN);
return sensorState;
}
float GetDistance(void)
{
long duration;
int distance;
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
distance = duration * 0.034 / 2;
return distance;
}
float GetAmbLight(void)
{
int inputData = analogRead(AMB_LIGHT_PIN);
float amb_light = map(inputData * 2, ADC_MIN, ADC_MAX, AMB_LIGHT_MIN, AMB_LIGHT_MAX);
return amb_light;
}
float GetGas(void)
{
int inputData = analogRead(GAS_PIN);
float gas = map((inputData * 2.57), ADC_MIN, ADC_MAX, GAS_MIN, GAS_MAX);
return gas;
}
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------
//declare all sensors
sensor sensorList[SENSOR_NR_OF] =
{
{GetTemperature, "Temperature", "celsius", '1', 40},
{GetLight, "Light", "points", '2', 50},
{GetPIR, "Motion", "yes/no", '3', 1},
{GetDistance, "Distance", "cm", '4', 120},
{GetAmbLight, "Ambient light", "points", '5', 40},
{GetGas, "Gas", "density", '6', 40}
};
//-----------------------------------------------------------------------------------
//--------------------------------------------------------
//init all sensors
void InitSensors(void)
{
pinMode(TEMPERATURE_PIN, INPUT); //temperature
pinMode(LDR_PIN, INPUT); //light
pinMode(PIR_PIN, INPUT); //pir
pinMode(TRIG_PIN, OUTPUT); //ultrasonic trigger
pinMode(ECHO_PIN, INPUT); //ultrasonic echo
pinMode(AMB_LIGHT_PIN, INPUT); //ambient light
pinMode(GAS_PIN, INPUT); //gas
//led pins
pinMode(GREEN_LED_PIN, OUTPUT);
pinMode(RED_LED_PIN, OUTPUT);
}
//---------------------------------------------------------
//---------------------------------------------------------
//Printing process
char button;
void PrintSensorData()
{
if(button >= '1' && button <='9')
{
for(int i = 0; i < SENSOR_NR_OF; i++)
{
if (button == sensorList[i].button)
{
if(sensorList[i].get() < sensorList[i].limit)
{
Serial.print(sensorList[i].name);
Serial.print(": ");
Serial.print(sensorList[i].get());
Serial.print(" ");
Serial.print(sensorList[i].unitName);
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(GREEN_LED_PIN, HIGH);
}
else
{
Serial.print("Sensor value off limit");
digitalWrite(GREEN_LED_PIN, LOW);
digitalWrite(RED_LED_PIN, HIGH);
}
}
}
Serial.println();
}
}
//---------------------------------------------------------
void setup(void)
{
Serial.begin(9600);
InitSensors();
}
void loop(void)
{
while(Serial.available())
button = Serial.read();
//button = customKeypad.getKey();
PrintSensorData();
delay(200);
}