Go Down

Topic: Soil Moisture sensor (Read 5 times) previous topic - next topic

robtillaart

you should read the section about arrays, that can make your code far more compact

Code: [Select]

int flipPins[] = { 2, 3, 4, 6, 7, 8, 9, 11 };
int sensorPins[] = {4, 2, 1, 0};
int avg[] = {0,0,0,0};

setup:
for (int i=0; i< 8; i++)
{
  pinMode(flipPins[i], OUTPUT);
  digitalWrite(flipPins[i], LOW);
}

for (int i=0; i< 4; i++)
{
  pinMode(sensorPins[i], INPUT);
  digitalWrite(flipPins[i], LOW);
}


furthermore then you can introduce functions for repeating pieces of code:
Code: [Select]


void function(int number)
{
    digitalWrite(flipPins[number], HIGH);
    digitalWrite(flipPins[number+1], LOW);
    delay(flipTimer);

    int val1 = analogRead(sensorPin[number]);
    delay(flipTimer);

    digitalWrite(flipPins[number], HIGH);
    digitalWrite(flipPins[number+1], LOW);
    delay(flipTimer);

    int val2 = 1023 - analogRead(sensorPin[number]);
    avg[number] = (val1 + val2) / 2;

    digitalWrite(flipPins[number], HIGH);
    digitalWrite(flipPins[number+1], LOW);
}


The switch will become simpler
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Lentrave

tanks for the script, yes it's simpler but i have the same problem .... i cannot use 4 sensor at same time, i can read 3 but when i add one it give me bad read ....

Papa G

Maybe you shouldn't use digital pins 0 and 1.

Lentrave

I try nearly all analog pin combinations and i dont even see a patern... Random number on random pins... Maybe limitation with the A/D ...???

robtillaart

#define voltageFlipPin11 2
#define voltageFlipPin21 4

conflicts with these pins ?

#define sensorPin1 4
#define sensorPin2 2

IIRC Analog pins should be addressed as A0 A1 A2 A4 etc.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up