为什么我的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?
最满意答案
是的,这是因为你正在使用重载的
>
运算符到一个String
类型,它可能执行词法,而不是数字,比较。在执行算术运算之前将字符串数据转换为数字。
Yes it is due to the fact that you are using the overloaded
>
operator to aString
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和电机(Arduino and motors)[2022-03-15]
通常不建议从输出端为您的电机供电。 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 ... -
Arduino NFC PN532(Arduino NFC PN532)[2023-01-20]
常见的错误和编译器可以帮助你 改变这一行: 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 .... ...
-
矩阵键盘4 * 4 arduino(Matrix keyboard 4*4 arduino)[2022-04-13]
在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 ... -
与Arduino伺服(Servo with Arduino)[2022-02-15]
您可能遇到电源问题。 尝试使用墙上电源而不是使用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 ...