首页 \ 问答 \ 为什么我的Arduino不能超过10?(Why can't my Arduino compare above 10?)

为什么我的Arduino不能超过10?(Why can't my Arduino compare above 10?)

我正在进行Arduino项目,在那里我不断测量气压,并使用Adafruit_BMP085库计算第一次测量的相对高度。

我显示当前值和峰值。 它基本上可以工作,但只要当前高度超过10米就停止更新峰值。

草图看起来像这样:

#include <Adafruit_BMP085.h>

String currentAltitude;
String peakAltitude;
int32_t groundpressure;

Adafruit_BMP085 bmp;

void setup() {
    groundpressure = bmp.readPressure();
}

void loop() {
    currentAltitude = bmp.readAltitude(groundpressure);
    if (currentAltitude > peakAltitude) { 
        peakAltitude = currentAltitude;
    }
    Serial.println("Current: " + currentAltitude + "m");
    Serial.println("Peak: " + peakAltitude + "m");
    delay(10);
}

我得到的(当我提高传感器/或增加气压时)是这样的:

Current: 0m
Peak: 0m

Current: 4m
Peak: 4m

Current: 11m
Peak: 4m (still)

为什么它停止比较。 是因为我正在比较的变量类型?


I'm on an Arduino project where I constantly measure the the air pressure and calculate a relative height from the first measurement using the Adafruit_BMP085 library.

I display the current value and a peak value. It basically works, but as soon as the current height exceeds 10m it stops updating the peak value.

Sketch looks something like this:

#include <Adafruit_BMP085.h>

String currentAltitude;
String peakAltitude;
int32_t groundpressure;

Adafruit_BMP085 bmp;

void setup() {
    groundpressure = bmp.readPressure();
}

void loop() {
    currentAltitude = bmp.readAltitude(groundpressure);
    if (currentAltitude > peakAltitude) { 
        peakAltitude = currentAltitude;
    }
    Serial.println("Current: " + currentAltitude + "m");
    Serial.println("Peak: " + peakAltitude + "m");
    delay(10);
}

What I get (when I raise the sensor/ or increase air pressure) is something like this:

Current: 0m
Peak: 0m

Current: 4m
Peak: 4m

Current: 11m
Peak: 4m (still)

Why does it stop comparing. Is it due to the type of variable I'm comparing?

更新时间:2023-02-04 12:02

最满意答案

是的,这是因为你正在使用重载的>运算符到一个String类型,它可能执行词法,而不是数字,比较。

在执行算术运算之前将字符串数据转换为数字。


Yes it is due to the fact that you are using the overloaded > operator to a String type which is probably performing a lexographic, not a numerical, comparison.

Convert your string data to numbers before performing arithmetic operations.

相关问答

更多
  • 是的,这是因为你正在使用重载的>运算符到一个String类型,它可能执行词法,而不是数字,比较。 在执行算术运算之前将字符串数据转换为数字。 Yes it is due to the fact that you are using the overloaded > operator to a String type which is probably performing a lexographic, not a numerical, comparison. Convert your string data ...
  • 通常不建议从输出端为您的电机供电。 Arduino上的数字引脚只能产生少量电流。 通常驱动电机的数量太少。 但是,这当然取决于电机。 比电机电压更重要的是知道需要多少电流来驱动它。 对于您的电阻问题,请使用电阻查看分压器。 麦克风 Powering your motor right from the output is typically not advised. The digital pins on Arduino can only souce a small amount of current. Ty ...
  • 常见的错误和编译器可以帮助你 改变这一行: uint32_t NFC1 = 3795120787; 到这一行: const uint32_t NFC1 = 3795120787; 您现在将得到一个编译器错误,这将导致您前进:o。 这行需要== ,而不是a = 。 不是这个: if(NFC1 = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A)){ / doh! 这个: if(NFC1 == nfc.readPassiveTargetID(PN532_M ...
  • 我在初始化时看到一个问题,在这一行: POT = Arduino.analogRead(0) 然后,在这里,你在这里使用POT作为引脚号,但POT将在上面的行中初始化为变量: pot = Arduino.analogRead(POT) 我认为这就是你出乎意料的行为的原因。 我认为如果你将POT的初始化改为POT=0或你的电位器连接的引脚号(如果它不是引脚0),它可能会起作用。 I figured it out. The blink issue was not expected. However it w ...
  • 看起来你的'else'语句属于错误的'if'块。 根据你的目标,你应该拥有它,这样如果没有按下一个键,你就会把Arduino.LOW写到9-12针。 基本上,只需移动一个支架: void draw() { if (keyPressed == true) { if (key == 'w' || key == 'W') { arduino.digitalWrite (12, Arduino.HIGH); } if (key == 's' || key == 'S') { ...
  • 如果您使用LiquidCrystal库,您可以在哪个基础上打印您的号码 a = 15; lcd.print(a,HEX); //print E 顺便说一下,它也可以应用于Serial对象 b = 14; Serial.println(b,HEX) //prints D 循环 for( int i = 0 ; i < 100 ; i++) lcd.print(i,HEX); 这将打印: 0 1 2 3 .... 9 A B C D E F 10 11 ... 1E 1F 20 21 22 .... ...
  • 在Arduino上,UNO引脚0和1配置为串行通信,如果使用Serial库。 尝试将这些引脚更改为其他一些数字引脚。 作为纯粹的推测,尝试从具有digitalRead()的引脚读取或处于此状态将导致LOW ,因此Keypad库将永远不会认为列活动,因此驻留在这些列上的按钮将永远不会工作。 On an Arduino UNO pins 0 & 1 are configured for serial communication, if you use the Serial library. Try changi ...
  • 在Arduino论坛上有一个类似的问题,但我不知道你的问题是否完全相同: [FIXED] [CapacitiveSensor]无法使用Due编译(端口操作) 它看起来并不像Arduino人非常敏感。 用户修改了寄存器名称,最终将自己的库放在那里。 *“...将uint8_t寄存器更改为RwReg(编译器没有抱怨),我用简单的pinMode(PIN,MODE)调用替换了portModeRegister(PIN)的使用(它的速度较慢,但它可以工作)。”* There is a similar question ...
  • 您没有添加低条件,因此当引脚变高时,它会保持高电平。 此外,我已经检查过,我认为你的接线不正确。 我在下面发布完整的代码和相应的连接。 #include #include int a = 2; int b = 3; int c = 4; int d = 5; int e = 6; int f = 7; int g = 8; int dp = 9; void setup() { pinMode(led1,OUTPUT); pinMod ...
  • 您可能遇到电源问题。 尝试使用墙上电源而不是使用USB端口为Arduino供电。 您还可以在伺服系统中添加一个大电容。 请参阅“如果伺服器行为不当” You could have an issue with the power supply. Try powering the Arduino with a wall power supply instead of using the USB port. You can also add a large capacitor in series with the ...

相关文章

更多

最新问答

更多
  • 在csproj中使用appdata环境变量(Use appdata environment variable in csproj)
  • 从背景返回后,Skobbler Map崩溃(Skobbler Map crashes after returning from background)
  • 如何保持对绑定服务的轮询?(How to keep polling a bound service?)
  • ASP.NET单选按钮jQuery处理(ASP.NET radio button jQuery handling)
  • Linux上的FORTRAN图形库(FORTRAN graphic library on Linux)
  • 我们如何根据索引更新dynamodb表(不基于primary has和range key)(how can we update dynamodb table based on index(not based on primary has and range key))
  • 功能包装避免重复(wrap of functions avoid duplicating)
  • Android BroadcastReceiver和Activity.onPause()(Android BroadcastReceiver and Activity.onPause())
  • 无法使用phonegap 2.4在Android上播放录音(unable to play audio recordings on android using phonegap 2.4)
  • VS2015 + Resharper:不要使用C#6(VS2015 + Resharper: Don't use C#6)
  • 大学电脑四级对初学者来说要多久能过
  • 特殊字符删除?(Special characters remove?)
  • Android视频教程现在网上的都比较零散呢?有些太坑爹了,感觉老师就是在想当然的讲
  • 计算同一个表中不同行之间的差异[重复](Calculate delta's between different rows in same table [duplicate])
  • Javaweb开发,技术路线是什么?该怎么写?
  • JavaScript只在php代码中执行一次(JavaScript only executes once inside php code)
  • 不兼容的字符编码:ASCII-8BIT和UTF-8(incompatible character encodings: ASCII-8BIT and UTF-8)
  • Clojure(加载文件)给出错误(Clojure (load-file) gives an error)
  • 为具有瞬态scala依赖性的spring-xd项目优化gradle(Optimize gradle for spring-xd project with transient scala dependency)
  • 如何才能在Alpha测试模式下发布我的应用程序?(How can I publish my app in Alpha test mode only?)
  • “没有为此目标安装系统映像”Xamarin AVD Manager(“No system images installed for this target” Xamarin AVD Manager)
  • maven中的Scalatest:JUnit结果(Scalatest in maven: JUnit results)
  • 使用android SDK将文件直接上传到存储桶中的文件夹(Upload a file directly to a folder in bucket using android SDK)
  • 是否应将plists导入CoreData?(Should plists be imported to CoreData?)
  • java.lang.reflect.InvocationTargetException JavaFX TableView(java.lang.reflect.InvocationTargetException JavaFX TableView)
  • 根据唯一列值动态创建多个子集(Dynamically create multiple subsets based on unique column values)
  • 使用CSS可以使HTML锚标签不可点击/可链接吗?(Is it possible to make an HTML anchor tag not clickable/linkable using CSS?)
  • 嵌套的模板可能性(Nested template possibilities)
  • 任何方式在iOS7 +上以编程方式打开蓝牙(Any way to turn on bluetooth programmatically on iOS7+)
  • 如何为给定的SQL查询编写JPA查询(How I can write JPA query for given SQL query)