尝试使用Arduino Nano使OLED显示超声波范围传感器的输入
我很难从超声波测距仪获取输入,以便在Oled显示屏上显示距离。我用的是Arduino Nano。我可以让显示器打印Hello World,同时可以在Arduino IDE串行监视器上查看测距仪的所有输入。我使用的是1.3英寸oled显示屏和3针超声波测距仪。它有vcc、接地和信号引脚。我尝试了许多不同的组合,试图让它显示出来,但没有任何效果。以下是我目前所拥有的,至少可以让这两个设备同时工作。对于显示器和传感器,制造商提供了代码,使其能够在Arduino Nano上独立工作。很抱歉,我的代码有这么多混乱尝试使用Arduino Nano使OLED显示超声波范围传感器的输入,arduino,oledb,nano,arduino-ultra-sonic,Arduino,Oledb,Nano,Arduino Ultra Sonic,我很难从超声波测距仪获取输入,以便在Oled显示屏上显示距离。我用的是Arduino Nano。我可以让显示器打印Hello World,同时可以在Arduino IDE串行监视器上查看测距仪的所有输入。我使用的是1.3英寸oled显示屏和3针超声波测距仪。它有vcc、接地和信号引脚。我尝试了许多不同的组合,试图让它显示出来,但没有任何效果。以下是我目前所拥有的,至少可以让这两个设备同时工作。对于显示器和传感器,制造商提供了代码,使其能够在Arduino Nano上独立工作。很抱歉,我的代码有这
#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!");
}
#包括
#包括“Arduino.h”
U8GLIB_SSD1306_128X64 u8g(u8g_I2C_OPT_NONE | u8g_I2C_OPT_DEV_0);//适用于0.96英寸和1.3英寸
类超声波
{
公众:
超声波(int-pin);
空隙距离度量(void);
长微秒加速度计(空);
长微秒音程(无效);
私人:
int _pin;//与超声波测距仪SIG引脚连接的Arduino引脚号。
长持续时间;//接收到的脉冲时间;
};
超声波::超声波(内部引脚)
{
_引脚=引脚;
}
/*开始检测并获取脉冲返回信号*/
空隙超声::距离测量(空隙)
{
引脚模式(引脚,输出);
数字写入(_引脚,低电平);
延迟微秒(2);
数字写入(引脚,高电平);
延迟微秒(5);
数字写入(_引脚,低电平);
引脚模式(_引脚,输入);
持续时间=脉冲强度(_针,高);
}
/*从0到400厘米范围内测量的距离*/
长超声波::微秒计(空隙)
{
返回时间/29/2;
}
/*从0到157英寸范围内测量的距离*/
长超声波::微秒声(无效)
{
返回时间/74/2;
}
超声(7);
作废设置(作废)
{
Serial.begin(9600);
如果(u8g.getMode()==u8g\u MODE\u R3G3B2){
u8g.setColorIndex(255);//白色
}
else if(u8g.getMode()==u8g_MODE_GRAY2BIT){
u8g.setColorIndex(3);//最大强度
}
else if(u8g.getMode()==u8g\u MODE\u BW){
u8g.setColorIndex(1);//打开像素
}
else if(u8g.getMode()==u8g\u MODE\u HICOLOR){
u8g.setHiColorByRGB(255255);
}
}
void循环(){
{
长射程英寸;
远程激励计;
超声波.DistanceMeasure();//获取当前信号时间;
Rangeinches=超声波。微秒触控();//将时间转换为英寸;
RangeInCentimeters=超声波。微秒分光计();//将时间转换为厘米
Serial.println(“到前方障碍物的距离为:”);
串行打印(范围为英寸);//0~157英寸
串行打印长度(“英寸”);
串行打印(量程激励计);//0~400cm
序列号。打印号(“cm”);
延迟(100);
}
{
//图像循环
u8g.firstPage();
做{
draw();
}而(u8g.nextPage());
//延迟一段时间后重建图片
延迟(50);
}
}
作废取款(作废){
u8g.setFont(u8g\u font\u unifont);
u8g.设置打印位置(5,20);
u8g.打印(“你好,世界!”);
}
我无法尝试,但我猜您必须使用string()
将超声波传感器的范围转换为字符串,然后您可以在OLED显示屏上绘制。如果在循环函数外声明变量,也可以在绘图函数中使用它们
long RangeInInches;
long RangeInCentimeters;
void loop() {
...
RangeInCentimeters = ...
Serial.print(RangeInCentimeters);
}
void draw() {
...
u8g.print(String(RangeInCentimeters));
}
您的实际问题是什么?当我试图仅使用RangeInCentimeters更改void draw()中显示的打印内容时,总是会出现以下错误:“RangeInCentimeters”未在此范围内声明,因此如果我从传感器(RangeInCentimeters)输入字符串,它将允许将其导入显示?我一定会试试的。当我尝试使用RangeInCentimeters更改void draw()中显示的打印内容时,我总是会遇到这样的错误:“RangeInCentimeters”未在本文档中声明scope@BoatingPuppy您在循环()中声明了RangeInCentimeters,因此无法从draw()访问它。遵循Kokodoko的建议或将距离作为参数交给draw()。要将范围打印到显示器上,必须先将长文本转换为文本。看这个答案。我按照建议改变了它。它解决了问题,我知道我做错了什么。只要它在循环()之外声明,就可以将其引入draw()。