Hi
I'm working on a project for college which involves a transmitter device taking some gas, temperature and humidity readings and sending them to the monitor/receiver device via RF.
The monitor device receives the data & uses a timer interrupt to store the different readings to the associated variables and display them on an LCD when the appropriate menu selection is made via a keypad.
My problem is that when i press '3' on the keypad for example, to display the gas readings the LCD only displays the gas values that were stored when the key was pressed and won't update the values on screen there after. Even though the serial monitor shows the values updating.
Can anyone suggest how I'd over come this??
My monitor/receiver code is as follows:
#include <Wire.h>
#include "Keypad.h"
#include <LiquidCrystal_I2C.h>
#include <VirtualWire.h>
#include <stdlib.h>
#include <MsTimer2.h>
#include <math.h>
#undef int
#undef abs
#undef double
#undef float
#undef round
/*****************************20x4 LCD I2C***********************************************/
// set the LCD address to 0x27 for a 20 chars 4 line display
// Set the pins on the I2C chip used for LCD connections:
// addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
/*****************************Keypad***********************************************/
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {8, 7, 6, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {4, 3, 2}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
/*****************************Global Variables**********************************************/
volatile char CharLPG[8]; //RF Tx container
volatile char CharCO[8];
volatile char CharSmoke[8];
volatile char CharTemp[8];
volatile char CharHumid[8];
char copy_CharLPG[8]; //RF Tx container
char copy_CharCO[8];
char copy_CharSmoke[8];
char copy_CharTemp[8];
char copy_CharHumid[8];
long LPG, CO, Smoke;
int Temp, Humid, k;
void p_entryMenu();
void mainMenu();
void gases();
/*****************************Setup***********************************************/
void setup()
{
keypad.addEventListener(keypadEvent); // Add an event listener for this keypad
lcd.begin(20,4); // initialize the lcd for 20 chars 4 lines, turn on backlight
MsTimer2::set(2000, store_sensor); // 2s period
MsTimer2::start();
mainMenu();
Serial.begin(9600); // Debugging only
Serial.println("setup");
vw_set_rx_pin(11);
// Initialise the IO and ISR for RX
vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(4000); // Bits per sec
vw_rx_start(); // Start the receiver PLL running
lcd.blink();
}
/*****************************Loop***********************************************/
void loop()
{
char key = keypad.getKey();
int i;
noInterrupts();
for (k = 0; k < 8; k++)
{copy_CharLPG[k] = CharLPG[k];}
for (k = 0; k < 8; k++)
{copy_CharCO[k] = CharCO[k];}
for (k = 0; k < 8; k++)
{copy_CharSmoke[k] = CharSmoke[k];}
for (k = 0; k < 8; k++)
{copy_CharTemp[k] = CharTemp[k];}
for (k = 0; k < 8; k++)
{copy_CharHumid[k] = CharHumid[k];}
interrupts();
if (key == '3')
{ gases();
delay(2000);}
/***DEBUGGING****/
Serial.print(LPG);
Serial.print(" ");
Serial.print(CO);
Serial.print(" ");
Serial.println(Smoke);
delay(250);
}
/*****************************Functions***********************************************/
void keypadEvent(KeypadEvent key){
switch (keypad.getState()){
int i;
case PRESSED:
lcd.clear();
lcd.setCursor(10,2);
lcd.write(key);
delay(1000);
lcd.clear();
if (key == '1')
{
lcd.clear();
lcd.setCursor(10,2);
lcd.write(key);
delay(1000);
p_entryMenu();
}
if (key == '2')
{
lcd.clear();
lcd.setCursor(10,2);
lcd.write(key);
}
if (key == '3')
{
lcd.clear();
lcd.setCursor(4,0);
lcd.write("Gas Only:");
/***LPG***/
lcd.setCursor(0,1);
lcd.write("LPG: ");
lcd.setCursor(13,1);
lcd.write("ppm");
/***CO***/
lcd.setCursor(0,2);
lcd.write("CO: ");
lcd.setCursor(13,2);
lcd.write("ppm");
/***Smoke***/
lcd.setCursor(0,3);
lcd.write("Smoke: ");
lcd.setCursor(13,3);
lcd.write("ppm");
delay (3000);
}
if (key == '4')
{
lcd.clear();
lcd.setCursor(4,0);
lcd.write("Temp/Humid Only:");
/***Display Temp***/
lcd.setCursor(0,1);
lcd.write("Temp: ");
lcd.setCursor(7,1);
lcd.write(copy_CharTemp);
lcd.setCursor(10,1);
lcd.write("*C");
/***Display Humidity***/
lcd.setCursor(0,2);
lcd.write("Humdity: ");
lcd.setCursor(9,2);
lcd.write(copy_CharHumid);
lcd.setCursor(11,2);
lcd.write("%");
}
if (key == '5')
{
lcd.clear();
lcd.setCursor(4,0);
lcd.write("Gas & Temp:");
}
if (key == '6')
{
lcd.setCursor(0,0);
lcd.write("Device 1");
}
if (key == '7')
{
lcd.setCursor(0,0);
lcd.write("Device 2");
}
if (key == '8')
{
lcd.setCursor(0,0);
lcd.write("Device 3");
}
if (key == '*')
{
mainMenu();
}
break;}
}
void store_sensor()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
/***Message***/
if (vw_get_message(buf, &buflen)) // Non-blocking
{ int i;
// Message with a good checksum received, dump it.
digitalWrite(13, true); // Flash a light to show received good message
Serial.println("Got: ");
LPG = atoi(strtok((char*)buf, ","));
CO = atoi(strtok(NULL, ","));
Smoke = atoi(strtok(NULL, ","));
Temp = atoi(strtok(NULL, ","));
Humid = atoi(strtok(NULL, "."));
ltoa(LPG,(char *)CharLPG, 10); //convert from long to a string
ltoa(CO,(char *)CharCO, 10);
ltoa(Smoke,(char *)CharSmoke, 10);
itoa(Temp, (char *)CharTemp, 10); //convert from int to a string
itoa(Humid, (char *)CharHumid, 10);
}
}
void gases()
{
int i;
/***LPG****/
lcd.setCursor(6,1);
for ( i=0;i<7;i++)
{lcd.write(" ");}
lcd.setCursor(6,1);
lcd.write(copy_CharLPG);
/***CO****/
lcd.setCursor(6,2);
for (i=0;i<7;i++)
{lcd.write(" ");}
lcd.setCursor(6,2);
lcd.write(copy_CharCO);
/*****SMOKE*****/
lcd.setCursor(6,3);
for (i=0;i<7;i++)
{lcd.write(" ");}
lcd.setCursor(6,3);
lcd.write(copy_CharSmoke);
}
void p_entryMenu()
{
lcd.setCursor(3,1);
lcd.write("PRE-ENTRY DEV:");
delay(1500);
lcd.clear();
lcd.setCursor(0,0);
lcd.write("Select readings:");
lcd.setCursor(0,1);
lcd.write("3. Gas only");
lcd.setCursor(0,2);
lcd.write("4. Temp/Humid only");
lcd.setCursor(0,3);
lcd.write("5. Gas & Temp/Humid");
lcd.blink();
}
void mainMenu()
{
lcd.clear();
lcd.setCursor(5,0);
lcd.write("MAIN MENU");
lcd.setCursor(0,1);
lcd.write("Select device:");
lcd.setCursor(0,2);
lcd.write("1. Pre-entry Device");
lcd.blink();
}