So here's what I'm aiming for:
I have a 16x2 LCD hooked up via an adafruit LCD shield (LCD Shield Kit w/ 16x2 Character Display - Only 2 pins used! [BLUE AND WHITE] : ID 772 : $19.95 : Adafruit Industries, Unique & fun DIY electronics and kits) and my plan is to turn it into a soft of multi-function display, using a wifi shield to supply weather, email counts, whatever.
My first step was to make a basic temperature display using an MCP9808 sensor (MCP9808 High Accuracy I2C Temperature Sensor Breakout Board : ID 1782 : $4.95 : Adafruit Industries, Unique & fun DIY electronics and kits). Each "page" of the display would be keyed to a button on the shield (there are 5 total, plus a reset). I have the simple sketch written (see below) but I'm trying to figure out the best way to work the button coding. Currently all works, but the button to display temperature only polls the sensor once, then stops.
What would the best way to work the buttons so that when each is pressed, the resultant code would loop continuously, until another button is pressed?
// include the library code:
#include <Wire.h>
#include <Adafruit_MCP23017.h>
#include <Adafruit_RGBLCDShield.h>
#include <Adafruit_MCP9808.h>
// The shield uses the I2C SCL and SDA pins. On classic Arduinos
// this is Analog 4 and 5 so you can't use those for analogRead() anymore
// However, you can connect other I2C sensors to the I2C bus and share
// the I2C bus.
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
Adafruit_MCP9808 tempsensor = Adafruit_MCP9808();
void setup() {
Serial.begin(9600);
lcd.begin(16,2);
tempsensor.begin(0x19);
}
void temp () {
float c = tempsensor.readTempC();
lcd.setCursor(0,0);
lcd.print("Current Temp.");
lcd.setCursor(0,1);
lcd.print(c);
lcd.setCursor(6,1);
lcd.print("deg. C");
}
void loop() {
uint8_t buttons = lcd.readButtons();
if (buttons) {
lcd.clear();
lcd.setCursor(0,0);
if (buttons & BUTTON_UP) {
lcd.print("UP ");
}
if (buttons & BUTTON_DOWN) {
lcd.print("DOWN ");
}
if (buttons & BUTTON_LEFT) {
lcd.print("LEFT ");
}
if (buttons & BUTTON_RIGHT) {
lcd.print("RIGHT ");
}
if (buttons & BUTTON_SELECT) {
temp();}
}
}