1

EDIT DOWNSTAIRS:

Hello beautiful people of the internet! How are you smart people doing with all that's going on these days. Okay so the title says it pretty much. I've got a code and inside of my if's I have added something that lets me see the data from the sensor in percentage on my LCD. It works but it just stays at the number it read when it went on that mode. How do I make it so it starts "updating" automatically. Sooo I am talking about this part mostly (and other exactly like it)

    if (whichPlant == 2) {
      lcd.setRGB(plant3R, plant3G, plant3B);
      lcd.clear();
       lcd.display();
      lcd.print("Orchid");// print the results to the LCD Display:
      sensorValue = analogRead(waterSensorPin);// read the analog in value:
      sensorValue = map(sensorValue, 0, 750, 0, 100);
      lcd.setCursor(0, 1);
      lcd.print(sensorValue);
      lcd.print("% Enough water");
      Serial.println(sensorValue);
    }

And here is my entire code if you're interested

#include <Servo.h>


#include <Wire.h>
#include "rgb_lcd.h"
rgb_lcd lcd;

const int numberOfPlants = 4;
const int noPlant = -1;  // index meaning no plant is being monitored

const int plant1R = 0;
const int plant1G = 0;
const int plant1B = 255;
const int zeroR = 0;
const int zeroG = 0;
const int zeroB = 0;
const int plant2R = 0;
const int plant2G = 255;
const int plant2B = 0;
const int plant3R = 255;
const int plant3G = 0;
const int plant3B = 0;
const int plant4R = 255;
const int plant4G = 0;
const int plant4B = 255;

// Pinout.
const int waterSensorPin = A1;
const int buttonPin = 2;
const int servoPin = 5;
const int ledPins[numberOfPlants] = {12, 11, 10, 9};

// Moisture ranges for the different plants.
const int moistureMinima[numberOfPlants] = {  0,   0,   0,   0};
const int moistureMaxima[numberOfPlants] = {195, 682, 750, 950};

// Servo holding the indicator needle.
Servo indicator;
int sensorValue = 0; // value read from the Soil Moisture
void setup() {
  lcd.begin(16, 2);
  Serial.begin(9600);
  pinMode(waterSensorPin, INPUT);
  pinMode(buttonPin, INPUT);
  for (int ledNo = 0; ledNo < numberOfPlants; ledNo++) {
    pinMode(ledPins[ledNo], OUTPUT);
  }
  indicator.attach(servoPin);
}

