ESP8266超声波车位检测+OLED显示汉字【留存自用】

本文介绍了如何使用ESP8266NodeMCU配合超声波传感器和0.96英寸OLED显示屏,实时监测车位占用情况,并通过LCD显示车位剩余数量,同时具备报警功能。
摘要由CSDN通过智能技术生成

准备ESP8266、超声波传感器、0.96OLED显示屏

 

取汉字字模

 

 

/*
 * ESP8266-NodeMCU通过驱动oled显示文文字和图片
 * 需要使用Arduino-OLED第三方库:https://github.com/ThingPulse/esp8266-oled-ssd1306/tree/4.2.0
 */
#include "SSD1306Wire.h"

/* 设置oled屏幕的相关信息 */
const int I2C_ADDR = 0x3c;              // oled屏幕的I2c地址
#define SDA_PIN 4                       // SDA引脚,默认gpio4(D2)
#define SCL_PIN 5                       // SCL引脚,默认gpio5(D1)

/* 新建一个oled屏幕对象,需要输入IIC地址,SDA和SCL引脚号 */
SSD1306Wire oled(I2C_ADDR, SDA_PIN, SCL_PIN);

static const uint8_t text[][60] = {
{0x00,0x08,0x08,0x08,0x88,0x09,0x48,0x09,0x28,0x09,0x18,0x09,0x0F,0x09,0xE8,0xFF,0x08,0x09,0x08,0x09,0x08,0x09,0x08,0x09,0x08,0x09,0x08,0x08,0x00,0x08,0x00,0x00},/*"车",0*/
{0x00,0x01,0x80,0x00,0x60,0x00,0xF8,0xFF,0x07,0x40,0x10,0x40,0x90,0x41,0x10,0x5E,0x11,0x40,0x16,0x40,0x10,0x70,0x10,0x4E,0xD0,0x41,0x10,0x40,0x00,0x40,0x00,0x00},/*"位",1*/
{0x80,0x00,0x80,0x40,0x40,0x22,0x20,0x1A,0x50,0x02,0x48,0x42,0x44,0x82,0xC3,0x7F,0x44,0x02,0x48,0x02,0x50,0x02,0x20,0x0A,0x40,0x12,0x80,0x60,0x80,0x00,0x00,0x00},/*"余",2*/
{0x20,0x00,0x20,0x80,0x20,0x80,0xBE,0xAF,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xFF,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xBE,0xAF,0x20,0x80,0x20,0x80,0x20,0x00,0x00,0x00},/*"量",3*/
{0x00,0x00,0x00,0x00,0x00,0x36,0x00,0x36,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*":",0*/
};

// 设定SR04连接的数字引脚
const int trigPin1 = 2; //设置接受引脚
const int echoPin1 = 13; //设置发射引脚

const int trigPin2 = 14; //设置接受引脚
const int echoPin2 = 12; //设置发射引脚

float sound_spd=343;//声速初始值
float distance1,distance2; //距离

//设置灯光引脚
//int ledPin = 2;

void setup() 
{   
  // 初始化串口通信及连接SR04的引脚
  Serial.begin(115200); //设置波特率
  pinMode(trigPin1, OUTPUT); 
  pinMode(trigPin2, OUTPUT);
  // 要检测引脚上输入的脉冲宽度,需要先设置为输入状态
  pinMode(echoPin1, INPUT);
  pinMode(echoPin2, INPUT); 
  //Serial.println("Ultrasonic sensor:");
  //设置灯光引脚
  //pinMode(ledPin, OUTPUT);
  //digitalWrite(ledPin, HIGH);
  
  //oled屏幕初始化
  oled.init();
  oled.flipScreenVertically();        // 设置屏幕翻转
  oled.setContrast(255);              // 设置屏幕亮度
  drawRect();                         // 测试屏幕显示
  oled.clear(); 
  oled.display();                     // 清除屏幕
} 

void loop() 
{ 
  //车位1
  //产生一个10us的高脉冲去触发TrigPin 
  digitalWrite(trigPin1, LOW); 
  delayMicroseconds(2); 
  //产生高脉冲前线产生2us低脉冲,确保高脉冲的纯净
  digitalWrite(trigPin1, HIGH); 
  delayMicroseconds(10);
  digitalWrite(trigPin1, LOW); 
  // 检测脉冲宽度,并计算出距离
  distance1 = pulseIn(echoPin1, HIGH)/ 58.00;
  Serial.print("车位1:");
  Serial.println(distance1);
  
  //车位2
  //产生一个10us的高脉冲去触发TrigPin 
  digitalWrite(trigPin2, LOW); 
  delayMicroseconds(2); 
  //产生高脉冲前线产生2us低脉冲,确保高脉冲的纯净
  digitalWrite(trigPin2, HIGH); 
  delayMicroseconds(10);
  digitalWrite(trigPin2, LOW); 
  // 检测脉冲宽度,并计算出距离
  distance2 = pulseIn(echoPin2, HIGH)/ 58.00;
  Serial.print("车位2:");
  Serial.println(distance2);
  
  int i = 2;
  if(distance1<10 && distance2<10 ){
    i=i-2;
    oled.clear();
    Serial.print("车位:");
    Serial.print(i);
    Serial.print("个"); 
    Serial.println(); 
    delay(200);  
  }
  else if((distance2<10 && distance1>10)||(distance2>10 && distance1<10)){
  //此时为警报距离,触发声光报警装置
      //digitalWrite(ledPin, HIGH);
      //delay(20); 
      i=i-1;
      oled.clear();   
  //此时为警报距离,输出报警距离
      Serial.print("车位:");
      Serial.print(i);
      Serial.print("个"); 
      Serial.println(); 
      delay(200);   
  }
  else{ 
     oled.clear();
      Serial.print("车位:");
      Serial.print(i);
      Serial.print("个"); 
      Serial.println(); 
      delay(200);   
  }
  oled.setFont(ArialMT_Plain_24);     // 设置字体
  
  /*显示汉字*/
  oled.drawFastImage(20 + 16*0, 35, 16, 16, text[0]);
  oled.drawFastImage(20 + 16*1, 35, 16, 16, text[1]);
  oled.drawFastImage(20 + 16*2, 35, 16, 16, text[2]);
  oled.drawFastImage(20 + 16*3, 35, 16, 16, text[3]);
  oled.drawFastImage(20 + 16*4, 35, 16, 16, text[4]);
  oled.setFont(ArialMT_Plain_16);     // 设置字体
  oled.drawString(25,5,"---garage---");
  oled.setFont(ArialMT_Plain_16);     // 设置字体
  oled.drawString(96,35,String(i));     // 将要显示的文字写入缓存
  oled.display();
    
}

void drawRect(void) {
  for (int16_t i=0; i<oled.getHeight()/2; i+=2) {
    oled.drawRect(i, i, oled.getWidth()-2*i, oled.getHeight()-2*i);
    oled.display();
    delay(50);
  }
}

效果图:

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值