Help sending int from one arduino to another over serial and display it


i realize trying send int 1 arduino regular problem i've searched days , have tried several methods can't seem find relates i'm trying accomplish or methods work. i'm trying send value(int) [varies 3 digits 500 4 digits 3500 , 3 digits] 1 mega second mega on serial display value on second mega lcd. first mega receiving int correctly because can correctly display there. when try send int second mega , display it shows random numbers longer int being sent. looks not being translated correctly second mega i'm still new arduino , not sure mistake is.
setup: tx/rx on mega1 ->rx/tx on mega2, gnd mega1->gnd mega2

sending
code: [select]
void rpm()
{
      int value;
      obd.readpid(pid_rpm, value);
      serial2.print(value);
     
}


receiving
code: [select]
//read rpm sent master , print value on lcd
char value1[4];
int rpm;
 
 if(serial2.available() >0)
  {
   
    serial2.readbytes(value1, 2); //not sure if 2 bytes enough 4 digit number
    rpm = atoi(value1); //convert input value int
    if(rpm <= 999)
    {
      myglcd.printnumi(rpm, 480, 180, 4, '/'); //to put blank space in display when 3 digits read
    }
    else
    {
      myglcd.printnumi(rpm, 480, 180, 4); //remove blank , display 4 digits when 4 available
    }                                     //this if statement may unnecessary haven't
                                          //changed yet see, more concerned displaying
                                          //the correct numbers i'll clean up
  }


i appreciate help.

code: [select]
serial2.print(value);

this going send number ascii.  1234 send character '1' (which number 49) , character '2' (which number 50) , '3' (51) , '4' (52).

so take 4 bytes read 4 digit number.  , use atoi convert 4 character string you've read real number store in int. 

note store 4 digit number string, value1 have 5 characters long.  remember string needs 0 mark end. 


Arduino Forum > Using Arduino > Programming Questions > Help sending int from one arduino to another over serial and display it


arduino

Comments

Popular posts from this blog

Error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode - Raspberry Pi Forums

class MPU6050 has no member named begin

missing filename after '-o'