Search function on lcd

Hi All,

How to search array values using search function on LCD shield(for example if i pressed number 2 the stt & prices 2nd values to be displayed like Masal 70 and if i press number 16,it should display the value).Any one tried this type of function.Help please.

[u]String Array Values[/u]

char Stt [100]={"Rice","Oil","Masal","HCare","HairC","Baby","Pickle","Oral Care","Dry Fruits","Noodles"}; int prices[] = {0, 52, 70, 15, 62, 75, 175, 58, 44, 17, 30};

Regards JR

There are probably hundreds of ways to write this, but I’m assuming the function needs to return the two values to you. So I’d probably do something like:

void setup() {
  Serial.begin(9600);
}

void loop() {
  char buffer[10];
  int val, price, ret;
  
  if (Serial.available() > 0) {
    val = Serial.read() - '0';
    ret = FindItemData(val, buffer, &price);
    if (ret) {
      Serial.print(buffer);
      Serial.print("   ");
      Serial.println(price);
    } 
  }  
}
int FindItemData(int whichOne, char *item, int *price)
{
   static char *Stt[100]={"Rice","Oil","Masal","HCare","HairC","Baby","Pickle","Oral Care","Dry Fruits","Noodles"};
   static int prices[100] = {0, 52, 70, 15, 62, 75, 175, 58, 44, 17, 30};
   int flag = 0;

   if (whichOne < (sizeof(Stt) / sizeof(Stt[0]))) {
     strcpy(item, Stt[whichOne]);
     *price = prices[whichOne];
     flag = 1;
   } else {
     *item = NULL;
     *price = -1;
     flag = 0;
   }
   return flag;
}

Hi econjack,

First of all thanks for your code it's showing perfectly from 0 to 9,and if i type 10 it showing 1 and 0 values.Kindly suggest and how to implement this for LCD.

[u]Output from serial[/u] Rice 0 Oil 52 Masal 70 HCare 15 HairC 62 Baby 75 Pickle 175 Oral Care 58 Dry Fruits, 44

Noodles 17

Oil 52 Rice 0

Thanks & Regards JR

Any please give me some suggestion.

Regards
JR

Hi econjack,

First of all thanks for your code it's showing perfectly from 0 to 9,and if i type 10 it showing 1 and 0 values.Kindly suggest and how to implement this for LCD.

Output from serial Rice 0 Oil 52 Masal 70 HCare 15 HairC 62 Baby 75 Pickle 175 Oral Care 58 Dry Fruits, 44

Noodles 17

Oil 52 Rice 0

Thanks & Regards JR

