我正在使用DS3231定时器电路和带有arduino uno的I2C lcd和RELAY屏蔽。我的目标是根据预先设定的时间打开继电器并在LCD中显示当前时间。 该程序工作正常,除了我使用了一个名为WATCHMAN的子程序。该子程序的目的是运行手表并将其显示在LCD中。问题是我每次插上它然后插上整个硬件后......液晶显示器显示奇怪的字符而不是CLOCK。拧紧液晶显示器有什么问题?
代码如下 -
#include <SoftwareSerial.h>
SoftwareSerial SIM900A(9, 10);
#include <Wire.h>
#define ADDRESS 0x68
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
#define BACKLIGHT_PIN 13
void setup() {
Serial.begin(9600);
Wire.begin();
SIM900A.begin(9600);
delay(15);
lcd.begin(16,2);
delay(15);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
pinMode ( BACKLIGHT_PIN, OUTPUT );
digitalWrite ( BACKLIGHT_PIN,HIGH);
}
void loop(){
delay(500);
Watchman();
int yrchk=Year();
int daychk=Date();
int mthchk=Month();
String mth=MonthString ();
int hrchk= (Hour())*100;
int minchk= Min();
int schk=(Sec())/100;
int convtime=hrchk+minchk+schk;
Serial.println(convtime);
if (yrchk>=2018 &&(mth=="January"|| mth=="February"|| mth=="March")){
if (convtime>=1810 && convtime<=2359){
Serial.println("First stage-1 loop is running");
digitalWrite(11,HIGH);
digitalWrite(12,HIGH);} // Turning on the relay pin no 11 & 12
else if ((convtime>=100 && convtime<=540)){digitalWrite(11,HIGH);digitalWrite(12,HIGH); // Turning on the relay pin no 11 & 12
Serial.println("First stage-2 loop is running");}
}
else if (yrchk>=2018 &&(mth=="April"|| mth=="May"|| mth=="June"|| mth=="July"|| mth=="August")){
if (convtime>=1830 && convtime<=2359){
Serial.println("second stage-1 loop is running");
digitalWrite(11,HIGH);digitalWrite(12,HIGH);} // Turning on the relay pin no 11 & 12
else if ((convtime>=100 && convtime<=520)){digitalWrite(11,HIGH);digitalWrite(12,HIGH);
Serial.println("second stage-2 loop is running");}
}
else if (yrchk>=2018 &&(mth=="September"|| mth=="October")){
if (convtime>=1750 && convtime<=2359){
Serial.println("Third stage-1 loop is running");
digitalWrite(11,HIGH); // Turning on the relay pin no 11 & 12
digitalWrite(12,HIGH);}
else if ((convtime>=100 && convtime<=540)){digitalWrite(11,HIGH);digitalWrite(12,HIGH);
Serial.println("Third stage-2 loop is running");}
}
else if (yrchk>=2018 &&(mth=="November"|| mth=="December")){
if (convtime>=1735 && convtime<=2359){
Serial.println("Fourth stage-1 loop is running");
digitalWrite(11,HIGH);digitalWrite(12,HIGH);} // Turning on the relay pin no 11 & 12
else if ((convtime>=100 && convtime<=600)){digitalWrite(11,HIGH);digitalWrite(12,HIGH);
Serial.println("Fourth stage-2 loop is running");}
}
else if (yrchk<2018){
//SIM900A.println("AT+CMGF=1");
//delay(1000);
//SIM900A.println("AT+CMGS=\"+8801714206279\"\r"); // Replace x with mobile number
//delay(1000);
//SIM900A.println("Out of date error in DS3231,send instruction");// The SMS text you want to send
//delay(100);
//SIM900A.println((char)26);// ASCII code of CTRL+Z
//delay(1000);
digitalWrite(11,LOW);
digitalWrite(12,LOW);
}
}
//void eepromWrite(int address, int location,int data)
//{
//Wire.beginTransmission(address);
//Wire.write(location);
//Wire.write(data);
//Wire.endTransmission();
//}
//byte eepromRead(byte location)
//{
//Wire.beginTransmission(ADDRESS);
//Wire.write(location);
//Wire.endTransmission();
//Wire.requestFrom(ADDRESS,25);
//while(!Wire.available()){}
//return Wire.read();
//}
String MonthString ()
{
Wire.beginTransmission(ADDRESS);
Wire.write(0x05);
Wire.endTransmission();
Wire.requestFrom(ADDRESS,1);
while(!Wire.available()){}
int i= Wire.read();
String k;
if (i==1){ k="January";};
if (i==2) {k="February";};
if (i==3) {k="March";};
if (i==4) {k="April";};
if (i==5) {k="May";};
if (i==6) {k="June";};
if (i==7) {k="July";};
if (i==8) {k="August";};
if (i==9) {k="September";};
if (i==10){k="October";};
if (i==11){k="November";};
if (i==12){k="December";};
return k;
}
int Month()
{
Wire.beginTransmission(ADDRESS);
Wire.write(0x05);
Wire.endTransmission();
Wire.requestFrom(ADDRESS,1);
while(!Wire.available()){}
int i= Wire.read();
return i;
}
byte Min()
{
Wire.beginTransmission(ADDRESS);
Wire.write(0x01);
Wire.endTransmission();
Wire.requestFrom(ADDRESS,2);
while(!Wire.available()){}
byte bcd_value = Wire.read(); // lets say bcd_value = 55, or 0x37
byte tens = bcd_value >> 4; // tens = 3 (by shifting down the number four places)
byte units = bcd_value & 0x0F; // units = 7 (the 0x0F filters-out the high digit)
byte final_value = (tens * 10) + units;
return final_value;
}
byte Hour()
{
Wire.beginTransmission(ADDRESS);
Wire.write(0x02);
Wire.endTransmission();
Wire.requestFrom(ADDRESS,1);
while(!Wire.available()){}
byte bcd_value = Wire.read(); // lets say bcd_value = 55, or 0x37
byte tens = bcd_value >> 4; // tens = 3 (by shifting down the number four places)
byte units = bcd_value & 0x0F; // units = 7 (the 0x0F filters-out the high digit)
byte final_value = (tens * 10) + units;
return final_value;
}
byte Date()
{
Wire.beginTransmission(ADDRESS);
Wire.write(0x04);
Wire.endTransmission();
Wire.requestFrom(ADDRESS,1);
while(!Wire.available()){}
byte bcd_value = Wire.read(); // lets say bcd_value = 55, or 0x37
byte tens = bcd_value >> 4; // tens = 3 (by shifting down the number four places)
byte units = bcd_value & 0x0F; // units = 7 (the 0x0F filters-out the high digit)
byte final_value = (tens * 10) + units;
return final_value;
}
int Year()
{
Wire.beginTransmission(ADDRESS);
Wire.write(0x06);
Wire.endTransmission();
Wire.requestFrom(ADDRESS,1);
while(!Wire.available()){}
byte bcd_value = Wire.read(); // lets say bcd_value = 55, or 0x37
byte tens = bcd_value >> 4; // tens = 3 (by shifting down the number four places)
byte units = bcd_value & 0x0F; // units = 7 (the 0x0F filters-out the high digit)
byte final_value =(tens * 10) + units;
int k= final_value+2000;
return k;
}
String DayString ()
{
Wire.beginTransmission(ADDRESS);
Wire.write(0x03);
Wire.endTransmission();
Wire.requestFrom(ADDRESS,1);
while(!Wire.available()){}
int i= Wire.read();
String k;
if (i==1){ k="Monday";};
if (i==2) {k="Tuesday";};
if (i==3) {k="Wednesday";};
if (i==4) {k="Thursday";};
if (i==5) {k="Friday";};
if (i==6) {k="Saturday";};
if (i==7) {k="Sunday";};
return k;
}
byte Sec()
{
Wire.beginTransmission(ADDRESS);
Wire.write(0x00);
Wire.endTransmission();
Wire.requestFrom(ADDRESS,2);
while(!Wire.available()){}
byte bcd_value = Wire.read(); // lets say bcd_value = 55, or 0x37
byte tens = bcd_value >> 4; // tens = 3 (by shifting down the number four places)
byte units = bcd_value & 0x0F; // units = 7 (the 0x0F filters-out the high digit)
byte final_value = (tens * 10) + units;
return final_value;
}
byte Temp()
{
Wire.beginTransmission(ADDRESS);
Wire.write(0x11);
Wire.endTransmission();
Wire.requestFrom(ADDRESS,1);
while(!Wire.available()){}
byte bcd_value = Wire.read(); // lets say bcd_value = 55, or 0x37
byte tens = bcd_value >> 4; // tens = 3 (by shifting down the number four places)
byte units = bcd_value & 0x0F; // units = 7 (the 0x0F filters-out the high digit)
byte final_value = (tens * 10) + units;
return final_value;
}
void Watchman()
{
byte hr=Hour();
byte mn=Min();
byte sc=Sec();
byte tm= Temp();
byte dt=Date();
byte mt=Month();
int yr=Year();
String o;
if(hr>12){o=" PM";}
else{o=" AM";}
if (hr>12){hr=Hour()-12;}
lcd.clear();
lcd.setCursor(4,0);
lcd.print(dt);
lcd.print("/");
lcd.print(mt);
lcd.print("/");
lcd.print(yr);
//Serial.println(yr);
lcd.setCursor(4,1);
lcd.print(hr);
lcd.print(".");
lcd.print(mn);
lcd.print(".");
lcd.print(sc);
lcd.print(o);
//lcd.print(",T=");
//lcd.print(tm);
delay(500);
}
//}
void Serialman()
{
byte hr=Hour();
byte mn=Min();
byte sc=Sec();
byte tm= Temp();
byte dt=Date();
byte mt=Month();
int yr=Year();
String o;
if(hr>12){o=" PM";}
else{o=" AM";}
if (hr>12){hr=Hour()-12;}
Serial.print(dt);
Serial.print("/");
Serial.print(mt);
Serial.print("/");
Serial.println(yr);
Serial.print(hr);
Serial.print(".");
Serial.print(mn);
Serial.print(".");
Serial.print(sc);
Serial.print(o);
Serial.print(",T=");
Serial.print(tm);
Serial.println("");}