oled螢幕不卡方案

MKT-porter發表於2024-03-16

#include "U8glib.h"
#include "DHT.h"

U8GLIB_SSD1306_128X32 u8g(U8G_I2C_OPT_NONE);
#define DHTTYPE DHT22   // DHT 22  (AM2302)
#define DHTPIN 2     // what pin we're connected to
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  // put your setup code here, to run once:
  u8g.setFont(u8g_font_8x13B);
  u8g.setFontRefHeightText();
  u8g.setFontPosTop();
  Serial.begin(9600); 
  Serial.println("DHTxx test!");
  dht.begin();
}

void loop() {
  // put your main code here, to run repeatedly:
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  delay(2000);
  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(t) || isnan(h)) {
    Serial.println("Failed to read from DHT");
  } else {
    Serial.print("Humidity: "); 
    Serial.print(h);
    Serial.print(" %\t");
    Serial.print("Temperature: "); 
    Serial.print(t);
    Serial.println(" *C");
    u8g.firstPage();
    do{
      u8g.drawStr(0, 0, "Hum");
      u8g.setPrintPos(40, 0);
      u8g.print(h);u8g.print("%");
      u8g.drawStr(0, 20, "Temp");
      u8g.setPrintPos(40, 20);
      u8g.print(t);u8g.print("c");
    }while(u8g.nextPage());
  }
}

  

採集鐳射顯示

#include "U8glib.h"

U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);


#include <SoftwareSerial.h>
SoftwareSerial mySerial(4,5);//定義軟串列埠,3號埠為TX,2號埠為RX
char buff[4]={0x80,0x06,0x03,0x77};
char Oncebuff[4]={0x80,0x06,0x02,0x78};

unsigned char data[11]={0};
String distance_send="0";

int miao =0;
int fen=0;
int hour=0;

String TestDistance(){
 
  //String distance_send="0";
  while(1)
  {

    if(mySerial.available()>0)//判斷串列埠是否有資料可讀
    {
      
      delay(50);
      for(int i=0;i<11;i++)
      {
        data[i]=mySerial.read();
        
      }
      unsigned char Check=0;
      for(int i=0;i<10;i++)
      {
        Check=Check+data[i];
      }
      Check=~Check+1;
      if(data[10]==Check)
      {
        if(data[3]=='E'&&data[4]=='R'&&data[5]=='R')
        {
          //Serial.println("Out of range");
          distance_send="-1";
          return distance_send;
          break;
        }
        else
        {
          float distance=0;
          distance=(data[3]-0x30)*100+(data[4]-0x30)*10+(data[5]-0x30)*1+(data[7]-0x30)*0.1+(data[8]-0x30)*0.01+(data[9]-0x30)*0.001;
          distance=distance*1000; //換算成mm
          //Serial.print("Cur Distance = ");
         // Serial.print(distance,1);
         // Serial.println(" MM");
          distance_send=String(distance); // 測兩次 防止快取沒變
          return distance_send;
           
      
        }
      }
      else
      {
       // Serial.println("Invalid Data!");
        distance_send="-1";
        return distance_send;
        break;
      }
    }
    delay(30);
  }
 
 
   
  }
 
 
String ReadOneDis(){
   int count=0;
 
   while(1){


     mySerial.print(Oncebuff); // 測量單次
     
     
     if (count >=2){        
          //Serial.print("====  Real Distance === ");
          //Serial.print(distance_send);
         // Serial.println(" MM");
          return "ok";
        }
     else{
          //Serial.println(count);
       
          distance_send=TestDistance();
          if(distance_send!="-1")
          {
            count=count+1; // 統計成功次數
          }
      }
 
   }
             
 
         
   
  }

int btn_beign_stop=2;
int btn_reset=3;

int state_begin_stop=0;
int state_rest=0;





unsigned int timeAllcnt=0;// 記錄總時間

unsigned int timecnt=0;// 記錄總時間

void Interrupt_btn_beign_stop(){
    state_begin_stop=!state_begin_stop;
    if(state_begin_stop)
      {//Serial.println("當前狀態:開始");
      }
    else{
      // Serial.println("當前狀態:停止");
      }
    
}

void Interrupt_btn_reset(){
    state_rest=1;
    //Serial.println("復位按鍵");

    timecnt = 0;
    timeAllcnt=0;
    state_begin_stop=1;// 自動開始

    
}


bool state_old=0;
bool state_cur=0;
  
void setup()
{

  u8g.setFont(u8g_font_8x13B);
  u8g.setFontRefHeightText();
  u8g.setFontPosTop();
  
  Serial.begin(9600);
  mySerial.begin(9600);

  pinMode(btn_beign_stop, INPUT);
  pinMode(btn_reset, INPUT);
  attachInterrupt(digitalPinToInterrupt(btn_beign_stop), Interrupt_btn_beign_stop, FALLING);//FALLING  RISING
  attachInterrupt(digitalPinToInterrupt(btn_reset), Interrupt_btn_reset, FALLING);

  
 
  
}
 String msg1;
 String msg2;
 String msg3;
 String msg4;
 
void loop()
{

  
  timecnt=millis();

  
  ReadOneDis();
  
 
    u8g.firstPage();
    do{
      u8g.drawStr(0, 0, "work:");
      u8g.setPrintPos(72, 0);
      u8g.print((state_begin_stop));
      
      u8g.drawStr(0, 20, "distance:");
      u8g.setPrintPos(72, 20);
      u8g.print((distance_send));

      miao = int(timeAllcnt/1000);
      msg3="time:"+String(hour)+":"+String(fen)+":"+String(miao);
  
      u8g.drawStr(0, 40, "time:");
      u8g.setPrintPos(0, 40);
      u8g.print(msg3);
    }while(u8g.nextPage());

  float distance_light=distance_send.toFloat();
  if(distance_light<=90){
    state_cur=1;
    }
  else{
    state_cur=0;
    }
    


  if(state_rest==1){
    timecnt=0;
    state_rest=0;
    }
  else
  {
    timecnt = millis()-timecnt;
    }
  
  if(state_begin_stop && state_cur==1 ){
    timeAllcnt=timeAllcnt+timecnt;
    
    if(timeAllcnt>=60000){
      fen=fen+1;
      if(fen>=60){
          hour=hour+1;
        }
      timeAllcnt=timeAllcnt%1000;
      }
  }


}

  

相關文章