0

I am trying to communicate Pc - Arduino Uno.

My Arduino code is here: (It is working as below)

int ndx = 0;
char pack[12];            
void setup() {
Serial.begin(9600);
while(!Serial){;}
}

  void loop() { 

   while(Serial.available() ){  
    char rb = Serial.read();
    pack[ndx] = rb;
    ndx++;
    Serial.write(rb);
    if(ndx == 12){ 
        ndx=0;
      }     
    }
    }

If I try in

Serial.write(rb);

Like that:

Serial.write(pack[ndx]);

Program is acting weird, I mean sending weird bytes. I do not know what is the reason. Can you help me, please? What is going on in code? My python code: https://github.com/mucahitkayadan/Python

  • A character is send as a ascii character, not the decimal value. Try to cast it to an int or try using DEC as second parameter. With such a low baudrate, should it be better to add a seperator? for example a comma between the values. – Jot Aug 16 '18 at 9:12
  • we do not see the definition of the array – Juraj Aug 16 '18 at 10:17
  • @Juraj Sorry, There were some unnecessary definions. When I was posting,I deleted them, and array definion was out of sight. I fixed,edited it. – Mucahit Kayadan Aug 16 '18 at 10:37
  • @Jot I can add comma in between every values in python list, but will it be easier? I have not understood the algorithm you are talking about. – Mucahit Kayadan Aug 16 '18 at 10:37
  • Post the code that is not working – Juraj Aug 16 '18 at 12:01
0

you replace Serial.write(rb); to Serial.write(pack[ndx]); and get?:

char rb = Serial.read();
pack[ndx] = rb;
ndx++;
Serial.write(pack[ndx]);

then you read an uninitialized array item on the next index

  • Actually, it is working without using array. I you use directly " Serial.write(inByte); " it is working normally. But if you use " Serial.write(array[index]); " it is working weird. Sending 0-255 random numbers or maybe not random but I do not know reason or meaning of that weird numbers. – Mucahit Kayadan Aug 16 '18 at 10:14
  • I changed the aswer – Juraj Aug 16 '18 at 11:26
  • I have to process the data, so I need to process with array, namely 'pack[index]', thats why I don't want use Serial.write(rb); When I put "Serial.write(pack[ndx]);" program giving me different weird numbers. – Mucahit Kayadan Aug 16 '18 at 11:57
  • do you have ndx++ before write(pack[ndx]);? – Juraj Aug 16 '18 at 12:01
  • Yes, absolutely for this reason. Now it is working. So basic thing I can not believe myself, overlooked. – Mucahit Kayadan Aug 16 '18 at 12:33
0

The sequence:

pack[ndx] = rb;
ndx++;
Serial.write(rb);

modifies ndx before printing. So in your first case - printing 'rb' - you'll print the value you just stored in the array; in the second case printing 'pack[ndx]' - you'll print whatever happens to be in the following member of the array. Swapping the second and third statements in my excerpt will fix it.

  • My packs have 12 bytes and I have totally 7 pack.I tried to just send my index, namely 'ndx'. And it receives correctly. I mean, pc receives 0-11 numbers for every time for every pack. It is expected. That thing is what I could not understand, for every iteration rb and pack[rdx] are equal. Why write(rb) is working correctly but write(pack[ndx]) does not work? – Mucahit Kayadan Aug 16 '18 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.