Selecting Timer functions

Hi There

I am developing with Photon and a Blynk apps.
The project is about to send from other microcontroler 5 sensor analog reading converted to 5 char arrays to the photon using serial.
I have the same project working OK since the last November but just for one sensor. On the project I am refer to in this post I repeat the one sensor snippet for the others ones.
The photon get connected and disconnected periodically going through breathing cyan, fast green, many short red blink an so on.
I am using the serial 1 for the photon.
The baud-rate for the photon and the other microcontroller are setting to the same value (Just for you to know it).
First, I consulted with Blynk forum and they told me that in the void loop, there are five “if” that runs thousand times for second. Then, they suggest I should use some Photon timer and not a Blynk Timer.
My point is to know what timer do you recommend me to use in this case.
I will appreciate very much your helps.

// This #include statement was automatically added by the Particle IDE.
#include <blynk.h>
#define BLYNK_printserial

char auth[] = "745cf23fe70543bcbfcd68673d82e3ff" ;
char Lbs_1[6]; // 1th sensor array
char Lbs_2[6]; // 2th sensor array
char Lbs_3[6]; // 3th sensor array
char Lbs_4[6]; // 4th sensor array
char Lbs_5[6]; // 5th sensor array

BlynkTimer timer;
int Email_1=0;
const unsigned long SendEmail=17000L;
unsigned long lastEmail; // Global initialize to 0

const unsigned long SendLbs_1=1000L;
unsigned long lastLbs_1; // Global initialize to 0

const unsigned long SendLbs_2=1000L;
unsigned long lastLbs_2; // Global initialize to 0

const unsigned long SendLbs_3=1000L;
unsigned long lastLbs_3; // Global initialize to 0

const unsigned long SendLbs_4=1000L;
unsigned long lastLbs_4; // Global initialize to 0

const unsigned long SendLbs_5=1000L;
unsigned long lastLbs_5; // Global initialize to 0


void sendPressure_1()

{
    int i=0;
    while( Serial1.available() && i<7)
        {
           Lbs_1[i]=Serial1.read(); 
           if (Lbs_1[i]=='\0;'     )
           {
               
               break;
           }
           i++;
            
        }

      i=0;
      Lbs_1[6]='\0';
      
      
      /* Send it over the Blynk server                              */
     Blynk.virtualWrite (V7,Lbs_1);
      
}



void sendPressure_2()

{
    int i=0;
    while( Serial1.available() && i<7)
        {
           Lbs_2[i]=Serial1.read(); 
           if (Lbs_2[i]=='\0;'     )
           {
               
               break;
           }
           i++;
            
        }

      i=0;
      Lbs_2[6]='\0';
      
      
      /* Send it over the Blynk server                              */
     Blynk.virtualWrite (V8,Lbs_2);
      
}


void sendPressure_3()

{
    int i=0;
    while( Serial1.available() && i<7)
        {
           Lbs_3[i]=Serial1.read(); 
           if (Lbs_3[i]=='\0;'     )
           {
               
               break;
           }
           i++;
            
        }

      i=0;
      Lbs_3[6]='\0';
      
      
      /* Send it over the Blynk server                              */
     Blynk.virtualWrite (V9,Lbs_3);
      
}


void sendPressure_4()

{
    int i=0;
    while( Serial1.available() && i<7)
        {
           Lbs_4[i]=Serial1.read(); 
           if (Lbs_4[i]=='\0;'     )
           {
               
               break;
           }
           i++;
            
        }

      i=0;
      Lbs_4[6]='\0';
      
      
      /* Send it over the Blynk server                              */
     Blynk.virtualWrite (V10,Lbs_4);
      
}


void sendPressure_5()

{
    int i=0;
    while( Serial1.available() && i<7)
        {
           Lbs_5[i]=Serial1.read(); 
           if (Lbs_5[i]=='\0;'     )
           {
               
               break;
           }
           i++;
            
        }

      i=0;
      Lbs_5[6]='\0';
      
      
      /* Send it over the Blynk server                              */
     Blynk.virtualWrite (V11,Lbs_5);
      
}


void SEmail()
{
    
   while( Serial1.available()>0) 
    
    {
      Email_1=Serial1.read();  
      if (Email_1==0xAA)   
      {
       Blynk.email("Subjet","Lbs_1>250");
       Email_1 =0; break;
      }   
    } 
          
        
        
    
    
    
    
    
}



void setup() {

// Debug Console
Serial1.begin(57600);
delay(5000);// Allow board to settle
Blynk.begin (auth);
}

void loop() {

unsigned long topLoop_1 = millis();
//unsigned long topLoop_2 = millis();
//unsigned long topLoop_3 = millis();
//unsigned long topLoop_4 = millis();
//unsigned long topLoop_5 = millis();

Blynk.run();

if ((topLoop_1-lastLbs_1 )>=SendLbs_1)

{
    lastLbs_1=topLoop_1;
    sendPressure_1();
}


if ((topLoop_1-lastLbs_2 )>=SendLbs_2)

{
    lastLbs_2=topLoop_1;
    sendPressure_2();
}

if ((topLoop_1-lastLbs_3 )>=SendLbs_3)

{
    lastLbs_3=topLoop_1;
    sendPressure_3();
}

if ((topLoop_1-lastLbs_4 )>=SendLbs_4)

{
    lastLbs_4=topLoop_1;
    sendPressure_4();
}

if ((topLoop_1-lastLbs_5 )>=SendLbs_5)

{
    lastLbs_5=topLoop_1;
    sendPressure_5();
}



}

That many short blinks would give away some crucial info about the error reason.
I’d exepcet it a sequence of SOS blinks followed by a number of slow blinks followed by another SOS pattern.
This number of blinks in between is interesting.

But one thing strikes me.
When you declare char Lbs_1[6] the last element you are allowed to address would be Lbs_1[5] since in C/C++ arrays are 0-indexed (0…5).

And that code is mainly five times the same thing. Unifying all that into reusable code blocks would be a better way to go.

1 Like

Hi ScuffR
Thank for your answer.
Yes, I was wrong regarding the array’s sub-index, I’ll fix that.
I’m not familiar with C++, can you please explain to me that in terms of C?
Are you suggesting to create a function " SendPressure called 5 times?
I will try to get a better info regarding those blinks.
Thank you again.

What exactly? The stuff I mentioned does work just the same in C++ as it does in C - although with C++ you might have additional options at hand.

Instead of five functions void sendPressure_x() why not use void sendPressure(int x)?
When you also replace the five char Lbs_x[6] arrays with one two-dimensional array char Lbs[5][6] (similarly do that for your other xxxx_x variables).

BTW, the semicolon has no busines in this expression if (Lbs_1[i]=='\0;' )

1 Like

ScruffR
Awesome, thanks for clarifying me.
Have a great weekend

2 posts were merged into an existing topic: Unable to put Photon in listening mode