Skip to content

Instantly share code, notes, and snippets.

@diegogslomp
Last active August 27, 2018 14:46
Show Gist options
  • Select an option

  • Save diegogslomp/ca9dc04ff42b2cfd3b98317084280f50 to your computer and use it in GitHub Desktop.

Select an option

Save diegogslomp/ca9dc04ff42b2cfd3b98317084280f50 to your computer and use it in GitHub Desktop.
[arduino] Grower with lcd menu, temp/moisture sensors - under dev
/*
Temperature and moisture sensors displayed at 16x9 lcd.
Menu navigation using 2-axis joystick.
The circuit:
* LCD RS pin to digital pin 6
* LCD Enable pin to digital pin 7
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* LCD VSS pin to ground
* LCD VCC pin to 5V
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
Joystick ---> Arduino:
* GND GND
* +5V 5V
* VRx A0
* VRy A1
* SW 9
Sensors:
* Temperature A2
* Moisture A3
by Diego Gobbi Slomp
Based on this examples:
* http://www.arduino.cc/en/Tutorial/LiquidCrystalHelloWorld
* https://www.addicore.com/v/vspfiles/downloadables/Product%20Downloadables/Project_Interface_Kit/Joystick_2Axis_with_Switch.txt
*/
// include the library code:
#include <LiquidCrystal.h>
//Arduino pins attached to joystick
#define joystick_switch_pin 9
#define joystick_x_pin A0
#define joystick_y_pin A1
//Temp sensor
#define temperature_sensor_pin A2
//Moisture sensor
#define moisture_sensor_pin A3
//Joystick values
#define up 0
#define right 1
#define down 2
#define left 3
#define enter 4
#define none 5
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 6, en = 7, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// Menus
// TODO: Submenus
String main_menu[] = {"1.Temperature:","2.Moisture:","3.Lights:"};
// Vars to display in lcd
int current_menu_item;
String *current_menu;
// Used to check joystick state
int last_joy_read;
float read_temperature() {
float temperatureValue = analogRead(temperature_sensor_pin);
temperatureValue = (5.0 * analogRead(temperature_sensor_pin) * 100.0) / 1024; // TODO: check float int cast
return temperatureValue;
}
int read_moisture() {
int moistureValue = analogRead(moisture_sensor_pin); // TODO: check moisture value before return
return moistureValue;
}
void check_sensors() {
float temperature = read_temperature();
int moisture = read_moisture();
main_menu[0] = "1. Temp: " + String(temperature) + "C";
main_menu[1] = "2. Moist: " + String(moisture);
}
int read_joystick() {
int output = none;
// read all joystick values
int X_Axis = analogRead(joystick_x_pin); // read the x axis value
int Y_Axis = analogRead(joystick_y_pin); // read the y axis value
Y_Axis = map(Y_Axis, 0, 1023, 1023, 0); // invert the input from the y axis so that pressing the stick forward gives larger values
int SwitchValue = digitalRead(joystick_switch_pin); // read the state of the switch
SwitchValue = map(SwitchValue, 0, 1, 1, 0); // invert the input from the switch to be high when pressed
if (SwitchValue == 1){
output = enter;
} else if (X_Axis >= 900) {
output = right;
} else if (X_Axis <= 100) {
output = left;
} else if (Y_Axis >= 900) {
output = up;
} else if (Y_Axis <= 100) {
output = down;
}
return output;
}
void print_line(int line, String text) {
lcd.setCursor(0, line);
lcd.print(" ");
lcd.setCursor(0, line);
lcd.print(text);
}
void move_up(){
if (current_menu_item <= 0){
current_menu_item = sizeof(current_menu);
} else {
current_menu_item--;
}
}
void move_down(){
if (current_menu_item >= sizeof(current_menu)){
current_menu_item = 0;
} else {
current_menu_item++;
}
}
void move_right(){
// TODO: enter submenu
// TODO: change selected value
// TODO: increase selected value (ex: defaut temperature, timers, etc..)
}
void move_left(){
// TODO: go back mainmenu
// TODO: go back submenu
// TODO: decrease selected value (ex: defaut temperature, timers, etc..)
}
void setup() {
// arduino default once time function
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// set up joy pins
pinMode(joystick_switch_pin, INPUT_PULLUP);
// Print template on lcd.
check_sensors();
print_line(0, "Main Menu:");
print_line(1, main_menu[current_menu_item]);
// Init vars
current_menu_item = 0;
last_joy_read = none;
current_menu = main_menu;
}
void loop() {
// "main" arduino program
check_sensors();
int current_joy_read = read_joystick();
if (current_joy_read != last_joy_read) {
last_joy_read = current_joy_read;
switch (current_joy_read) {
case up:
move_up();
break;
case down:
move_down();
break;
case right:
move_right();
break;
case left:
move_left();
break;
default: break;
}
delay(100);
}
// clear line and print values to lcd
print_line(1, current_menu[current_menu_item]);
delay(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment