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;
}
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.
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
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?
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.