Declaring variables a quicker way

is there a way i could declare my variables in some kind of loop to prevent all the messy code? and if so would this be outside the main loop and the setup?

i am also exposing 8 int’s to the cloud to store in a mysql database so i can request them from the database and display in a webpage i basically want to know if the input is on or off would i be best using bool or would int the job i plan on having some php which says if the int is 1 then display on else if the int is 0 display off.

how many of these ints could i send in one publish command? currently im sending 7 but in the events log in console i only see 0 - 5 whith the value of 6 not displayed, i dont know if the box which displays the values in the console is not big enough to see the value for input 6 or my data is being cut off for some reason.

Thanks…Ben


char msg[256];        // used for snprintf for Publish.
/////////////////////////////////////////////////////////////////////////////////////
const int dimp0 = D0;  // set all the inputs
const int dimp1 = D1;
const int dimp2 = D2;
const int dimp3 = D3;
const int dimp4 = D4;
const int dimp5 = D5;
const int dimp6 = D6;

int dimpval0 = 0;
int dimpval1 = 0;
int dimpval2 = 0;
int dimpval3 = 0;
int dimpval4 = 0;
int dimpval5 = 0;
int dimpval6 = 0;

int lastdimpval0 = 0;
int lastdimpval1 = 0;
int lastdimpval2 = 0;
int lastdimpval3 = 0;
int lastdimpval4 = 0;
int lastdimpval5 = 0;
int lastdimpval6 = 0;




unsigned long lastTime = 0; // to store the last time for the timer
unsigned long now; // to store the now time which is used to compare against last time

void setup() {
    
    Serial.begin(9600);
    
     pinMode(dimp0, INPUT_PULLDOWN);
     pinMode(dimp1, INPUT_PULLDOWN);
     pinMode(dimp2, INPUT_PULLDOWN);
     pinMode(dimp3, INPUT_PULLDOWN);
     pinMode(dimp4, INPUT_PULLDOWN);
     pinMode(dimp5, INPUT_PULLDOWN);
     pinMode(dimp6, INPUT_PULLDOWN);

 }
void loop() {
    
         now = millis();
	        if ((now - lastTime) >= 5000)
	   {
            lastTime = now;
    
    dimpval0 =  digitalRead(dimp0);
    dimpval1 =  digitalRead(dimp1);
    dimpval2 =  digitalRead(dimp2);
    dimpval3 =  digitalRead(dimp3);
    dimpval4 =  digitalRead(dimp4);
    dimpval5 =  digitalRead(dimp5);
    dimpval6 =  digitalRead(dimp6);
    
    Serial.println("---------------------------------------------");
    Serial.print("input 0 Reads :");Serial.println(dimpval0);
    Serial.print("input 1 Reads :");Serial.println(dimpval1);
    Serial.print("input 2 Reads :");Serial.println(dimpval2);
    Serial.print("input 3 Reads :");Serial.println(dimpval3);
    Serial.print("input 4 Reads :");Serial.println(dimpval4);
    Serial.print("input 5 Reads :");Serial.println(dimpval5);
    Serial.print("input 6 Reads :");Serial.println(dimpval6);
    Serial.println("---------------------------------------------");
    
       
    
    publishDataToCloud();
  
	   }
}



void publishDataToCloud()
{
//lu is for unsigned long, f is for float
snprintf(msg, sizeof(msg), "{\"input0\":%d, \"input1\":%d, \"input2\":%d, \"input3\":%d, \"input4\":%d, \"input5\":%d, \"input6\":%d,}", dimpval0, dimpval1, dimpval2, dimpval3, dimpval4, dimpval6);
Particle.publish("photonmonitor", msg, PRIVATE, NO_ACK);
 return;
 }

Yes, using array :wink:

Yes.

You could expose one string variable containing all the numeric variables and parse them on your DB server.

JSON also supports arrays which would make your event data shorter - but with up to 622 characters you should be able to transfer many variables :wink:

1 Like

You're missing a value in your snprintf :wink:

1 Like

well spotted thank you very much

Thats great thanks

ok so my string would look something like this 1011101 depending on the inputs then somehow dissect it in the php script?

