Not coming out from function in a case to another case

Alright.. I think I know where it goes wrong.. but still faces prob of fixing it..

case send1:
      Serial.println("waiting P message!");
      millis();
      Serial.println("about to enter while rf95 loop");
      while (rf95.available())
      
        millisNow = millis();


        Serial.println("About to enter millis while loop");
        while ((millis() - millisNow) <= timeOut * 100)
        {
          if (rf95.recv(buf, &len))
         //do something here, like in previous reply,,
        if (xxx)
            {reply_message();}
       else
             rebroadcast_message();

stage=wait1;
 break;

so it does not get stuck when I shorten to timeOut*100.. but it was too short for my case.. if I make it longer,,it get stucked hmmmmm

addition: stucked even after the timeout..

I think you should start throwing a bunch more braces in there at various places.

Or take some out.

Same with parentheses.

Your semicolons look OK, not entirely sure though.

HTH

a7

sry but I am nt sure what you mean by this..?

Anyone can provide general solution..?
It is probably the problem of

but I do not know how to solve that..

I will try to add up to 100 then..thanks for the suggestion

It is still not working!!PLS HELP

Please (note spelling) post your code.

That’s a good starting point (at post #47)

Everyone is trying to help you.

How many more analogies do you need to read?

A mechanic can't fix your car unless you let him look under the hood.

A plumber can't fix your leak unless you let him look at your pipes.

A psychologist can't ease your mental struggles unless you let them into your head.

A programmer cannot fix your broken project until you let them see the code.

We seem to be missing a lot of variable declarations.
And code.

This is a complete waste of time.

sry I have added what should be in... the problem is timeout there... it runs the void reply_messages three times and stuck forever after that..

Addition: the routeRssiPairs can be ignored in this problem..

I thi the easiest solution already been suggested.

Move the thread to Gigs, and offer a fee to fix the problem.

You may get a better solution wth a complete rewrite, or you can simply offer $100 for a hack to fix your omission / stated problem.

I’m not watching any more.

You don't even have a call to the reply_message function in the code you've posted, you call reply_Pmessage(); You've got while loops that don't close, a switch case that doesn't close, void loop function that doesn't close. There's no void Setup() in what you've posted etc etc etc The code you have posted has ZERO chance of running.

So how can someone recreate/debug your code for you, when you do not give them code that will compile and allow them to see the problem for themselves?

This is getting quite boring now. Post.The.Full.Code

@lastchancename How can they even pay someone to fix the problem. They would need to send them the full code to fix it :rofl:

How are zz, zzz, zzzz, and zzzzz declared?

  Serial.println("Broadcasting P message ");
  strcpy(radiopacket1, "P");
  strcat(radiopacket1, ",");
  sprintf(zz, "%s", src_ID);
  strcat(radiopacket1, zz);
  strcat(radiopacket1, ",");
  sprintf(zzz, "%s", sID);
  strcat(radiopacket1, zzz);
  strcat(radiopacket1, ",");
  sprintf(zzzz, "%s", src_ID);
  strcat(radiopacket1, zzzz);
  strcat(radiopacket1, "|");
  itoa(xrssi, zzzzz, 5);
  strcat(radiopacket1, zzzzz);
  strcat(radiopacket1, "/");
  strcat(radiopacket1, "1");
  strcat(radiopacket1, ",");

Why use four different buffers? You can get the same effect with:

  char radiopacket1[80];
  Serial.println("Broadcasting P message ");
  strcpy(radiopacket1, "P,");
  strcat(radiopacket1, src_ID);
  strcat(radiopacket1, ",");
  strcat(radiopacket1, sID);
  strcat(radiopacket1, ",");
  strcat(radiopacket1, src_ID);
  strcat(radiopacket1, "|");
  itoa(xrssi, radiopacket1+strlen(radiopacket1), 5);
  strcat(radiopacket1, "/1,");

They were posted in reply #50, now deleted.
It's almost as if the OP doesn't want help.

Anyway, here's the code

#include <SPI.h>
#include <RH_RF95.h>
#include <string.h>
#include <stdio.h>
long randNum;
char messageType[2];
char sID[3];
char dID[3];
char hops[2];
int16_t pRSSI[6];
char routes[6][3];
char zz[7];
char zzz[7];
char zzzz[7];
char zzzzz[7];
unsigned long millisNow;
unsigned long millisNow1;
unsigned long millisNow2;

enum State {
  
  wait1,
  wait2,
  wait3,
  send3,  
  ,
}; 

enum State stage = wait1;
void reply_message()
{
  char radiopacket1[80];
  Serial.println("Broadcasting message ");
  strcpy(radiopacket1, "P");
  strcat(radiopacket1, ",");
  sprintf(zz, "%s", src_ID);
  strcat(radiopacket1, zz);
  strcat(radiopacket1, ",");
  sprintf(zzz, "%s", sID);
  strcat(radiopacket1, zzz);
  strcat(radiopacket1, ",");
  sprintf(zzzz, "%s", src_ID);
  strcat(radiopacket1, zzzz);
  strcat(radiopacket1, "|");
  strcat(radiopacket1, "1");
  strcat(radiopacket1, ",");

  Serial.println(radiopacket1);

  rf95.send(radiopacket1, strlen(radiopacket1) + 1);
  Serial.println("Waiting for packet to complete..."); delay(10);

  randNum = random (10, 30);
  delay(randNum);
  rf95.waitPacketSent();
  Serial.println("delay at random msecond");   // it stucks here after running three times

}

void rebroadcast_message()
{//similar working as in previous function
}
void loop()
{
  Serial.println("Entering loop function!");
  switch (stage)
  {
    case wait1:
      Serial.println("Wait for  message");
      delay(1000);
      millis();
      Serial.println("Start waiting2");
      //while (rf95.available())
      
        millisNow2 = millis();
       
        while ((millis() - millisNow) <= timeOut * 1000)
        {
          if (rf95.recv(buf, &len))
          {

            RH_RF95::printBuffer("Received: ", buf, len);
            Serial.print("Got: ");
            Serial.println((char*)buf);
            strlcpy (tempC, buf, sizeof tempC);
            uint8_t len2 = sizeof(tempC);
          

            uint8_t routeRssiPairs = 0;
            for (uint16_t k = 0; k < strlen(tempC); k++)
            {
              if (tempC[k] == '/')
                routeRssiPairs++;
            }
if ((routeRssiPairs !=0)&&(routeRssiPairs <= 6))
      {
        char radiopacket1[100];
        Serial.println("rebroadcasting  messages1");
        strcpy(radiopacket1, "P");
        strcat(radiopacket1, ",");
        sprintf(x, "%s", sID);
        strcat(radiopacket1, x);
        strcat(radiopacket1, ",");
        sprintf(xx, "%s", dID);
        strcat(radiopacket1, xx);
        strcat(radiopacket1, ",");
        
       
        rf95.send(radiopacket1, strlen(radiopacket1) + 1);
        Serial.println("Waiting for packet to complete..."); delay(10);
        
        rf95.waitPacketSent();
        Serial.println("delay at random msecond3");
      }//end of if routeRssipairs statement
      else if(routeRssiPairs == 0)
      {
        
        reply_Pmessage();
        }
      else
      {
        Serial.println("the routeRssiPairs has exceed six!");

      }//end of else statement
}
}
      stage = wait2;
      break;
      

case wait2:
      Serial.println("waiting  message2!");
      millis();
      Serial.println("about to enter while rf95 loop2");
      
      
        millisNow = millis();


        Serial.println("About to enter millis while loop1");
        while ((millis() - millisNow) <= timeOut * 500)
        {
          if (rf95.recv(buf, &len))
          {

            RH_RF95::printBuffer("Received: ", buf, len);
            Serial.print("Got: ");
            Serial.println((char*)buf);
            strlcpy(tehC, buf, sizeof tehC);
            xrssi = rf95.lastRssi();
            Serial.println(xrssi);

         }
        Serial.println("TimeOut2");
   

      stage = wait3;
      break;

Is Steph a troll?

1 Like

Hard to tell. Since February they have created 26 topics and 222 posts. Maybe the difficulty in getting working examples comes entirely from fear that their sketch will be stolen and turned in as someone else's class project. I have nearly given up doing anything but point to actual or potential programming errors in their snippets since none of their sketches compile.

Probably. But then it is almost everytime possible to narrow down the problem into a code of reduced size / lines that is ideally possible to compile and shows the undesired problem. Or does get not compiled but with the same error message than the original code.
For sure higher efforts for the OP, but Its their decision..