Wireless Serial Array problem

Hi,
This is a RC Model Submarine, this section is for wireless diagnostic !

I need help on how to make my wireless communication working:

Wireless transmission by APC220 between 2 Arduino MEGA,

Operations:
I need, when the data arrive (2 numbers for prefix and 3 numbers for data), to be treated corresponding to the header value
for exemple: 01200

  • No treatement at all !
  • No variables printed beside menuItems, for example ( gite 200)
  • The program is always on serial receive, and it is not possible to navigate through the menus.
  • When navigation will be possible, i need a little temporisation between menu items when navigating
  • Do the Serial Buffer is cleaned automatically ?

Many thanks for who can help me

Regards

(Some parts of menus have been removed (9500 characters maximum on forum)

#include <MenuBackend.h>
#include <LiquidCrystal.h>

int secoursTR = 0, periscopeTR = 0, lampeTR = 0, sigsurfaceTR = 0, ledTR = 1, cdebarresavTR = 0, tiltbarresavTR = 0, cdeevballastTR = 0, powerTR = 0;
int prefix = 0,value = 0;
int sigairtankTR = 0, emergencyTR = 0, sigtempmotTR = 0, sigtempcompTR = 0, ppmbarresTR = 0;
int lestTR = 0, cdecaisseavTR = 0, cdecaissearTR = 0, sigballastfullTR = 0, waterTR = 0, compgTR = 0, compdTR = 0, airpowerTR = 0;
int  cdewatpumpTR = 0, cdeinvpumpTR = 0, rollvar = 0, ballastinTR = 0, ballastoutTR = 0;
int opentrapTR=0;                                                                                                                                                //  Temporisation de vidange du ballast
int menugval = 0, validval = 0, menudval = 0, outval = 0, menuval = 0;                                                                                            // Definition des variables de pilotage du menu
long voie1TR = 0, voie3TR= 0, voie5TR = 0, voie6TR = 0, voie7TR = 0;
int pitchTR = 0, rollTR = 0, sigprofTR = 0, siglampeTR = 0;
int timetorpTR = 0 , timemissTR = 0 , maxtempTR = 0 , maxprofTR = 0 , sinkadjustTR = 0;
int pressureTR = 0;
const int pressurepin = 8;
int myStr[5];
int i = 0;
int touche = 0, ad0val = 0;
const int ad0pin = 0;

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

MenuBackend menu = MenuBackend(menuUseEvent,menuChangeEvent);

MenuItem systeme = MenuItem("Systeme");
      MenuItem serial = MenuItem("View Serial", value);
      
MenuItem tester = MenuItem("TESTER");      
      MenuItem roll = MenuItem("Gite " , rollTR);
      MenuItem pitch = MenuItem("Pitch " , pitchTR);
      MenuItem sigtempmot = MenuItem("Temp Moteur " ,sigtempmotTR);
      MenuItem sigtempcomp = MenuItem("Temp Compr " , sigtempcompTR);
      MenuItem sigprof = MenuItem("Profondeur " , sigprofTR);
      MenuItem sigsurface = MenuItem("Capt Surf " , sigsurfaceTR); 
      MenuItem lampe = MenuItem("Lampe " , siglampeTR);
      MenuItem secours = MenuItem("Ballon " , secoursTR);
      MenuItem periscope = MenuItem("Periscope " , periscopeTR);
      MenuItem led = MenuItem("Led: " , ledTR);
      MenuItem cdebarresav = MenuItem("Bar AV R/S " , cdebarresavTR);
      MenuItem tiltbarresav = MenuItem("Bar AV Tilt" , tiltbarresavTR);
      MenuItem cdeevballast = MenuItem("EV Ballast " , cdeevballastTR);

            
MenuItem reglages = MenuItem("REGLAGES");
      MenuItem timetorp = MenuItem("Duree Torp" , timetorpTR);
      MenuItem timemiss = MenuItem("duree miss" , timemissTR);
      MenuItem maxtemp = MenuItem("Temp Max" , maxtempTR);
      MenuItem maxprof = MenuItem("Prof. max" , maxprofTR);
      MenuItem sinkadjust = MenuItem("Cal. Plongee" , sinkadjustTR);

void setup()
{
  Serial1.begin(9600);
  lcd.begin(16,2);
  menuSetup();
  lcd.setCursor(0,0);
  lcd.print("SUBSOFT V1.04");
  establishContact();  // send a byte to establish contact until receiver responds 

}

void loop()
{
  prefix = 0;
  if (Serial1.available() > 0) {      // get incoming byte:
      delay(5);
      i=1;      
      while(i<6){
         myStr[i] = Serial1.read();
         i++;
      // Serial1.print(myStr[5]);
      lcd.setCursor(1,1);
      lcd.print(myStr[5]);   
                }
      prefix = myStr[1] && myStr[2];
      value = myStr[3] && myStr[4] && myStr[5];
      lcd.setCursor(6,1);
      lcd.print(value);
      delay (200);
      }
       
  if (prefix == 'AA')
  {
      lcd.setCursor (14,0);
      lcd.print("OK");
  }
      
  else
  {    
      lcd.setCursor(14,0);
      lcd.print("  ");
      prefix = 00 ;
      value = 000;
      lcd.setCursor(14,0);
      lcd.print("KO");
      delay (100);
  }
    
  switch (prefix)
      {
    case 01: 
      rollTR = value;
      break;
    case 02:
      pitchTR = value;
      break;
    case 03:
      sigtempmotTR = value; 
      break;
    case 04:
      sigtempcompTR = value;
      break;
    case 05:
      sigprofTR = value;
      break;
    case 06:
      sigsurfaceTR = value;
      break;
  }
      
lcd.setCursor(0,1);
  ad0val = analogRead(ad0pin);
  if (ad0val >= 110 && ad0val <= 135)
  {
    touche = 3;                      // Touche UP
    menu.moveUp();
    //lcd.setCursor(0,0);
    //lcd.print("Up");
  }
  else if (ad0val >= 290 && ad0val <= 315)
  {
    touche = 4;                       // Touche DOWN
    menu.moveDown();
    //lcd.setCursor(0,0);
    //lcd.print("Down");
  }
  else if (ad0val >= 465 && ad0val <= 485)
  {
    touche = 1;                       // touche LEFT
    menu.moveLeft();
    //lcd.setCursor(0,0);
    //lcd.print("left");
  }
  else if (ad0val >= 0 && ad0val <= 150)
  {
    touche = 2;                        // touche RIGHT
    menu.moveRight();
    //lcd.setCursor(0,0);
    //lcd.print("Right");
  }
  else if (ad0val >= 700 && ad0val <= 730)
  {
    touche = 0;                        // touche SELECT
    menu.use();
    //lcd.setCursor(0,0);
    //lcd.print("Use");
  }
}

void menuSetup()
{     
  menu.getRoot().add(tester);
  //setup the tester menu item
  //we want a left movement to pint to tester from anywhere
  reglages.addLeft(tester);
  tester.addLeft(tester);
  tester.addBefore(systeme);
  tester.addAfter(reglages);
  reglages.addAfter(systeme);
  systeme.addAfter(tester);
  reglages.addBefore(tester);
 
 tester.addRight(roll);
      roll.addBefore(emergency);
      roll.addAfter(pitch);
      pitch.addAfter(sigtempmot);
      sigtempmot.addAfter(sigtempcomp);
      sigtempcomp.addAfter(sigsurface);
      sigsurface.addAfter(sigprof);
      sigprof.addAfter(lampe);
      lampe.addAfter(periscope);
      roll.addLeft(tester);
      pitch.addLeft(tester);
      sigtempmot.addLeft(tester);
      sigtempcomp.addLeft(tester);
      sigsurface.addLeft(tester);
      sigprof.addLeft(tester);
      lampe.addLeft(tester);
      periscope.addLeft(tester);
      secours.addLeft(tester);
      led.addLeft(tester);
      cdebarresav.addLeft(tester);
      tiltbarresav.addLeft(tester);
      cdeevballast.addLeft(tester);
      power.addLeft(tester);


void establishContact() {
  while (Serial1.available() <= 0) {
    Serial1.print('AA', BYTE);   // send a capital A
    delay(300);
  }
}
  if (Serial1.available() > 0) {      // get incoming byte:
      delay(5);
      i=1;      
      while(i<6){
         myStr[i] = Serial1.read();
         i++;
      // Serial1.print(myStr[5]);
      lcd.setCursor(1,1);
      lcd.print(myStr[5]);   
                }

If there is at least one character available to read, wait 5 milliseconds (what for?), then read all 5 of them. No. No. No.

Good thing that print of the 6th element of a 5 element array is commented out. Too bad the last character being read is stuffed beyond the end of the array.

The int myStr[5] statement reserves space for 5 elements. Those elements are [0], [1], [2], [3], and [4]. Indices start at 0, not 1.

Good thing you learned that before writing a lot of code.

hi, thanks for reply, i tried this code:
but the code received on lcd, is one number after one number, i really need to extract the prefix and the value,

wish you got idea, because i'm lost !

Thanks

  prefix = 0;
  if (Serial1.available() > 0) {      // get incoming byte:
      delay(5);
      //i=1;      
      //while(i<6){
         myStr = Serial1.read();
       //  i++;
      // Serial1.print(myStr[5]);
      lcd.setCursor(1,1);
      lcd.print(myStr , BYTE);
         myStr = Serial1.read();

Did you change how myStr was defined? It was an array, and now you are trying to assign a byte to whole array.

Oh, sorry, the int myStr[5] is now replaced by int myStr

Is that needed for read entire 5 numbers sent by Serial ?

Is that needed for read entire 5 numbers sent by Serial ?

To read 5 characters using Serial.read(), you need a 6 element array. After adding each character to the array, you need to append a NULL.

char inStr[6];

void loop()
{
  while(Serial.available() < 5)
  {
     // Do nothing (i.e. wait for 5 characters to be available)
  }

  for(int i=0; i<5; i++)
  {
     inStr[i] = Serial.read();
     inStr[i+1] = '\0';
  }

  int inNum = atoi(inStr);
}

It works fine, now i can see the 5 numbers on 1 on Lcd, but how to extract the prefix and the value ?

Thanks

It works fine, now i can see the 5 numbers on 1 on Lcd, but how to extract the prefix and the value ?

If the prefix is always two characters, and the value always 3:

char pref[3];
char valu[4];

void loop()
{
   // Get data in inStr...

   pref[0] = inStr[0];
   pref[1] = inStr[1];
   pref[2] = '\0';

   valu[0] = inStr[2];
   valu[1] = inStr[3];
   valu[2] = inStr[4];
   valu[3] = '\0';

   int prefNum = atoi(pref);
   int valuNum = atoi(valu);
}

Hi, this code sounds good, value printed on screen is ok, but now, the code following the Serial comm. does not work properly (check for buttons), and the code joined is not working, variable rollTR is not shown on screen, do you know how to get printed, on screen, Gite "rollTR variable" ?

Thanks

 MenuItem roll = MenuItem("Gite " , rollTR);