0

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!");
    }
  }
}
3
  • Have you used the API and languange documentation? Commented Aug 4, 2021 at 0:34
  • @TedLyngmo - Hey I'm kind of new to editing, and I guess you were at the same time that I was - but I formatted the code with proper spacing and whatnot - and it told me I needed to explain my edit - and so I did that both in the post and the edit summary - after refreshing from your edit - but it would not let me post. Any idea why? I'm not sure what I've done wrong. Commented Aug 4, 2021 at 0:40
  • 4
    The point is, have you searched "arduino String to int"? The very first hit is the Arduino documentation for toInt Commented Aug 4, 2021 at 0:42

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.