Arduino 16 * 2 LCD显示随机字符而不是预期文字

时间:2016-04-09 17:37:56

标签: arduino electronics

我正在尝试向我的16 * 2 LCD模块显示一些数据但是显示了一些随机字符。我有一些简单的代码,我用来测试我的液晶显示器,它完美地工作。代码:

#include<LiquidCrystal.h>


// initializing pins - RS, E, rest of data pins
LiquidCrystal lcd(1, 2, 4, 5, 6, 7);

void setup() {
  lcd.begin(16, 2);
}

void loop() {
  lcd.print("Testing");   // thats the top row string
  delay(1800);

  lcd.setCursor(2, 1);   // move to the 2nd row, 1st col
  lcd.print("Display this!");
  delay(1800);
  lcd.clear();

  lcd.setCursor(7, 1);
  delay(400);
  lcd.blink();
  lcd.setCursor(6, 1);
  delay(400);
  lcd.setCursor(5, 1);
  delay(400);
  lcd.setCursor(4, 1);
  delay(400);
  lcd.setCursor(3, 1);
  delay(400);
  lcd.setCursor(2, 1);
  delay(400);
  lcd.setCursor(1, 1);
  delay(400);
  lcd.setCursor(0, 1);
  lcd.noBlink();
  lcd.print("Silly Isn't It?");

  lcd.cursor();
  delay(1800);
  lcd.noCursor();

  lcd.clear();
}

然而,现在我在面包板上有更多的东西 - 液晶显示器,微型SD读卡器,电位器和LM35温度传感器 这是我的代码:

#include<LiquidCrystal.h>
#include <SD.h>
#include <SPI.h>

////////// LCD
//initializing pins - RS, E, rest of data pins
LiquidCrystal lcd(1, 2, 4, 5, 6, 7);


const int CS_PIN = 10;
const int POW_PIN = 8;
int refreshRate = 2000;       // for reading vals

////////// LEDs
int ledPinR = 11;
int ledPinG = 12;
int ledPinY = 13;

////////// LM35
float temp;
int tempPin = A0;

void setup() {

  ////////// LED
  pinMode(ledPinR, OUTPUT);
  pinMode(ledPinG, OUTPUT);
  pinMode(ledPinY, OUTPUT);

  ////////// LCD
  lcd.begin(16, 2);
  lcd.print("please wait...");   //thats the top row string
  delay(2000);
  lcd.clear();
  lcd.blink();


  ////////// SD
  Serial.begin(9600);
  Serial.println("\nNow Initializing SD card...");
  pinMode(CS_PIN, OUTPUT);
  pinMode(POW_PIN, OUTPUT);
  digitalWrite(POW_PIN, HIGH);

  if(!SD.begin(CS_PIN)){
    Serial.println("\nSomething went wrong. Probably card failure, card format, or something else.");
    return;
  }

  Serial.println("\nCard ready!");
  File commandFile =  SD.open("tempLevels.txt");

  if(commandFile){
    Serial.println("\nNow Reading Command File...");

    while(commandFile.available())
    {
      refreshRate = commandFile.parseInt();
    }

    Serial.print("\nTapiwa, the refresh rate is: ");
    Serial.print(refreshRate);
    Serial.print(" ms");

    commandFile.close();
  }

  else{
    Serial.println("Oops! Failing to read command file!");
    return;
  }
}

void loop() {

  ////////// LM35
  temp = analogRead(tempPin);
  float mV = (temp / 1024.0) * 5000;
  float tempVal = mV / 10;

  Serial.println("\nTemperature is: ");
  Serial.println(tempVal);  

  File dataFile =  SD.open("log.csv", FILE_WRITE);          // dont know about that .csv format

  if(dataFile)
  {
    dataFile.print("\nTemperature is: ");
    dataFile.print(tempVal);
    dataFile.println("Deg");
    dataFile.close();

    Serial.println("\nSaved in DataFile >> Temperature is: ");
    Serial.print(tempVal);
  }

  else
  {
    Serial.println("DataFile error! Reading not saved");
    Serial.println("Could not open log file! Not on SD card!");
  }

  lcd.print("Temp: ");
  lcd.setCursor(2, 1);   // 2nd row, 1st col
  lcd.print(tempVal);
  delay(2000);

  lcd.clear();
  delay(refreshRate);
}

我在串行监视器中获得结果,但LCD显示类似于加密文本的随机字符。我哪里做错了? 我看过这个网站和其他网站上的多个帖子,但它们并没有那么有用:

This one made sense but not useful in my case

This one too!

And this one

0 个答案:

没有答案