Just so y'all know, I am very new to arduino.
I am using the Liquid Crystal LCD for arduino. It has 6 buttons, which I am trying to use to input values. The other piece to this project is a GPS which is not part of my problem, so do not concern yourself with it.
My problem is when I run this code, the LCD will not take in any inputs. It constantly blinks and almost looks like its just running through the loop and not taking in any inputs except reset. My best guess is to have something that would pause the code and wait for the user to input the value necessary. Then after the input is made, it continues on with whatever it is supposed to do. I tried a while loop to satisfy that condition where the SELECT button was pressed. However, it does not seem to be doing any better.
Any suggestions would be greatly helpful.
//Sample using LiquidCrystal library
#include <LiquidCrystal.h>
#include <math.h>
#include <stdlib.h>
// select the pins used on the LCD panel
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
// define some values used by the panel and buttons
int lcd_key = 0;
int adc_key_in = 0;
int lcd_key_value = 1500;
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
float latitudeD = 0;
float longitudeD = 0;
float latitudeL = 7357.1870;
float longitudeL = 4123.4700;
float distance = 0;
int iteration = 1;
String latitudeDestination = "";
String longitudeDestination = "";
void setup(){
}
void loop(){
if (iteration = 1) {
latitudeDestination += inputCoordinates();
iteration++;
longitudeDestination += inputCoordinates();
}
latitudeD = latitudeDestination.charAt(0)*pow(10,3) + latitudeDestination.charAt(1)*pow(10,2) + latitudeDestination.charAt(2)*pow(10,1) +
latitudeDestination.charAt(3)*pow(10,0) + latitudeDestination.charAt(4)*pow(10,-1) + latitudeDestination.charAt(5)*pow(10,-2) +
latitudeDestination.charAt(6)*pow(10,-3) + latitudeDestination.charAt(7)*pow(10,-4);
longitudeD = longitudeDestination.charAt(0)*pow(10,3) + longitudeDestination.charAt(1)*pow(10,2) + longitudeDestination.charAt(2)*pow(10,1) +
longitudeDestination.charAt(3)*pow(10,0) + longitudeDestination.charAt(4)*pow(10,-1) + longitudeDestination.charAt(5)*pow(10,-2) +
longitudeDestination.charAt(6)*pow(10,-3) + longitudeDestination.charAt(7)*pow(10,-4);
//Main Operation (navigating to destination)
lcd.setCursor(16, 2);
lcd.print(latitudeDestination + longitudeDestination);
lcd.setCursor(0,1);
distance = calculateDistance(latitudeD,longitudeD,latitudeL,longitudeL);
lcd.print(distance);
}
String inputCoordinates() {
lcd.begin(16, 2); // set LCD to top row
if(iteration = 1) {
lcd.print("Input Latitude"); // print instructions to operator
}
else {
lcd.print("Input Longitude"); // print instructions to operator
}
int space = 0;
int digit[] = {0,0,0,0,0,0,0,0,0};
int run = 1;
lcd.setCursor(0,1);
lcd.print("0000.0000");
delay(200);
while(run = 1) {
while(lcd_key_value > 1000) {
lcd_key_value = analogRead(0);
lcd_key = read_LCD_buttons(); // read the buttons
}
switch (lcd_key) { // depending on which button was pushed, we perform an action
case btnRIGHT: {
space++;
if (space == 4) {
space++;
}
}
case btnLEFT: {
space--;
if (space == 4) {
space--;
}
}
case btnUP: {
digit[space] = digit[space] + 1;
lcd.setCursor(space,1);
lcd.print(digit[space]);
}
case btnDOWN: {
digit[space] = digit[space] - 1;
lcd.setCursor(space,1);
lcd.print(digit[space]);
}
case btnSELECT: {
run = 0;
}
case btnNONE:{
}
}
String digit0 = "";
digit0 += digit[0];
String digit1 = "";
digit1 += digit[1];
String digit2 = "";
digit2 += digit[2];
String digit3 = "";
digit3 += digit[3];
String digit5 = "";
digit5 += digit[5];
String digit6 = "";
digit6 += digit[6];
String digit7 = "";
digit7 += digit[7];
String digit8 = "";
digit8 += digit[8];
String coordinate = "";
coordinate += digit0 + digit1 + digit2 + digit3 + digit5 + digit6 + digit7 + digit8;
return coordinate;
}
}
int read_LCD_buttons(){
adc_key_in = analogRead(0); // read the value from the sensor
// my buttons when read are centered at these valies: 0, 144, 329, 504, 741
// we add approx 50 to those values and check to see if we are close
if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 195) return btnUP;
if (adc_key_in < 380) return btnDOWN;
if (adc_key_in < 555) return btnLEFT;
if (adc_key_in < 790) return btnSELECT;
return btnNONE; // when all others fail, return this...
}
float calculateDistance(float lat1, float lon1, float lat2, float lon2) {
float d;
d = 2*asin(sqrt(pow(sin((lat1-lat2)/2),2)+cos(lat1)*cos(lat2)*pow(sin((lon1-lon2)/2),2)));
return d;
}