Show Posts
Pages: [1]
1  Using Arduino / Displays / Re: LCD Input and Output on: December 12, 2012, 12:55:19 am
Thanks for all the help I have been getting. I have been taking this time to modify my project to the solutions you have provided. I will post the results hopefully soon.

EDIT: I changed up the code a lot and moved things around. I made some progress where it seems like the buttons are starting to listen to me. However, they are not completing their necessary function.
When I hit the up button, I want the digit value at the cursor to increase in value. The down button would decrease the cursored value. The left and right move the cursor. The left and right buttons also make sure it doesn't hover over the '.' character.

The problem now is when I hit a button, it spits out 8 more zeroes and will do so with everytime I hit the button. Once the LCD is full, the LCD goes blank.

I have tried the clear() option in a few areas where it might be duplicating. But the issue continues. I feel like it is somewhere in my case statements.

Code:
void loop(){
  if(iteration == 1) {
  lcd.print("Input Latitude"); // print instructions to operator
  }
  else {
  lcd.print("Input Longitude"); // print instructions to operator
  }
  while( iteration < 3) {
  iteration++;
  int space = 0;
  int digit[] = {0,0,0,0,0,0,0,0};
  int run = 1;
  lcd.setCursor(0,1);
  lcd.print("00000000");
  delay(200);
  while(run = 1) {
    lcd.setCursor(0,1);
    lcd_key = read_LCD_buttons();
    while(lcd_key == 5) {   
      lcd_key = read_LCD_buttons();
    }
    switch(lcd_key) {
    case btnRIGHT: {     
        if(space = 8) space = 0;
        else space++;
      }
      case btnLEFT: {     
        if(space = 0) space = 7;
        else space--;
      }
     case btnUP: {
        digit[space] = digit[space] + 1;
      }
      case btnDOWN: {
        digit[space] = digit[space] - 1;
      }   
     case btnSELECT: {     
        run = 0;
      }
    }
      digit0 += digit[0];
      digit1 += digit[1];
      digit2 += digit[2];
      digit3 += digit[3];
      digit4 += digit[4];
      digit5 += digit[5];
      digit6 += digit[6];
      digit7 += digit[7];
      coordinate = "";
      coordinate += digit0 + digit1 + digit2 + digit3 + digit4 + digit5 + digit6 + digit7;
      lcd.setCursor(16,2);
      lcd.clear();
      if(iteration == 2) {
      lcd.print("Input Latitude"); // print instructions to operator
      }
      else {
      lcd.print("Input Longitude"); // print instructions to operator
      }
      lcd.setCursor(0,1);
      lcd.print(coordinate);
      delay(200);
    }
    if (iteration == 2) {
    latitudeDestination = coordinate;
    }
    else {
    longitudeDestination = coordinate;
    }
  }
2  Using Arduino / Displays / Re: LCD Input and Output on: December 11, 2012, 11:26:21 pm
1. http://www.dfrobot.com/wiki/index.php?title=Arduino_LCD_KeyPad_Shield_%28SKU:_DFR0009%29

2.
Code:
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(Serial.available() > 0) {   
      lcd.write(Serial.read());  // 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...
}

3. Delays don't work. Even if I made a long enough delay, I am trying to get it so that the user hits a button (the select button) and the data is passed into the code. There are a series of 0's displayed across the LCD. The user is supposed to move the cursors (left or right) and then change the digit values using (higher or lower). Once the user is satisfied with the numbers displayed, the user hits the select button to then input another value.

I have tried placing a while loop that would only be satisfied until that select button was hit. However, this did not solve the problem, as all the LCD continued to not show any input changes.
3  Using Arduino / Displays / LCD Input and Output on: December 11, 2012, 10:59:04 pm
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.

Code:
//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;
}
Pages: [1]