Serial.read() always = -1 !

Hey !

My Serial.read() is always equal to -1. Where is the problem ?

This is my code:

void setup()
{
    Serial.begin(9600);
}

void loop() {
  
  Serial.println(Serial.read());


}

When I start my program and Open "Monitor Serial"

Serial.println(Serial.read()) return ALWAYS -1 !

Why ? Can you explain my error pls ?

Thanks ! :slight_smile:

Thanks for your answer quick ! :wink:

But I tested this code:

int incomingByte = 0;   // variable pour lecture de l'octet entrant

void setup() {
        Serial.begin(9600);     // ouvre le port série et fixe le débit à 9600 bauds
}

void loop() {

        // envoie une donnée sur le port série seulement quand reçoit une donnée
        if (Serial.available() > 0) { // si données disponibles sur le port série
                // lit l'octet entrant
                incomingByte = Serial.read();

                // renvoie l'octet reçu
                Serial.print("Octet reçu: ");
                Serial.println(incomingByte, DEC);
        }
}

And have no return ... Why ?

Thanks !

My real code is for send and received IR:

/* Example program for from IRLib – an Arduino library for infrared encoding and decoding
 * Version 1.3   January 2014
 * Copyright 2014 by Chris Young http://cyborg5.com
 * Based on original example sketch for IRremote library 
 * Version 0.11 September, 2009
 * Copyright 2009 Ken Shirriff
 * http://www.righto.com/
 */
#include <IRLib.h>

IRsend My_Sender;

//Create a receiver object to listen on pin 11
IRrecv My_Receiver(11);

IRdecode My_Decoder;


int incomingByte = 0;

void setup()
{
    Serial.begin(9600);
    My_Receiver.enableIRIn(); // Start the receiver
}

void loop() {
  // Serial.println(Serial.read());
   

  if (Serial.read() != -1) {
    //send a code  every time a character is received from the serial port
    //Sony DVD power A8BCA
   // My_Sender.send(SONY,0xa8bca, 20);
   My_Sender.send(SONY,0xA8B5F, 20);
   Serial.print("send");
   delay(10000);
  }
  
    if (My_Receiver.GetResults(&My_Decoder)) {
      My_Decoder.decode();		//Decode the data
      My_Decoder.DumpResults();	//Show the results on serial monitor
      My_Receiver.resume(); 		//Restart the receiver
    }
}

I'm following this tutorial Sending IR Codes | Using an Infrared Library on Arduino | Adafruit Learning System

My Arduino (UNO) is connected to my computer yes :slight_smile:

EDIT:

Yes Serial Monitor open ... The problem come from this condition:

  if (Serial.read() != -1)

Delta_G:
Did you type a chracter in there and hit send to send it to your Arduino?

Jeez ... this sounds harder than getting paint off the front of your best tuxedo.

...R

Jeez ... this sounds harder than getting paint of the front of your best tuxedo.

What? Getting paint on a tux is easy.

If this works on the serial monitor, and doesn't work when you attempt to serial read form your IR, then the problem is not in the reading code but in the setup of the IR.

The behaviour that you are getting is that the Serial object never contains any bytes for you to read. One possible thing - for instance - that can cause this behaviour is if your IR transmitter doesn't have any batteries in it.