Hi again guys, more code troubles: I have got most of the functions working the way i want them ,i can take measurements of light. However I want to be able to navigate through my LCD with buttons it has. Up/down/left/right/Select
I want it such that the program starts up, displays a message. Press select then gives a choice
left for continuous measurement and right for a hold feature.
If i hit left, i get the continuous measurement but whilst trying to implement a back button it means i need to a while loop and its just failing and i cant navigate through that screen. If i hit right on the first screen, i can take single measurements with a press of the button, i can keep on pressing right to make more measurements. Hitting select takes me back to the first screen.
Depending on where i am in the whole interface, sometimes buttons i dont want to do anything at the moment -do stuff anyway - i think simply because of their position within the switch case
advice in how to control my flow and maybe how to call certain cases within another case would help. Thanks in advance
#include <LiquidCrystal.h>
/******************************************
*
*
*
******************************************/
LiquidCrystal lcd(8,9,4,5,6,7);
/*****************************************
* Definitions
*****************************************/
int lcd_key = 0;
int adc_key_in = 0;
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
/* ---------Button Information--------------
Right: 0.00V ; 0 @ 10 bit
UP: 0.71V ; 145 @ 10 bit
DOWN: 1.61V ; 329 @ 10 bit
LEFT: 2.47V ; 505 @ 10 bit
SELECT: 3.62V ; 741 @ 10 bit
-------------------------------------------*/
/*******************************************
* Setup
*******************************************/
void setup()
{
lcd.begin(16,2); // start
lcd.setCursor(0,0);
lcd.print("Hi");
lcd.setCursor(0,1);
lcd.print("There");
delay(3000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("MENU: Press");
lcd.setCursor(0,1);
lcd.print("SELECT to start");
}
/*****************************************************
* Main loop
******************************************************/
void loop()
{
lcd_key = read_LCD_buttons(); //read the buttons
button_switch_case(lcd_key);
}
/**********************************************
* Reading the buttons
***********************************************/
int read_LCD_buttons()
{
adc_key_in = analogRead(A0); // read value
if(adc_key_in > 1000) return btnNONE;
if(adc_key_in < 50) return btnRIGHT;
if(adc_key_in < 250) return btnUP;
if(adc_key_in < 450) return btnDOWN;
if(adc_key_in < 650) return btnLEFT;
if(adc_key_in < 850) return btnSELECT;
return btnNONE; // If all else fails
}
/*********************************************************************************
* Switch case for buttons
**********************************************************************************/
void button_switch_case(int lcd_key)
{
switch (lcd_key) //Depending on button pressed, perform action
{
/*******************************************************************************
* Right Button
*******************************************************************************/
case btnRIGHT:
{
lcd.clear();
float photoDiode_1 = analogRead(A1);
float photoDiode_2 = analogRead(A1);
float photoDiode_3 = analogRead(A1);
float averagetoprint = ((photoDiode_1)+(photoDiode_2)+(photoDiode_3))/3;
float voltage_1 = photoDiode_1 * ((5.0 / 1023.0)*1000);
float voltage_2 = photoDiode_2 * ((5.0 / 1023.0)*1000);
float voltage_3 = photoDiode_3 * ((5.0 / 1023.0)*1000);
float averageVoltage = ((voltage_1)+(voltage_2)+(voltage_3))/3;
float lux = 2.67857 * averageVoltage;
lcd.print("SEL:BACK >:AGAIN");
lcd.setCursor(0,1);
lcd.print(lux);
lcd.print( " Lux");
delay(1000);
//if(adc_key_in<=650 && adc_key_in>=450)lcd_key=btnLEFT;
break;
}
/*******************************************************************************
* Left Button
*******************************************************************************/
case btnLEFT:
{
while(!lcd_key < btnSELECT)
{
lcd.clear();
float photoDiode_1 = analogRead(A1);
float photoDiode_2 = analogRead(A1);
float photoDiode_3 = analogRead(A1);
float averagetoprint = ((photoDiode_1)+(photoDiode_2)+(photoDiode_3))/3;
float voltage_1 = photoDiode_1 * ((5.0 / 1023.0)*1000);
float voltage_2 = photoDiode_2 * ((5.0 / 1023.0)*1000);
float voltage_3 = photoDiode_3 * ((5.0 / 1023.0)*1000);
float averageVoltage = ((voltage_1)+(voltage_2)+(voltage_3))/3;
float lux = 2.67857 * averageVoltage;
lcd.print("SEL: BACK");
lcd.setCursor(0,1);
lcd.print(lux);
lcd.print( " Lux");
delay(1000);
}
}
/*******************************************************************************
* UP Button
*******************************************************************************/
case btnUP:
{
btnSELECT;
break;
}
/*******************************************************************************
* Down Button
*******************************************************************************/
case btnDOWN:
{
lcd.clear();
lcd.print("Calibration");
break;
}
/*******************************************************************************
* Select Button
*******************************************************************************/
case btnSELECT:
{
lcd.clear();
lcd.print("<: Continuous");
lcd.setCursor(0,1);
lcd.print(">: Hold,^ RETURN");
break;
}
/*******************************************************************************
* No Button Pressed
*******************************************************************************/
case btnNONE:
{
break;
}
}
}
some of the code i currently have in the while loop etc are wrong, i was just experimenting
Thanks in advance