Reading Int from Serial

Im trying to read a Integer from serial and turn on/off an LED based on the value. Why doesnt this work?

int ledRed = 13;      // LED connected to digital pin 4
int inInt;

void setup(){
  // Open serial connection.
  Serial.begin(9600);
  pinMode(ledRed, OUTPUT);

}

void loop(){
  
  Serial.write("The Value of inInt:");
  Serial.write(inInt);
  Serial.write("\n");

           
          if (inInt == 1) {
           digitalWrite(ledRed, HIGH);
            Serial.write("Varswitch is one");
           Serial.flush();
          }
          if (inInt == 0) {
            digitalWrite(ledRed, LOW);
            Serial.flush();
          }
         
     delay(1000);     
  if(Serial.available() > 0){    
         inInt = Serial.read();
         
        
         
  }
}

It displays the values correctly, but my assumption in it's not storing it as an integer? or there is a hidden character or something?

Serial.Read() reads bytes(8bit), not intergers(16bit). Also you are not specifying what is sending these values. If you are using the serial monitor the values are NOT sent as raw binary but as a ASCII string containing the individual digits. This string is terminated with a lf/cr (/10/13). If you want to read a integer sent by serial monitor there is a function called Serial.parseInt()

The examples in serial input basics mau be useful.

...R

int led=13;
void setup()
{
  Serial.begin(9600); 
pinMode(13,OUTPUT);
}
void loop()
{
  int val = Serial.read() - '0';
  if (val == 1)
  { 
    Serial.println("LED ON");

 digitalWrite(13,HIGH);
  }
  else if (val == 0) // test for command 0 then turn off LED
  {

    Serial.println("LED OFF");
 digitalWrite(13,LOW);
  }
  Serial.println("arduino");
  delay(1000);

}

Try this code if you send 1 LED 13 will ne high ; if you send 0 LED will be low. If open Serial monitor keep printing arduino.

When ever send command 0/1 relevant thing will work

Thanks Amps that worked from the Serial Monitor

nilton61: Serial.Read() reads bytes(8bit), not intergers(16bit). Also you are not specifying what is sending these values. If you are using the serial monitor the values are NOT sent as raw binary but as a ASCII string containing the individual digits. This string is terminated with a lf/cr (/10/13). If you want to read a integer sent by serial monitor there is a function called Serial.parseInt()