You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Arduino Uno与LCD、键盘、GSM SIM900A代码合并问题求助

Hey there! Let's figure out why your merged code isn't behaving as expected—since each piece works fine on its own, the issue is almost certainly from conflicting resources, timing gaps, or messy logic when combining them. Here are the most likely fixes and checks to run through:

1. Pin & Resource Conflicts First

First up, double-check that your SoftwareSerial (GSM) and LiquidCrystal pins aren't overlapping or competing for hardware resources:

  • Your code shows GSM using pins 2 (RX) & 3 (TX), and LCD using 14-19—on paper that's fine, but verify your physical wiring matches exactly. It's easy to accidentally cross a GSM wire into an LCD pin when rewiring.
  • Remember that SoftwareSerial on Arduino has a hard limit: it can only listen to one serial port at a time. If your LCD code uses interrupts or blocks the main loop for too long, it might starve the GSM serial buffer and cause missed AT commands or responses.

2. Fix Timing & Buffer Overflows

When juggling keyboard input, LCD updates, and GSM communication, timing issues are super common:

  • Stop refreshing the LCD constantly. Only update it when the user's input changes (e.g., when a new character is typed) instead of looping the update every cycle—this frees up time for GSM communication.
  • Add small delays after sending AT commands to the GSM module. For example, after sending AT+CMGF=1, add delay(100) to give the module time to process and respond. You can also use gsmSerial.available() to wait for a response before moving to the next command, instead of hardcoding delays.

3. Clean Up Code Structure & Variable Scope

Merging code often leads to accidental variable overlaps or broken logic flow:

  • Check if you have duplicate variable definitions (like two smsContent strings) that overwrite each other. Make sure variables storing user input are declared at the top of your sketch so both LCD and GSM code can access them.
  • Don't just paste two loop() functions together. Instead, split your logic into separate functions: one for handling keyboard input/LCD updates, and another dedicated to sending SMS. Only trigger the SMS send function when the user finishes input (e.g., presses a "send" key).

Example Merged Code Framework

Here's a cleaned-up structure that keeps input and SMS logic separated to avoid conflicts:

#include <SoftwareSerial.h>
#include <LiquidCrystal.h>

// GSM Serial (RX=D2, TX=D3)
SoftwareSerial gsmSerial(2, 3);
// LCD pins (adjust if your wiring differs)
LiquidCrystal lcd(14, 15, 16, 17, 18, 19);

String smsInput = "";
bool inputFinished = false;

void setup() {
  Serial.begin(9600); // For debugging
  gsmSerial.begin(9600);
  lcd.begin(16, 2);
  lcd.print("Enter SMS:");
}

void loop() {
  // Handle keyboard input only if we're not sending SMS
  if (!inputFinished) {
    char newChar = readKeyboard(); // Replace with your actual keyboard read logic
    if (newChar != '\0') {
      if (newChar == '#') { // Use # as "send" trigger
        inputFinished = true;
        lcd.clear();
        lcd.print("Sending...");
        sendSMS(smsInput);
        // Reset for next input
        inputFinished = false;
        smsInput = "";
        lcd.clear();
        lcd.print("Enter SMS:");
      } else {
        smsInput += newChar;
        lcd.setCursor(0, 1);
        lcd.print(smsInput);
      }
    }
  }
}

// Dedicated SMS send function
void sendSMS(String message) {
  // Set GSM to text mode
  gsmSerial.println("AT+CMGF=1");
  delay(100);
  // Target phone number (replace with your recipient)
  gsmSerial.println("AT+CMGS=\"+1234567890\"");
  delay(100);
  // Send the message content
  gsmSerial.print(message);
  delay(100);
  // Send Ctrl+Z to finalize SMS
  gsmSerial.write(26);
  delay(2000);
  
  // Print GSM response to Serial Monitor for debugging
  while (gsmSerial.available()) {
    Serial.write(gsmSerial.read());
  }
}

// Example keyboard read function (customize for your hardware)
char readKeyboard() {
  // Add your actual keyboard logic here (e.g., matrix keypad, individual buttons)
  // Return '\0' if no key is pressed, return the character if a key is pressed
  return '\0';
}

Quick Debugging Tips

  • Use the Arduino Serial Monitor to print debug info: log every character the keyboard reads, and every response the GSM module sends. This will show you exactly where the flow breaks.
  • Test each module separately again after merging—confirm the LCD still updates correctly, then confirm the GSM can still send a hardcoded SMS before combining the two.
  • Check your GSM module's power supply: combining two modules might draw more current than your power source can handle, causing the GSM module to reset mid-operation.

内容的提问来源于stack exchange,提问作者Nishant Agarwal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:25:22