Assigning result of for loop to a constant

Hello,

I would like to equalize result of a for loop to a constant. Then I will compare with another constant How can I do it?

Here the code results bits in hex code in one line and then goes for a new line.
if (ibusData[length + 1] == checksumByte){
for(int i = 0; i <= length + 1; i++)
{
Serial.print(ibusData*, HEX);*

  • Serial.print(" ");*
  • }*
  • Serial.println();*
    Thanks

You have to explain yourself better. It's hard to understand what you need. For example, a for loop normally does not have any result. Also, your for loop does not modify any variables.

You can't assign anything to a constant.
It's like, uh, a constant.

( but I like your italics)

mbt28:
Hello,

I would like to equalize result of a for loop to a constant. Then I will compare with another constant How can I do it?

Here the code results bits in hex code in one line and then goes for a new line.
if (ibusData[length + 1] == checksumByte){
for(int i = 0; i <= length + 1; i++)
{
Serial.print(ibusData*, HEX);*

  • Serial.print(" ");*
  • }*
  • Serial.println();*
    Thanks
    [/quote]
    My powers of deduction (checksumByte) leads me to believe that once you have confirmed that the checksum matches, you want to verify that the char array you parsed matched a char array you have stored… perhaps?
    if you were using two null terminated char arrays (aka C strings) you could you the strcmp() function.
    however, it seems that may not be the case because you have a checksum byte and it appears followed by nothing (cannot tell).
    so you can do it something like this (assuming that I am correct in my deductive powers:
    * *if (ibusData[length + 1] == checksumByte) {  bool match = true;  for(int i = 0; i <= length + 1; i++)  {    if (myKey[i] != ibusData[i])    {      match = false;      break;    }    Serial.println(ibusData[i], HEX);  }  if(match)  {    doSomething();  } }* *

Thanks for the replies.

@BulldogLowell

Actually what I exactly want is assigning result of the line below to something
Serial.println(ibusData*, HEX);*
It prints out the bytes that arduino received over serial port. And one line results something like this:
68 5 18 38 0 0 4D
When I receive this message I would like to send another message to serial port.
I hope I am clear enough. Thank you again.

So you want to assign ibusData to some variable?

someVariable = ibusData[i];

Where someVariable has the appropriate type. Without seeing the rest of your code it's hard to tell if that should be long or int.

Without seeing the rest of your code it's hard to tell if that should be long or int.

Or even an array. The assignment, though, will NOT assign "68" to some variable. It will assign 0x68 to some variable. if that isn't what you want, you need to be much clearer on what you expect to do with the saved data later.

Perhaps he's wanting to capture the results of the print statement as a string to forward and manipulate at a later time or place.

It's all supposition.

Perhaps he’s wanting to capture the results of the print statement as a string to forward and manipulate at a later timed or place.

It’s all supposition.

Actually not exactly because I think print statement writes it down byte by byte into a single line . So somehow I should capture that line.

So you want to assign ibusData to some variable?

Code: [Select]

someVariable = ibusData*;*
Where someVariable has the appropriate type. Without seeing the rest of your code it’s hard to tell if that should be long or int. [/quote]
This wont help because “i” is a variable which is in use in “for loop”.
Here is my total code, please dont mind about gapdetect.ino it just detects the timings between bits.
```
*// http://forum.arduino.cc/index.php?topic=234125.75

// this version is the one I posted on Forum modified to use an interrupt.
// 07Jun2014
// this version is modified to use a Timer2 interrupt to detect gaps to avoid the need
//  to wait for a new message when there is a long delay between messages.
// The Timer2 code is in gapDetect.ino
//
// Having detected the gap Timer2 will be inactive during a long gap.
//  It will only be restarted when the first bit of the next message is detected
// This means that detecting a gap signals the end of a message
// Previous versions of this code were based on detecting a new message.

const int CS_LWAKE = 8; // HIGH enables MCP2025 Tx

volatile boolean endOfMessage = false;

byte ibusData[37];  // assumes max message is 37 bytes
byte ibusCol = 0;
byte length;
byte checksumByte;
const byte cdc_poll [5] = { 0x68 , 0x03 , 0x18 , 0x01 , 0x72 }; //  Radio polling for CD changer. Expects response of 18 04 68 02 00 76.
const byte cdc_respond [6] = { 0x18 , 0x04 , 0x68 , 0x02 , 0x00 , 0x76 }; // CD changer responding to Radio to confirm existance.

void setup() {
  pinMode (CS_LWAKE, OUTPUT); // initialize pin.
  digitalWrite (CS_LWAKE, HIGH); // write pin HIGH - enables MCP2025 Tx
  Serial.begin(9600);
  Serial.println("-- IBUS Gap Detect version --");
  Serial1.begin(9600, SERIAL_8E1);
  Serial1.write(cdc_respond, sizeof (cdc_respond));
  beginGapDetection(); // function in gapDetect.ino

}

void loop() {
  getIbusStuff();
}

void getIbusStuff() {

if (endOfMessage) {
    startNewIbusMessage();
    saveIbusBytes(); // all of them
    endOfMessage = false;
    processData();
  }

}

void processData()
{
  length = ibusData[1];
  if (length == 0) {
    Serial.println(“Length Zero”);
    return;
  }
  byte checksumByte = 0;
  for (int i = 0; i <= length; i++){
    checksumByte ^= ibusData[i];
  }
  if (ibusData[length + 1] == checksumByte){
    for(int i = 0; i <= length + 1; i++)
    {
      Serial.print(ibusData[i], HEX);
      Serial.print(" ");
    }
    Serial.println();

if (last message == cdc_poll[6]){
    Serial1.write(cdc_respond, sizeof (cdc_respond));
  }

}
  else {
    Serial.println(“Checksum Bad”);

}
}

void startNewIbusMessage() {
  ibusCol = 0;
}

void saveIbusBytes() {

while (Serial1.available() > 0) {
    ibusData[ibusCol] = Serial1.read();
    ibusCol ++;
    if (ibusCol > 37) {
      ibusCol = 37;
    }
  }

}*

```

Can you give an example of exactly what you want to capture? Not the code you think should do it, but an example of the data and what you want to have in another variable. Is it just one byte or all of them? Do you want the bytes or do you want it as ascii text?

Everything you are printing is already there in ibusData array. So you really don't need to capture anything. You already have it saved in that array. Why not just use that?

Delta_G:
Everything you are printing is already there in ibusData array. So you really don't need to capture anything. You already have it saved in that array. Why not just use that?

;D

Hello,

I made it now. But I need a better solution for if (ibusData[3] == cdc_poll[3] && ibusData[4] == cdc_poll[4]) how can compare ibusData and cdc_poll with an easier way? Because there are pretty long messages as well.

Thank you.

// http://forum.arduino.cc/index.php?topic=234125.75

// this version is the one I posted on Forum modified to use an interrupt.
// 07Jun2014
// this version is modified to use a Timer2 interrupt to detect gaps to avoid the need
//   to wait for a new message when there is a long delay between messages.
// The Timer2 code is in gapDetect.ino
//
// Having detected the gap Timer2 will be inactive during a long gap.
//   It will only be restarted when the first bit of the next message is detected
// This means that detecting a gap signals the end of a message
// Previous versions of this code were based on detecting a new message.

const int CS_LWAKE = 8; // HIGH enables MCP2025 Tx

volatile boolean endOfMessage = false;

byte ibusData[37];  // assumes max message is 37 bytes
byte ibusCol = 0;
byte length;
byte checksumByte;
const byte cdc_poll [5] = { 0x68 , 0x03 , 0x18 , 0x01 , 0x72 }; //  Radio polling for CD changer. Expects response of 18 04 68 02 00 76. 
const byte cdc_respond [6] = { 0x18 , 0x04 , 0x68 , 0x02 , 0x00 , 0x76 }; // CD changer responding to Radio to confirm existance. 

const byte request2play [7] = { 0x68 , 0x05 , 0x18 , 0x38 , 0x03 , 0x00 , 0x4E };
const byte cdc_playing [12] = {0x18 , 0x0A , 0x68 , 0x39 , 0x00 , 0x09 ,0x00 , 0x3F , 0x00 , 0x06 , 0x05 , 0x76 }; //playing 06 05

void setup() {
  pinMode (CS_LWAKE, OUTPUT); // initialize pin.
  digitalWrite (CS_LWAKE, HIGH); // write pin HIGH - enables MCP2025 Tx
  Serial.begin(9600);
  Serial.println("-- IBUS Gap Detect version --");
  Serial1.begin(9600, SERIAL_8E1);
  Serial1.write(cdc_respond, sizeof (cdc_respond));
  delay(2000);
  beginGapDetection(); // function in gapDetect.ino

  
}


void loop() {
  getIbusStuff();
}

void getIbusStuff() {

  if (endOfMessage) {
    startNewIbusMessage();
    saveIbusBytes(); // all of them
    endOfMessage = false;
    processData();
  }

}

void processData()
{
  length = ibusData[1];
  if (length == 0) {
    Serial.println("Length Zero");
    return;
  }
  byte checksumByte = 0;
  for (int i = 0; i <= length; i++){
    checksumByte ^= ibusData[i];
  }
  if (ibusData[length + 1] == checksumByte){
    for(int i = 0; i <= length + 1; i++)
    {
      Serial.print(ibusData[i], HEX);
      Serial.print(" ");
    }
    Serial.println();
    Serial.write(cdc_poll[3]);
    Serial.println();
    
    if (ibusData[3] == cdc_poll[3] && ibusData[4] == cdc_poll[4]){
       Serial1.write(cdc_respond, sizeof (cdc_respond));
       Serial.print("HU polls for CDC - ");
       Serial.println();
    }

    if (ibusData[3] == request2play[3] && ibusData[4] == request2play[4]){
       Serial1.write(cdc_playing, sizeof (cdc_playing));
       Serial.print("HU request CDC to play - ");
       Serial.println();
    }

  }
  else {
    Serial.println("Checksum Bad");

  }
}

void startNewIbusMessage() {
  ibusCol = 0;
}

void saveIbusBytes() {

  while (Serial1.available() > 0) {
    ibusData[ibusCol] = Serial1.read();
    ibusCol ++;
    if (ibusCol > 37) {
      ibusCol = 37;
    }
  }

}

memcmp maybe?

If it's just positions 3 and 4 that you are comparing then what you are doing now is probably the easiest.

aarg:
You have to explain yourself better. It’s hard to understand what you need. For example, a for loop normally does not have any result. Also, your for loop does not modify any variables.

Also, you can’t assign to a constant.

Delta_G:
memcmp maybe?

If it’s just positions 3 and 4 that you are comparing then what you are doing now is probably the easiest.

Hi,

I made it with the following code, but I have a problem here I dont know how can create and array with multiple set of byte arrays

I mean lets say
byte msg1 [3] = {ab, bc, cd}
byte msg2 [2] = {bb, 01, 33}
byte msg3 [4] = {22, 34, 05}

I would like to create an array like byte messages [3] = {msg1, msg2, msg3} but it not possible in this way.

and here is my code that I was working on today:

byte message[4] = (mesaj1, mesaj2, mesaj3, mesaj4);

void assign_message() {
  for (int x=0; x <= sizeof message[x]; x++ ){
    check_message();
  }
}

void check_message() {
  int allmatch=1
  for (int i=0; i <= sizeof message[x]; i++){
    if ibusData[i] != message[i]{
      allmatch = 0;
      break;
    }
  }
    if allmatch = 1;
      action_message();
}

void action_message(){
  
}
 byte* messages [3] = {msg1, msg2, msg3}

mbt28:
Hi,

I made it with the following code, but I have a problem here I dont know how can create and array with multiple set of byte arrays

I mean lets say
byte msg1 [3] = {ab, bc, cd}
byte msg2 [2] = {bb, 01, 33}
byte msg3 [4] = {22, 34, 05}

Did you perhaps mean {0xab, 0xbc, 0xcd} ?

aarg:
Did you perhaps mean {0xab, 0xbc, 0xcd} ?

Yes, yes definitely. In the real code I wrote in that way. I will give a try to AWOL's suggestion.

Hello,

byte* message[4] = { cdc_poll, request2play, rq_paramethers, clear_screen };

I made in this way, I got an error.

receiver:40: error: invalid conversion from 'const byte* {aka const unsigned char*}' to 'byte* {aka unsigned char*}' [-fpermissive]

const byte* message[4] = { cdc_poll, request2play, rq_paramethers, clear_screen };