Problems with Serial Communication

I'm trying to control my Arduino via serial input from a computer. I was having problems with getting this to work, so I ran a very basic code as a test. This prints -1 constantly. Am I doing something wrong in the code here, and how might I fix the problem?

void setup() {
Serial.begin(9600);
int Data = 0;
}

void loop() {
while(Serial.available() > 0){
Data = Serial.read();
Serial.print(Data);
}
}

Try this

if (Serial.available() > 0){
byte Data = Serial.read() ; // try char also, see what is different
Serial.print (Data);
}

Byte and Char give the same thing.

TheWitchHunter:
Byte and Char give the same thing.

And that would be ?

Something is missing here. The code in the OP will not compile. Data is not defined in the scope of loop function.

It should not even compile. Data is declared in setup and not in loop.

void loop()
{
  int data;
  if(Serial.available() > 0)
  {
    data = Serisl.read();
    Serisl.println(data);
  }
}

You can remove Data from setup.

ieee488:
And that would be ?

-1 repeating

sterretje:
It should not even compile. Data is declared in setup and not in loop.

void loop()

{
  int data;
  if(Serial.available() > 0)
  {
    data = Serisl.read();
    Serisl.println(data);
  }
}




You can remove Data from setup.

Thanks that fixed it, though I ended up doing something different for my project.

Have a look at Serial Input Basics

...R