Problem with sending Serial Data from Java to Arduino

I have a problem about my java program keeps on sending all 4 values to Arduino instead of sending 1 at a time

I wonder is it something wrong with my Java Codes since arduino site already been tested with the serial monitor

This is what i have written in Java
public void writeData(int UpCounter, int DownCounter, int LeftCounter, int RightCounter)
if (UpCounter == 1)
{
output.write('w');
output.flush();
}

if(DownCounter == 1)
{
output.write('s');
output.flush();

}

if(LeftCounter == 1)
{
output.write('a');
output.flush();
}

if(DownCounter == 1)
{
output.write('d');
output.flush();
}

This is what in Arduino
void setup()
{
Serial.begin(9600);
Serial.println("Hello world!");

pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(13, OUTPUT);

}//end setup()

void loop(){

int id = Serial.read();

switch (id){
case 'w':
digitalWrite(13,HIGH);
delay(0500);
digitalWrite(13,LOW);
Serial.println("w!");
Serial.flush();
break;

case 's':
digitalWrite(13,HIGH);
delay(0500);
digitalWrite(13,LOW);
Serial.println("s!");
Serial.flush();
break;

case 'a':
digitalWrite(13,HIGH);
delay(0500);
digitalWrite(13,LOW);
Serial.println("a!");
Serial.flush();
break;

case 'd':
digitalWrite(13,HIGH);
delay(0500);
digitalWrite(13,LOW);
Serial.println("d!");
Serial.flush();
break;
default:{;}
}
}

please use code tags => # button

The Java code looks OK (some missing parenthesis), if the 4 parameters all are 1 then all "wsad" will be fired. That says the code.

If you don't want that ==> reset the 4 counters after every call to the dataWrite(...) or maybe in the datawrite.

Maybe you are looking for something like this (not tested - no jave guru)

public void writeData (int &UpCounter, int &DownCounter, int &LeftCounter, int &RightCounter)     // use references !
{
  for (; UpCounter> 0; UpCounter--)
  {
    output.write('w');
    output.flush();
  }
  for (; DownCounter> 0; DownCounter--)
  {
    output.write('w');
    output.flush();
  }
  for (; LeftCounter> 0; LeftCounter--)
  {
    output.write('w');
    output.flush();
  }
  for (; RightCounter> 0; RightCounter--)
  {
    output.write('w');
    output.flush();
  }

Thanks for the reply, the problem is, i can't make the counter to '0' since it is used to calculate the distances the rc car travelled

is that any other way that can transfer the moving signals (ip, down, left, right) instead of changing the value of counter to '0' ?

The Java code looks OK (some missing parenthesis), if the 4 parameters all are 1 then all "wsad" will be fired.

If the Java application does send "wsad", it won't affect the Arduino, since OP flushes the serial buffer after reading each character.

Which, in my opinion, is a serious mistake. Especially now with Serial.flush() doing different things on different versions of the IDE.

OP, I think you should give serious consideration to NOT flushing the input buffer if you are using 0023 or earlier. If you are using 1.0 or later, flushing the outgoing buffer is silly. Serial.flush() of the outgoing buffer simply blocks until the buffer is empty.

Thanks for the reply,

The arduino part has already been checked with serial monitor and working perfectly

My question is i want to send the output "wasd" each at a time instead of firing all at one time :stuck_out_tongue:

The arduino part has already been checked with serial monitor and working perfectly

Working perfectly would include doing nothing more than it needs to do. Since that is not the case, it is not working perfectly.

My question is i want to send the output "wasd" each at a time instead of firing all at one time

Then, you need to have separate functions to send the characters, and send them only when appropriate.