void loop() {
  // Which plant are we currently monitoring?
  static int whichPlant = noPlant;
  // Switch to next plant on button press.
  if (digitalRead(buttonPin) == HIGH) {
    if (whichPlant != noPlant) {
      digitalWrite(ledPins[whichPlant], LOW);  // turn off previous LED
    }
    ++whichPlant;
    if (whichPlant == numberOfPlants) {
      whichPlant = noPlant;
    } else {
      digitalWrite(ledPins[whichPlant], HIGH);  // turn on current LED
    }
    if (whichPlant == 3) {

      digitalWrite(10, HIGH);  // turn off previous LED
      digitalWrite(12, HIGH);  // turn off previous LED
      lcd.setRGB(plant4R, plant4G, plant4B);
      lcd.clear();
       lcd.display();
      lcd.print("Swamp Plant");// print the results to the LCD Display:
      sensorValue = analogRead(waterSensorPin);// read the analog in value:
      sensorValue = map(sensorValue, 0, 950, 0, 100);
      lcd.setCursor(0, 1);
      lcd.print(sensorValue);
      lcd.print("% Enough water");
      Serial.println(sensorValue);
      delay(500);  // crude debouncing
    }
    if (whichPlant == 2) {
      lcd.setRGB(plant3R, plant3G, plant3B);
      lcd.clear();
       lcd.display();
      lcd.print("Orchid");// print the results to the LCD Display:
      sensorValue = analogRead(waterSensorPin);// read the analog in value:
      sensorValue = map(sensorValue, 0, 750, 0, 100);
      lcd.setCursor(0, 1);
      lcd.print(sensorValue);
      lcd.print("% Enough water");
      Serial.println(sensorValue);
    }
    if (whichPlant == 1) {
      lcd.setRGB(plant2R, plant2G, plant2B);
     lcd.clear();
      lcd.display();
      lcd.print("Sunflower");// print the results to the LCD Display:
      sensorValue = analogRead(waterSensorPin);// read the analog in value:
      sensorValue = map(sensorValue, 0, 682, 0, 100);
      lcd.setCursor(0, 1);
      lcd.print(sensorValue);
      lcd.print("% Enough water");
      Serial.println(sensorValue);
    }
    if (whichPlant == 0) {
      lcd.setRGB(plant1R, plant1G, plant1B);
     lcd.clear();
      lcd.display();
      lcd.print("Orchid");// print the results to the LCD Display:
      sensorValue = analogRead(waterSensorPin);// read the analog in value:
      sensorValue = map(sensorValue, 0, 195, 0, 100);
      lcd.setCursor(0, 1);
      lcd.print(sensorValue);
      lcd.print("% Enough water");
      Serial.println(sensorValue);
    }

  }
  if (whichPlant == noPlant) {
    digitalWrite(10, LOW);  // turn off previous LED
    digitalWrite(12, LOW);  // turn off previous LED
    lcd.clear();
    lcd.noDisplay();
    lcd.setRGB(zeroR, zeroG, zeroB);

  }
  // Report the moisture level of this plant.
  if (whichPlant != noPlant) {
    int sensorValue = analogRead(waterSensorPin);
    int position = map(sensorValue,
                       moistureMinima[whichPlant], moistureMaxima[whichPlant], 180, 0);
    indicator.write(constrain(position, 0, 180));
    Serial.println(sensorValue);
    delay(500);

  }
  // added 14-02 to make sure servo pointer goes back to start after reading last reading from waterSensor
  if (whichPlant == noPlant) {
    int sensorValue = analogRead(waterSensorPin);
    int position = map(sensorValue,
                       moistureMinima[whichPlant], moistureMaxima[whichPlant], 180, 0);
    indicator.write(constrain(position, 180, 180));
    delay(500);

  }





  //gemaakt door Sinan van Noppen
}

and if you have any suggestions or you have a go on making my code better, more DRY I would be honoured and appreciate it :)

EDIT: (just changed the place of the if whcihplant == 3 etc.)

#include <Servo.h>

#include <Wire.h>
#include "rgb_lcd.h"
rgb_lcd lcd;

const int numberOfPlants = 4;
const int noPlant = -1;  // index meaning no plant is being monitored

const int plant1R = 0;
const int plant1G = 0;
const int plant1B = 255;
const int zeroR = 0;
const int zeroG = 0;
const int zeroB = 0;
const int plant2R = 0;
const int plant2G = 255;
const int plant2B = 0;
const int plant3R = 255;
const int plant3G = 0;
const int plant3B = 0;
const int plant4R = 255;
const int plant4G = 0;
const int plant4B = 255;


// Pinout.
const int waterSensorPin = A1;
const int buttonPin = 2;
const int servoPin = 5;
const int ledPins[numberOfPlants] = {12, 11, 10, 9};

// Moisture ranges for the different plants.
const int moistureMinima[numberOfPlants] = {  0,   0,   0,   0};
const int moistureMaxima[numberOfPlants] = {195, 682, 750, 950};

// Servo holding the indicator needle.
Servo indicator;
int sensorValue = 0; // value read from the Soil Moisture
void setup() {
  lcd.begin(16, 2);
  Serial.begin(9600);
  pinMode(waterSensorPin, INPUT);
  pinMode(buttonPin, INPUT);
  for (int ledNo = 0; ledNo < numberOfPlants; ledNo++) {
    pinMode(ledPins[ledNo], OUTPUT);
  }
  indicator.attach(servoPin);
}

