henry
henry

Reputation: 1

Converting string to int in arduino

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.

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!");
    }
  }
}

Upvotes: 0

Views: 1408

Answers (1)

HiEd
HiEd

Reputation: 160

Change

int(temp_obj)

to

temp_obj.toInt()

You can read more about the function in the official doc as pointed out in the comment.

Upvotes: 1

Related Questions