that's one possibility, but you could also use { \"input"\ : [ 0, 1, 2, 3, 4, 5, 6 ] } and on your receiving side just parses it like any JSON

    var myObj = JSON.parse(yourInputString);
    var field3 = myObj.input[3];

Thats’s great i will give it a go, Thank you

so if i change this

//snprintf(msg, sizeof(msg), "{\"input0\":%d, \"input1\":%d, \"input2\":%d, \"input3\":%d, \"input4\":%d, \"input5\":%d, \"input6\":%d,}", dimpval0, dimpval1, dimpval2, dimpval3, dimpval5 dimpval5, dimpval6);

for this


snprintf(msg, sizeof(msg),{ \"input"\ : [ 0, 1, 2, 3, 4, 5, 6 ] }

is this not going to just send the numbers 0123456 instead of the value inside dimpval0 etc?

Well, if the format string was enclosed in double quotes it would.
It seems you need to revisit how snprintf() actually works.

i will thanks i am just looking at arrays. i think i may have it

  int dimpval[7];
  int i;
  for (i = 0; i < 6; i++)
     dimpval[i] = i;
1 Like

But you are only filling the first 6 of 7 elements in your loop

im afraid at the moment im going to have to stick with the long winded version. i can create an array and add in the values but for some reason i cant understand how to change them out for variables.

int dimpval[7] = {0, 1, 2, 3, 4, 5, 6};

What do you mean?

Are you looking for something like this?

const int N = 7;
const int dimp[N] = { D0, D1, D2, D3, D4, D5, D6 };
int dimpval[N]; // initialised to 0 by default
int lastval[N];

void setup() {
  for (int i = 0; i < N; i++) 
    pinMode(dim[i], INPUT_PULLDOWN);
}

void loop() {
  static uint32_t lastTime = 0;
  if ((millis() - lastTime) < 5000) return;
  lastTime = millis();
  Serial.println("---------------------------------------------");
  for (int i = 0; i < N; i++) {
    dimpval[i] = digitalRead(dimp[i]);
    Serial.printlnf("input %d reads: %d", i, dimpval[i]);
  }
  Serial.println("---------------------------------------------");
}

But don’t just copy that code, try to understand why and how it works - if something isn’t clear just ask.

3 Likes

thanks yes thats what i intend to do

are we saying

const int N = 7;
const int dimp[N] = { D0, D1, D2, D3, D4, D5, D6 };

there are 7 inputs because N = 7?

Yup - or rather “N = 7 because you want 7 inputs”

When you know you will need that number more frequently (e.g. here for the loops and three different arrays) it’s best to define a variable which you’d only need to alter once in case you want to change the number of inputs.
If you had 7 as a number literal in all places chances are that you either miss one or may replace a unrelated 7 - let alone all the extra time needed to do that :wink:

thats great really thank you for your time. I think i get the rest of it

void setup() {
  for (int i = 0; i < N; i++) 
    pinMode(dim[i], INPUT_PULLDOWN);
}

void loop() {
  static uint32_t lastTime = 0;
  if ((millis() - lastTime) < 5000) return;
  lastTime = millis();
  Serial.println("---------------------------------------------");
  for (int i = 0; i < N; i++) {
    dimpval[i] = digitalRead(dimp[i]);
    Serial.printlnf("input %d reads: %d", i, dimpval[i]);
  }
  Serial.println("---------------------------------------------");
}

in set up we run through a for loop N times and set dimp[i] which will be 01234567 to pulldown
then we run through another for loop N times to read the inputs of the value of the loop to the variable number of the loop.

but would i have to run snprintf statement through a for loop?

i also see you have changed my timer had i made a mistake?

for (int i = 0; i < N; i++) {
Serial.printlnf("input %d reads: %d", i, dimpval[i]);

sorry with this instead

snprintf(msg, sizeof(msg), "{\"input0\":%d}", dimpval[N]

You can create the string via a loop or in this case just write the elaborate version.
That's up to you, I'd probably use a loop.

No mistake, I just prefer to reduce the level of indentation by not wrapping the whole content of loop() in an overarching if() block but rather bail out early.
And I also like using static local variables since then I can reuse the same variable name in any other function where I need one that does exactly the same thing.