Sending commands to Arduino

I did some searching and i can't seem to find any good answer.

Is there a way to Send 4 characters to the Arduino reliably?

I can get one character to send all the time, but if i try two or more, i get the first character and three 'ÿ'

Although i could just send letters and numbers that limits the number of commands a lot more then if i can send multiple characters.

I could really use some help.

Thanks Kevin

Can you post your code and how you communicate with arduino?

You should be able to send long strings to Arduino from a PC reliably without a problem.

Are you shure your PC's serial port is configured correct ?

I was just playing with something similar this afternoon (an upcoming project I have bubbling around in my head)

The code I boiled it down to was this:

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

void loop() {
  if(Serial.available() == 4)
  {
  Serial.print(Serial.read());
  Serial.print("-");
  Serial.print(Serial.read());
  Serial.print("-");
  Serial.print(Serial.read());
  Serial.print("-");
  Serial.print(Serial.read());
  Serial.println();
  }
}

It just displays the input so far but it might help?

Easty.

Is there a way to Send 4 characters to the Arduino reliably?

Sending to the Arduino from where? PC? Mac? Linux? Could you post the code that’s doing the sending?

int led0 = 2;
int led1 = 3;
int led2 = 4;
int led3 = 6;
int led4 = 8;
int led5 = 10;
int led6 = 12;
int led7 = 13;

void setup() {
  pinMode(led0, OUTPUT);  
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  pinMode(led7, OUTPUT);
  flash(led0,150);
  flash(led1,150);
  flash(led2,150);
  flash(led3,150);
  flash(led4,150);
  flash(led5,150);
  flash(led6,150);
  flash(led7,150);
  Serial.begin(115200);

}

void loop() {
  int val1=0;
  int val2=0;
  int val3=0;
  int val4=0;
  if (Serial.available()) {
     val1 = Serial.read();
     val2 = Serial.read();
     val3 = Serial.read();
     val4 = Serial.read();
     Serial.print(val1);
     Serial.print(val2);
     Serial.print(val3);
     Serial.println(val4);
     }
  Serial.flush();
  }
}

void flash(int led, int wait) {
 digitalWrite(led,HIGH);
 delay(wait);
 digitalWrite(led,LOW);
 delay(wait);
}

I am either sending the commands from the Ardino serial console, or via pyserail using the python console I am using ubunto 8.10

--Kevin

The reason why this code doesn't work is you are not waiting until you have received enough characters. It waits for one and then reads it and then reads the next three without there being anything in the buffer.

Change if (Serial.available()) { to if (Serial.available() > 3) {

Thanks!

That worked

--Kevin