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:

-1 is what read returns when there's nothing there to read. Use Serial.available to check if anything has been received before trying to read from the Serial line and only read when something is there.

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 !

First, lose the DEC in there. You don't need that.

Secondly, what are you sending to this Arduino? What are you sending it from?

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
    }
}
if (Serial.read() != -1) {
    //send a code  every time a character is received from the serial port

Yes, but WHAT is sending data to this Arduino over the serial port and WHAT is it sending.

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

Just answer the damned question.

Do you have the Arduino connected to a computer and you're sending things to it from the serial monitor? Is it connected to another arduino that is sending things over the serial line? WHAT is the arduino connected to via serial?

See, in that tuorial they are opening the serial monitor on the PC and sending characters to the Arduino. Are you doing that? Do you have the Serial monitor open? Have you typed something in there and hit send?

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)

emans:
Yes Serial Monitor open

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

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.