My original code was only mean to be used for a single digit index:

  if (Serial.available() > 0) {
    val = Serial.read() - '0';
    ret = FindItemData(val, buffer, &price);

If you want val to work with values greater than 9, you need to change the way the Serial input is handled and the search function:

void setup() {
  Serial.begin(9600);
}

void loop() {
  char buffer[10];
  int index, val, price, ret;

  if (Serial.available() > 0) {
    index = Serial.readBytesUntil('\n', buffer, 9);  // Look for newline or max of 9 chars
    buffer[index] = '\0';                            // It's now a string
    val = atoi(buffer);                              // Now it's an int
    ret = FindItemData(val, buffer, &price);
    if (ret) {
      Serial.print(buffer);
      Serial.print("   ");
      Serial.println(price);
    }
  }
}
int FindItemData(int whichOne, char *item, int *price)
{
  static char *Stt[100] = {"Rice", "Oil", "Masal", "HCare", "HairC", "Baby", "Pickle", "Oral Care", "Dry Fruits", "Noodles"};
  static int prices[100] = {0, 52, 70, 15, 62, 75, 175, 58, 44, 17, 30};
  int flag = 0;

  for (int i = 0; i < (sizeof(Stt) / sizeof(Stt[0])); i++) {
    if (prices[i] == whichOne) {
        strcpy(item, Stt[i]);
        *price = prices[i];
        flag = 1;
    }
  }
  return flag;
}

Hello econjack,

Thank you.But no output is displaying on serial.

Regards JR

Hi econjack,

Sorry for sending you a private message for this programming question.

Regards JR

Hello econjack,

I have mixed your old code in this.now it is showing perfectly.The changes i made in that code is copied below.Thanks for your kind help.
Regards
JR

void setup() {
  Serial.begin(9600);
}

void loop() {
  char buffer[10];
  int index, val, price, ret;

  if (Serial.available() > 0) {
 index = Serial.readBytesUntil('\n', buffer, 9);  // Look for newline or max of 9 chars
    buffer[index] = '\0';                            // It's now a string
    val = atoi(buffer);                              // Now it's an int
    ret = FindItemData(val, buffer, &price);
    if (ret) {
      Serial.print(buffer);
      Serial.print("   ");
      Serial.println(price);
    }
  }
}
int FindItemData(int whichOne, char *item, int *price)
{
  static char *Stt[100] = {"Rice", "Oil", "Masal", "HCare", "HairC", "Baby", "Pickle", "Oral Care", "Dry Fruits", "Noodles","Health","Fitness","Medi","Test"};
  static int prices[100] = {0, 52, 70, 15, 62, 75, 175, 58, 44, 17, 30,25,35,40};
  int flag = 0;
   if (whichOne < (sizeof(Stt) / sizeof(Stt[0]))) {
     strcpy(item, Stt[whichOne]);
     *price = prices[whichOne];
     flag = 1;
   } else {
     *item = NULL;
     *price = -1;
     flag = 0;
  }
  return flag;
}

The code I posted in #5 worked perfectly.

yes indeed.

Hi econjack,

How to change from serial to lcd using keypad.I tried several options nothing worked.Please give some suggestion.

void loop() {
  char buffer[10];
  int index, val, price, ret;
  if (Serial.available() > 0) {
 index = Serial.readBytesUntil('\n', buffer, 9);
    buffer[index] = '\0';                            // It's now a string
    val = atoi(buffer);                              // Now it's an int
    ret = FindItemData(val, buffer, &price);
    if (ret) {
      Serial.print(buffer);
      Serial.print("   ");
      Serial.println(price);
    }
  }
}

Hi All,

How to change from serial to LCD using keypad.I tried several options nothing worked.Please give some suggestion.Below is the code

void loop() {
  char buffer[10];
  int index, val, price, ret;
  if (Serial.available() > 0) {
  index = Serial.readBytesUntil('\n', buffer, 9);
    buffer[index] = '\0';                            // It's now a string
    val = atoi(buffer);                              // Now it's an int
    ret = FindItemData(val, buffer, &price);
  if (ret) {
      Serial.print(buffer);
      Serial.print("   ");
      Serial.println(price);
    }
  }
}

Is this code one of the options? Why show this one and not the others?

Can you write a test sketch that verifies that the keypad is connected?

Below is the complete code.The problem is it is working on serial,but implementing on lcd it only fetching 1st value only not going further.

Regards
JR

#include <LiquidCrystal.h>
#include "Keypad.h"
#include "Wire.h" // for I2C LCD
// Initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
// keypad type definition
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char customKey;
int total = 0;
char keys[ROWS][COLS] =
{ {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {2, 3, 11, 12}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {A2, A3, A4, A5}; // connect to the column pinouts of the keypad
int count = 0;
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
//===========================================================================
void setup() {
  //Set the characters and column numbers.
  //lcd.init();
  Serial.begin(9600);
  lcd.begin(16, 2);
  // set up the switch pin as an input
  lcd.clear();
}
void loop() {
   //customKey = myKeypad.getKey();
   char key = keypad.getKey();
   switch(customKey) 
   {
     case 'C':
   guru();
    break;
   }
  if (key != NO_KEY)
  {
    lcd.print(key);
    count++;
         if (count == 17)
    {
      lcd.clear();
      count = 0;
    }
  }
}
int FindItemData(int whichOne, char *item, int *price)
{

  static char *Stt[100] = {"Rice", "Oil", "Masal", "HCare", "HairC", "Baby", "Pickle", "Oral Care", "Dry Fruits", "Noodles", "Midha"};
  static int prices[100] = {0, 52, 70, 15, 62, 75, 175, 58, 44, 17, 30, 35};
  int flag = 0;
  if (whichOne < (sizeof(Stt) / sizeof(Stt[0]))) {
    strcpy(item, Stt[whichOne]);
    *price = prices[whichOne];
    flag = 1;
  } else {
    *item = NULL;
    *price = -1;
    flag = 0;
  }
  return flag;
}
void guru()
{
  char key = keypad.getKey();
  char buffer[10];
  int index, val, price, ret;
  if (count>key) {
   // digitalRead;
    //count++;
       //index = Serial.readBytesUntil('\n', buffer, 9);  // Look for newline or max of 9 chars
    buffer[index] = '\0';                            // It's now a string
    val = atoi(buffer);                              // Now it's an int
    ret = FindItemData(val, buffer, &price);
    if (ret) {
      Serial.print(buffer);
      Serial.print("   ");
      Serial.println(price);
      delay(200);
      //lcd.clear();
    }
  }
}

Please some one give me a idea. Regards JR

Hi MorganS,

Any solutions or suggestions?. Regards JR

seejr: Please some one give me a idea. Regards JR

Don't cross post! Threads merged.

seejr: Hi MorganS,

Any solutions or suggestions?. Regards JR

Don't bump your thread after two minutes!

The problem is it is working on serial,but implementing on lcd it only fetching 1st value only not going further.

It works on serial, but not on LCD, eh? Very strange. Let me look into that for a couple of years.

[quote author=Nick Gammon link=msg=2231330 date=1431588809] Don't cross post! Threads merged.

Don't bump your thread after two minutes! [/quote]

Sorry for that. Regards JR