Qiaolin Su
Published

Seeed Wio Terminal Smart Encouragement Display

A beginner-friendly DIY project that uses Seeed Wio Terminal and Grove Ultrasonic Distance Sensor to display an encouraging message!

BeginnerFull instructions provided6 hours84
Seeed Wio Terminal Smart Encouragement Display

Things used in this project

Hardware components

Wio Terminal
Seeed Studio Wio Terminal
×1
Seeed Studio Grove - Ultrasonic Distance Sensor
×1
Seeed Studio Grove - Universal 4 Pin Buckled 20cm Cable
×1
Western Digital High-Endurance Automotive SD Card
Western Digital High-Endurance Automotive SD Card
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Wio Terminal Pinout

Code

Wio Terminal Smart Encouragement Display

Arduino
This sketch uses a Grove Ultrasonic Distance Sensor and the Wio Terminal to detect when someone is nearby.
When a person is detected within 50 cm, the Wio Terminal reads an encouraging phrase from an SD card and displays it on the screen in two lines using a custom font.
#include <SPI.h>
#include <Seeed_FS.h>
#include "SD/Seeed_SD.h"
#include <TFT_eSPI.h>
#include "Free_Fonts.h"    // 请确保此文件已复制到与你的 .ino 文件相同的目录
#include "Ultrasonic.h"

// 创建 TFT_eSPI 对象(Wio Terminal 分辨率一般为 320x240)
TFT_eSPI tft = TFT_eSPI();

// 使用之前验证成功的超声波传感器方式(使用通道 0)
Ultrasonic ultrasonic(0);

bool textDisplayed = false;

// 从 SD 卡中读取文件内容(参数 filename 为 SD 卡上实际文件名,此处为 "text001.txt")
String getTextFromFile(const char *filename) {
  File file = SD.open(filename, FILE_READ);
  String content = "";
  if (file) {
    while (file.available()) {
      content += char(file.read());
    }
    file.close();
  } else {
    content = "File open error";
  }
  return content;
}

void setup() {
  Serial.begin(115200);
  while (!Serial) { ; }  // 等待串口初始化

  // 初始化 TFT 屏幕
  tft.begin();
  tft.setRotation(3);
  tft.fillScreen(TFT_BLACK);
  tft.setTextColor(TFT_WHITE, TFT_BLACK);

  // 初始化 SD 卡
  Serial.println("Initializing SD card...");
  if (!SD.begin(SDCARD_SS_PIN, SDCARD_SPI)) {
    Serial.println("SD card initialization failed!");
    while (1);
  }
  Serial.println("SD card initialized successfully.");
}

void loop() {
  // 读取超声波测距(单位:英寸),再转换为厘米
  long distanceInches = ultrasonic.MeasureInInches();
  float distanceCentimeters = distanceInches * 2.54;
  
  Serial.print("Distance: ");
  Serial.print(distanceInches);
  Serial.print(" inch, ");
  Serial.print(distanceCentimeters);
  Serial.println(" cm");

  // 当距离小于50cm时,显示鼓励文本,并手动进行换行显示
  if (distanceCentimeters > 0 && distanceCentimeters < 50) {
    if (!textDisplayed) {
      // 从 SD 卡读取文本内容(预期内容 "You are the best in the world!")
      String phrase = getTextFromFile("text001.txt");
      Serial.print("Read text: ");
      Serial.println(phrase);

      // 根据文本内容手动拆分为两行(你可以根据实际内容调整分行位置)
      String line1 = "You are the best";
      String line2 = "in the world!";

      // 设置较小的字体,例:12pt 斜体(如果你觉得12pt仍旧过大,也可以继续尝试更小磅值的字体)
      tft.setFreeFont(&FreeSansBoldOblique12pt7b);  // 请确认 Free_Fonts.h 中存在此字体定义

      // 清屏后分别绘制两行文字,这里的坐标可根据屏幕尺寸及效果调整
      tft.fillScreen(TFT_BLACK);
      tft.drawString(line1, 10, 100);
      tft.drawString(line2, 10, 140);
      
      textDisplayed = true;
    }
  } else {
    if (textDisplayed) {
      // 当目标远离,清空屏幕以便下次重新显示
      tft.fillScreen(TFT_BLACK);
      textDisplayed = false;
    }
  }
  
  delay(250);  // 每250ms更新一次数据
}

Credits

Qiaolin Su
1 project • 0 followers
Contact

Comments

Please log in or sign up to comment.