首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试使用Arduino Nano使OLED显示来自超声波距离传感器的输入

尝试使用Arduino Nano使OLED显示来自超声波距离传感器的输入
EN

Stack Overflow用户
提问于 2019-10-16 11:38:20
回答 1查看 198关注 0票数 0

我很难从超声波测距仪获得输入,以便在Oled显示器上显示距离。我用的是Arduino Nano。我可以让显示器打印Hello World,同时可以在Arduino IDE串行监视器上查看来自测距仪的所有输入。我用的是1.3英寸的oled显示屏和3针超声波测距仪。它有vcc、地和信号引脚。我尝试了许多不同的组合,试图让它显示,但都不起作用。这是我目前所拥有的,至少可以让这两个设备同时工作。对于显示器和传感器,制造商提供了代码,使它们在Arduino Nano上独立工作。很抱歉对我的代码造成了所有的混淆。

代码语言:javascript
复制
#include <U8glib.h>
#include "Arduino.h"

U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0); // for 0.96” and 1.3”

class Ultrasonic
{
  public:
    Ultrasonic(int pin);
    void DistanceMeasure(void);
    long microsecondsToCentimeters(void);
    long microsecondsToInches(void);
  private:
    int _pin; //pin number of Arduino that is connected with SIG pin of Ultrasonic Ranger.
        long duration;  // the Pulse time received;
};
Ultrasonic::Ultrasonic(int pin)
{
  _pin = pin;
}
/*Begin the detection and get the pulse back signal*/
void Ultrasonic::DistanceMeasure(void)
{
  pinMode(_pin, OUTPUT);
  digitalWrite(_pin, LOW);
  delayMicroseconds(2);
  digitalWrite(_pin, HIGH);
  delayMicroseconds(5);
  digitalWrite(_pin,LOW);
  pinMode(_pin,INPUT);
  duration = pulseIn(_pin,HIGH);
}
/*The measured distance from the range 0 to 400 Centimeters*/
long Ultrasonic::microsecondsToCentimeters(void)
{
  return duration/29/2; 
}
/*The measured distance from the range 0 to 157 Inches*/
long Ultrasonic::microsecondsToInches(void)
{
  return duration/74/2; 
}

Ultrasonic ultrasonic(7);
void setup(void)
{
  Serial.begin(9600);

  if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
    u8g.setColorIndex(255);     // white
  }
  else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
    u8g.setColorIndex(3);         // max intensity
  }
  else if ( u8g.getMode() == U8G_MODE_BW ) {
    u8g.setColorIndex(1);         // pixel on
  }
  else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
    u8g.setHiColorByRGB(255,255,255);
  }
}
void loop(){
{
  long RangeInInches;
  long RangeInCentimeters;
  ultrasonic.DistanceMeasure(); // get the current signal time;
  RangeInInches = ultrasonic.microsecondsToInches(); //convert the time to inches;
  RangeInCentimeters = ultrasonic.microsecondsToCentimeters(); //convert the time to centimeters
  Serial.println("The distance to obstacles in front is: ");
  Serial.print(RangeInInches);//0~157 inches
  Serial.println(" inch");
  Serial.print(RangeInCentimeters);//0~400cm
  Serial.println(" cm");
  delay(100);
}
{
  // picture loop
  u8g.firstPage();  
  do {
    draw();
  } while( u8g.nextPage() );
  // rebuild the picture after some delay
  delay(50);
}
}
void draw(void) {
  u8g.setFont(u8g_font_unifont);
  u8g.setPrintPos(5, 20); 
  u8g.print("Hello World!");
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-16 16:13:30

我不能尝试这个,但我猜你必须将超声波传感器的范围转换为字符串,使用String(),然后你可以将其绘制在有机发光二极管显示器上。如果您在循环函数外部声明变量,则也可以在绘图函数中使用它们。

代码语言:javascript
复制
long RangeInInches;
long RangeInCentimeters;

void loop() { 
    ...
    RangeInCentimeters = ...
    Serial.print(RangeInCentimeters);
}

void draw() {
    ...
    u8g.print(String(RangeInCentimeters));
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58405407

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档