Help with 2 moisture sensors and sparks core

I can’t seem to figure out how to get 2 sensors to work, I took code I found and duplicated it from 1 to 2 sensors. I know it is probably something simple.

//Team SoilStatus – K. Thach & B. Padgett
//Code to monitor soilstatus using Analog moisture counts from a Soil Moisture Stick.

//------------------------------
int thresholdup=6000; //Count Threshold
int thresholddown=350;
const int sensorPin = A7;
const int sensorPin2 = A6;
int sensorValue=0;
int sensorValue2=0;

void setup()
{
// put your setup code here, to run once:
pinMode(sensorPin, INPUT);
pinMode(sensorPin2, INPUT);

}
void sensor2()
{
int sensorValue2;
sensorValue2 = analogRead(sensorPin2);
if(sensorValue2<=thresholddown)
{

Particle.publish(“Plant 2 SoilStatus”,“DRY!”,60,PUBLIC);
delay(5000);
}
else if (sensorValue2>thresholdup)
{

Particle.publish(“Plant 2 SoilStatus”,“Swimming!”,60,PUBLIC);
}
else
{
Particle.publish(“Plant 2 SoilStatus”,“OK!”,60,PUBLIC);
}
delay(30000); //Checks every 5 minutes
Particle.publish(“Plant 2 SoilStatus”, String(sensorValue2), 60, PUBLIC);

}
void loop()

{ // put your main code here, to run repeatedly:
int sensorValue;
sensorValue = analogRead(sensorPin);
if(sensorValue<=thresholddown)
{

Particle.publish(“Plant 1 SoilStatus”,“DRY!”,60,PUBLIC);
delay(5000);
}
else if (sensorValue>thresholdup)
{

Particle.publish(“Plant 1 SoilStatus”,“Swimming!”,60,PUBLIC);
}
else
{
Particle.publish(“Plant 1 SoilStatus”,“OK!”,60,PUBLIC);
}
delay(30000); //Checks every 5 minutes
Particle.publish(“Plant 1 SoilStatus”, String(sensorValue), 60, PUBLIC);

}

I took code I found and duplicated it from 1 to 2 sensors. I know it is probably something simple.

As this one and number 2? Do you count that way? Or do you number everything you are counting?

Particle.publish

On an Arduino? The Particle forum is down the road a ways.

It hardly does any good to write a function, if you never call the function.

As in the code was written for 1 sensor, I duplicated a portion of it for 2 sensors? I have no clue what you mean by numbering what you count to be honest - PaulS

What I need help with is making both setups publish during the void loop. only 1 sensor will publish when I have 3 states for 2 sensors. But if I remove the second part of code I duplicated it runs perfectly fine.

I asked over on particle as well, I use arduino references to make up for the lack of info in the particle community compared to here. Figured I might as well ask here since the code is the same for 90% of stuff I have found

void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}

void loop() {
// read the input on analog pin 1:
int sensorValue1 = analogRead(A6);

if (sensorValue1 >=600 && sensorValue1 <=800){
Serial.println(sensorValue1);

Particle.publish(“Plant 1 SoilStatus”,“DRY!”,60,PUBLIC);
delay(5000);
}
else {
Serial.println(sensorValue1);
Particle.publish(“Plant 1 Soil Status”,“OK!”,60,PUBLIC);
delay(5000);
}{ //Checks every 5 minutes
Particle.publish(“Plant 1 SoilStatus”, String(sensorValue1), 60, PUBLIC);
delay(30000);
}

int sensorValue2 = analogRead(A7);

if (sensorValue2 >=600 && sensorValue2 <=800){
Serial.println(sensorValue2);
Particle.publish(“Plant 2 SoilStatus”,“DRY!”,60,PUBLIC);
delay(5000);
}
else {
Serial.println(sensorValue2);
Particle.publish(“Plant 2 SoilStatus”,“OK!”,60,PUBLIC);
}
delay(5000);
{
Particle.publish(“Plant 2 Soil Status”, String(sensorValue2), 60, PUBLIC);
delay(30000); //Checks every 5 minutes
}}

What I mean by 3 states

I can get this to work above where it only has 2 statements dry or ok, but when the 3rd if appears the code breaks. Now I can get it to work with 1 sensor in the void loop. The moment I try 2 it starts saying invalid IF and other errors.

I have no clue what you mean by numbering what you count to be honest - PaulS

I mean that instead of

int val;
int val2;

You should have

int val1;
int val2;

where both related names have numeric suffixes.

But if I remove the second part of code I duplicated it runs perfectly fine.

I’m sure that, with the code you duplicated, it runs fine, too.

If you make a copy of some code, as a separate function, don’t you think that calling the function somewhere would be necessary?

The point about numbering both variables is meant to help you see that the next logical step is to create an array of them. Then, you can have ONE function that you call, with the index of the array(s). Then, the function can access the proper pin numbers, class instances, etc. and can store the data in the appropriate place(s).

int sensorPins[] = {A6, A7};
int sensorVals[2];

void loop()
{
   for(byte i=0; i<2; i++)
   {
      publishSensorData(i);
   }
   delay(someRidiculousAmountOfTime);
}

void publishSensorData(byte index)
{
   sensorVals[index] = analogRead(sensorPins[index]);
   Serial.print("Sensor ");
   Serial.print(index);
   Serial.print(" reading: ");
   Serial.println(sensorVals[index]);

   // Do something with the reading...
}

You should be able to see how to use the value in index and the sensorVals array to publish the correct message.

Notice that publishing the message is NOT the place to stick your head in the sand. Do that in loop(), so that, when you realize that the Arduino is doing nothing most of the time, you can look at the blink without delay example, to see how to publish without delay, so that the Arduino CAN do other things while not publishing data.

Or the Particle, in your case.

Awesome, I will give this a try. Also what does “for(byte i=0; i<2; i++)” do

Also what does “for(byte i=0; i<2; i++)” do

Just what you’d expect a for statement to do.

Thank you, the code above did the trick, The last thing I am still trying to figure out is how I can assign names to the index.