淘宝上有卖。。。
#include <Arduino.h>
#include <Wire.h>
#include <MultiLCD.h>
LCD_SSD1306 lcd;
unsigned char hexdata[9] = {0x7E, 0x00, 0x08, 0x01, 0x00, 0x02, 0x01, 0xAB, 0xCD};
int debug_sensor = A3;
String incomingStr = "";
void setup() {
Serial.begin(9600);
pinMode(debug_sensor, INPUT);
lcd.begin();
lcd.clear();
}
void loop() {
if(!digitalRead(A3))
SendCmd();
}
void serialEvent(){
while(Serial.available())
Serial.read();
}
void ReadHeader(){
do{
LcdDisplay("waiting...");
}while(Serial.read() != 0x31);
}
void ReadData(unsigned long * _t){
while(true){
while(Serial.available()){
delay(10);
incomingStr += (char)Serial.read();
}
LcdDisplay(incomingStr);
if(incomingStr.length() > 0)
break;
if(millis() - *_t > 5000)
break;
}
}
void SendCmd(){
incomingStr = "";
unsigned long wait_t = millis();
Serial.write(hexdata, 9);
ReadHeader();
ReadData(&wait_t);
}
void LcdDisplay(String data){
lcd.clear();
lcd.setCursor(0, 1);
lcd.print(data);
}
|