void loop() {
  // Which plant are we currently monitoring?
  static int whichPlant = noPlant;
  sensorValue = analogRead(waterSensorPin);// read the analog in value:


  // Switch to next plant on button press.
  if (digitalRead(buttonPin) == HIGH) {
    if (whichPlant != noPlant) {
      digitalWrite(ledPins[whichPlant], LOW);  // turn off previous LED
    }
    ++whichPlant;
    if (whichPlant == numberOfPlants) {
      whichPlant = noPlant;
    } else {
      digitalWrite(ledPins[whichPlant], HIGH);  // turn on current LED
    }


  }
  if (whichPlant == noPlant) {
    digitalWrite(10, LOW);  // turn off previous LED
    digitalWrite(12, LOW);  // turn off previous LED
    lcd.clear();
    lcd.noDisplay();
    lcd.setRGB(zeroR, zeroG, zeroB);

  }
    if (whichPlant == 3) {

      digitalWrite(10, HIGH);  // turn off previous LED
      digitalWrite(12, HIGH);  // turn off previous LED
      lcd.setRGB(plant4R, plant4G, plant4B);
      lcd.clear();
      lcd.display();
      lcd.print("Swamp Plant");// print the results to the LCD Display:
      sensorValue = map(sensorValue, 0, 950, 0, 100);
      lcd.setCursor(0, 1);
      lcd.print(sensorValue);
      lcd.print("% Enough water");
      Serial.println(sensorValue);
      delay(500);  // crude debouncing
    }
    if (whichPlant == 2) {
      lcd.setRGB(plant3R, plant3G, plant3B);
      lcd.clear();
      lcd.display();
      lcd.print("Orchid");// print the results to the LCD Display:
      sensorValue = map(sensorValue, 0, 750, 0, 100);
      lcd.setCursor(0, 1);
      lcd.print(sensorValue);
      lcd.print("% Enough water");
      Serial.println(sensorValue);
    }
    if (whichPlant == 1) {
      lcd.setRGB(plant2R, plant2G, plant2B);
      lcd.clear();
      lcd.display();
      lcd.print("Sunflower");// print the results to the LCD Display:
      sensorValue = map(sensorValue, 0, 682, 0, 100);
      lcd.setCursor(0, 1);
      lcd.print(sensorValue);
      lcd.print("% Enough water");
      Serial.println(sensorValue);
    }
    if (whichPlant == 0) {
      lcd.setRGB(plant1R, plant1G, plant1B);
      lcd.clear();
      lcd.display();
      lcd.print("Cactus");// print the results to the LCD Display:
      sensorValue = map(sensorValue, 0, 195, 0, 100);
      lcd.setCursor(0, 1);
      lcd.print(sensorValue);
      lcd.print("% Enough water");
      Serial.println(sensorValue);
    }

  // Report the moisture level of this plant.
  if (whichPlant != noPlant) {
    int sensorValue = analogRead(waterSensorPin);
    int position = map(sensorValue,
                       moistureMinima[whichPlant], moistureMaxima[whichPlant], 180, 0);
    indicator.write(constrain(position, 0, 180));
    Serial.println(sensorValue);
    delay(500);

  }
  // added 14-02 to make sure servo pointer goes back to start after reading last reading from waterSensor
  if (whichPlant == noPlant) {
    int sensorValue = analogRead(waterSensorPin);
    int position = map(sensorValue,
                       moistureMinima[whichPlant], moistureMaxima[whichPlant], 180, 0);
    indicator.write(constrain(position, 180, 180));
    delay(500);

  }





  //gemaakt door Sinan van Noppen
}
  • what does contentiously mean? ... do you mean continuously or continually? – jsotola Mar 19 at 0:26
  • you could use an array as a lookup table for data ... the four if (whichPlant == x) { could be replaced by one code block with data being retrieved from a lookup table plant1R, plant1G, plant1B, "Orchid", 195, "% Enough water" – jsotola Mar 19 at 0:43
  • look at this arduino.cc/en/Tutorial/Debounce ...... put the ++whichPlant; at the line in the tutorial where the LED is toggled – jsotola Mar 19 at 1:06
  • the code starting at if (whichPlant == 3) { should not be inside of the if (digitalRead(buttonPin) == HIGH) { block ...................your code should check the button at the begining of the loop() block and increment the whichPlant variable if required ......... the subsequent code should always run and it should react to the value of whichPlant only ... it should not react to any button presses, or be prevented from running by any button state – jsotola Mar 19 at 1:12
  • you need to make some helper functions to cut down on the repetition and make the code easier to maintain. it looks like one to handle the plant could accept parameters of a string label and an upper bound for the map. another function should be the only way print to the lcd, from one place, i call it render(). you want the loop function to be short and to mainly call sub-routines to handle the details. – dandavis Mar 19 at 4:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.