首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在arduino中将字符串转换为int

在arduino中将字符串转换为int
EN

Stack Overflow用户
提问于 2021-08-04 00:30:32
回答 1查看 112关注 0票数 0

我的if语句出现了一个错误,我需要你的帮助来修复它,这个错误说明从类型String到类型int的转换无效。

代码语言:javascript
复制
int inputPin = 8;
int pirState = LOW;
int val = 0;
int counter = 0;
String temp_obj;

#include <Wire.h>
#include <Adafruit_MLX90614.h>
#include <LiquidCrystal_I2C.h>

Adafruit_MLX90614 mlx = Adafruit_MLX90614();

LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  temp_obj = mlx.readObjectTempC();
  mlx.readObjectTempC()
  temp_obj = temp_obj.substring(5, 6);
  lcd.begin();
  lcd.backlight();
  pinMode(inputPin, INPUT);
  Serial.begin(9600);
  mlx.begin();
}

void loop() {
  val = digitalRead(inputPin);
  if (val == HIGH) {
    Serial.print("Tem ");
    Serial.print(mlx.readObjectTempC());
    Serial.println(" Celsius");
    lcd.setCursor(0, 0);
    lcd.print("temperature");
    lcd.print(mlx.readObjectTempC());
    counter = counter + 1;
    delay(90);
    temp_obj = temp_obj.substring(0);
    Serial.println(temp_obj);
  }
}

void kaiguan() {
  if (pirState == LOW) {
    Serial.println("Motion detected!");
    pirState = HIGH;
    Serial.println(mlx.readObjectTempC());
    if (int(temp_obj) < 30) {
      //an error has popped up for my if statement and I need YOUR help to fix it the error says invalid cast from type String to type int.
      lcd.setCursor(0, 1);
      lcd.print(temp_obj);
      lcd.setCursor(0, 2);
      lcd.print("Not detected,reset!");
    }
  }
}
EN

回答 1

Stack Overflow用户

发布于 2021-08-04 05:21:10

变化

代码语言:javascript
复制
int(temp_obj)

代码语言:javascript
复制
temp_obj.toInt()

您可以阅读有关official doc中的函数的更多信息,如注释中所指出的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68644122

复制
相关文章

相似问题

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