Boolean Function

i Arduino Community

I Hope somebody of you can help me with my problem:

Basically i try to manage that the Arduino is waiting for an ACK Message at the Serial Port before i send further Instructions to the Slave.
I'm using an Arduino Mega2560 as a Master of an EPOS2 50/5 by Maxonmotor. The communication goes over the Serial1 Channel.

I have tryed to declare the Waiting for the Acknowledge as a Function of the Datatype Boolean who turns false at receiving the Ack... But the Compiler is Reporting following Failure:
"function definition does not declare parameters"
And when i declare a boolean Variable as Argument of the Function its reporting:
"WaitforAck was not declared in this scope".

I have here a snippet of my Sketch:

byte ack, failack, OpCode, RXmot1[1];

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


OpCode= 0x11;
ack = 0x4F;
failack =0x46;
}

void loop()
{
Serial1.write(OpCode);
while (WaitforAck)
{}
Serial1.write(Commandword);
while (WaitforAck)
{}

}

boolean WaitforAck
{
return=true;
Serial1.readBytes(RXmot1[0], 1);
if (RXmot1 == ack)
{
return false;
Serial.println("Received ACK");
}
else if (RXmot1 == failack)
{
return true;
Serial.println("Received FailAck");
}else
{
return true;
Serial.println("Serial Timed Out");
}
}

Where is my fault? Or is there an much easier Way to wait for an specific incoming Byte at the SerialPort?

Many Thanks

Vortex86

byte ack, failack, OpCode, RXmot1[1];

A one element array just screams clueless.

boolean WaitforAck

All function declarations need parentheses around the argument list, EVEN IF THE LIST IS EMPTY (like it is for loop()).

return=true;

return is a keyword. You can NOT use it as a variable name. Variables must be typed.

if (RXmot1 == ack)
{
return false;
Serial.println("Received ACK");
}

It's pointless to put anything after the return statement. The return statement terminates the function. Obviously, nothing after that will be executed.

several mistakes,

I stripped it to something working fixing the errors, should get you going

byte ack, failack, OpCode;
char RXmot1;

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

  OpCode= 0x11;
  ack = 0x4F;
  failack = 0x46;
}

void loop()
{
  Serial.write(OpCode);
  while (WaitforAck())
  {
  }
}

boolean WaitforAck()
{
  RXmot1 = Serial.read();
  if (RXmot1 == ack)
  {
    Serial.println("Received ACK");
    return false;
  }
  else if (RXmot1 == failack)
  {
    Serial.println("Received FailAck");
    return true;
  }
  else
  {
    Serial.println("Serial Timed Out");
    return true;
  }
}

Thanks a lot! I will try it as soon i have access to my Board :sunglasses: !

while (WaitforAck())
  {
  }

This means that the Program is entering the While loop and waits there until the WaitforAck turns false...
So how di i ensure that the boolean expression WaitforAck is true at the beginning because the While Loop is checking the expression before execution. In a bad case there is no execution...

Cheers
Vortex

in C such a waiting loop is often written as

while (WaitforAck());