将Arduino uno与液晶显示器,键盘,gsm sim900A和GPS连接

时间:2018-03-31 04:29:42

标签: arduino gps gsm lcd keypad

我已经开发了两个代码但是将它们组合在一起并没有给我想要的结果。 以下代码正确显示LCD上键盘的文本消息输入,并将其作为文本消息完美打印。

<fieldset class="fieldset">
<div class="div">1</div>
<div class="div">2</div>
<div class="div">3</div>
</fieldset>

此代码为我提供了存储在字符串Message中的正确GPS位置值。

#include <SoftwareSerial.h>  // serial software library for interfacing gsm module
SoftwareSerial Serial1(2, 3); // RX, TX  // connect gsm Tx at D2 and Rx at D3

#include<LiquidCrystal.h>     // LCD library for interfacing LCD
LiquidCrystal lcd(14,15,16,17,18,19);   // connect rs,en,d4,d5,d6,d7 respectevely

int i=0;

#include <Keypad.h>               // keypad library for interfacing keypad
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
int x=0;
int y=0;
int n=0;
int minValue=0;
int maxValue=0;
char keyPress=0;
int keyPressTime=100;
String msg="";

char hexaKeys[ROWS][COLS] = 
{
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

byte rowPins[ROWS] = {11, 10, 9, 8}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {7, 6, 5, 4}; //connect to the column pinouts of the keypad

Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 

String alpha="1!@_$%? ABC2 DEF3 GHI4 JKL5 MNO6 PQRS7 TUV8 WXYZ9 *  0# ";  

void setup() 
{
  Serial1.begin(9600);    // init serial1 for GSM
  lcd.begin(16,2);        // init LCD
  delay(2000);
  lcd.clear();
  lcd.print("Initializing GSM");
  delay(2000);

  /*Serial1.print("\r"); 
  delay(1000);*/
  Serial1.print("AT+CMGF=1\r");
  delay(1000);
  Serial1.print("AT+CMGS=\"+91**********\"\r");
  delay(1000);
  lcd.clear();
  lcd.print("Wallet Ready");
  delay(2000);
}

void loop()
{
  int n=0;
  lcd.clear();
  lcd.noCursor();
  while(1)
  {
    lcd.cursor();
    char key = customKeypad.getKey();
    if(key=='1')
      getkey(0, 7, key);

     if(key=='2')
      getkey(8, 12, key);

     else if(key=='3')
      getkey(13, 17, key); 

     else if(key=='4')
      getkey(18, 22, key);

    else if(key=='5')
      getkey(23, 27, key);

    else if(key=='6')
      getkey(28, 32, key);

    else if(key=='7')
      getkey(33, 38, key);

    else if(key=='8')
      getkey(39,43, key);

    else if(key=='9')
      getkey(44, 49, key);

    else if(key=='*')
      getkey(50, 51, key);

    else if(key=='0')
      getkey(52, 54, key);

    else if(key=='#')
      getkey(55, 56, key);

    else if(key == 'C')
    {
         x--;
         lcd.setCursor(x,y);
         lcd.print(" ");
         n--;
         msg[n]=' '; 
         lcd.setCursor(x,y);
    }

    else if(key == 'D')
    {
      lcd.clear();
      lcd.noBlink();
      lcd.print("Sending Data");
      lcd.setCursor(0,1);
      lcd.print("To Phone");

      /*Serial1.print("\r"); 
      delay(1000);*/
      Serial1.print("AT+CMGF=1\r");
      delay(1000);
      Serial1.print("AT+CMGS=\"+91**********\"\r");
      delay(1000);
      Serial1.println(msg);
      delay(1000);
      Serial1.write(0x1A);
      delay(1000);
      lcd.clear();
      lcd_status();
       //  clearmsg();
       n=0;
       i=0;
       x=0;
       y=0;
       msg="";
    }
  }
}

void getkey(int minValue, int maxValue, char keyPress)
{
   int ch=minValue;
   int pressed=1;
   char key=keyPress;
   lcd.noBlink();
   for(int i=0;i<keyPressTime;i++)
   {     
      if(key==keyPress)
      {
         lcd.setCursor(x,y);
         lcd.print(alpha[ch]);
         ch++;
         if(ch>maxValue)
         ch=minValue;
         i=0;   
      }
      key=customKeypad.getKey();
      delay(10);
    }

     if(pressed)
     {  
        x++;
        msg+=alpha[ch-1];
        n++;
        if(x>15)
        {
          x=0;
          y=1;
        }
     }
    pressed=0;
    lcd.blink();
}

void lcd_status()
{
  lcd.clear();
  lcd.print("Transaction Sent");
  lcd.setCursor(0,1);
  lcd.print("Successfully");
  delay(2000);
  lcd.clear();
}

但是当我尝试将这两个代码组合在一起时问题就出现了,这样我就可以通过GSM模块打印短信时获得正确的键盘输入和GPS位置。

0 个答案:

没有答案