Array does not update with new input

Hey Everyone,

First off, thank you all and please excuse any dumb remarks. I’m still pretty new to all of this electronic wizardry but I’ve been trying to figure this bit out for the past solid week now. And I feel it may be some kind of declaration issue I’ve yet to come across.

I’m trying to make a plant watering system as my first project. The idea is to have a servo that moves a small hose to a position and have a motor power the water through the hose at ‘x’ position. There will be 5 positions for 5 plants. There are two arrays motordelay [how long the plant gets watered for] and servodelay [how long the servo stays in that position].

I have 5 LEDs to represent my 5 plants [I can toggle through these via ‘button’] and a red selector LED [which can be selected using ‘button2’]. So that if a plant is selected then the plant does not get watered and the motordelay will read ‘0’ for that plant.

The problem is that my if statements [from doiwaterit()] do not redefine the plant name I have in my motordelay array.

I’ve posted the code below. Again, thank you and sorry if any of this is too verbose.

#include <Servo.h> 

Servo myservo;  // create servo object to control a servo - a maximum of eight servo objects can be created 
int pos = 0;    // variable to store the servo position 
int motor = 11; // D11 = pin 11 = position 17
int angle = 36; // 36 deg. angle increments across 180 deg. e.g. 180/36=5 positions to stop at
int tranPin = 15; //Transister pin to regulate the 5V supply to the servo 

int spiderplant = 2000; // associated with LED1 so that if selectorLED is on while LED1 is on then the motordelay for spiderplant is 0 and gets no water
int motordelays[] = { 
  spiderplant,0,0,0,0}; //motordelay is to delay the motor while HIGH to supply the water to the plant - will add more plants...
int servodelays[] = {
  (spiderplant + 2000),(0 + 2000),(0 + 2000),(0 + 2000),(0 + 2000)}; //servodelay is to pause the servo at a position while the motor turns on to water the plant

int state=LOW;    //to toggle thru LEDs...
int lastState=LOW;    // ""
int count=0;    // ""
int remainder=0;    // ""
int lastPin=5;  // ""
int button=2;  // button to toggle through LEDs
int startpin=5; // the first LEDs pin position number 
int ledcount=5; // the total number of green LEDs to cycle through

int button2=1; //button for selectorled, pin 13
int button2reading;

int selectorLED=13; //a red LED used to select green plant LED - to determine which plants get watered

long debounce = 200; //debounce time used in the LED selection 
long time = 0;

int ledstate1; //will have 5 total LEDs each associated with their own plant
int LED1= 5; // ""

unsigned long TimeUntilNextWatering = 10000;
unsigned long lastTime = millis();



void setup() 
{ 
  myservo.attach(10);  // attaches the servo on pin 10 to the servo object, pin 10 = D10 = position 16
  pinMode(motor, OUTPUT);     // initialize the digital pin as an output.

  pinMode(LED1,OUTPUT);

  pinMode(button, INPUT);
  pinMode(button2, INPUT);

  state=digitalRead(button);
  digitalWrite(lastPin,HIGH);

  pinMode(selectorLED,OUTPUT);
  pinMode(tranPin, OUTPUT); 

} 


void loop() 
{ 
  
  if(millis() > (lastTime + (TimeUntilNextWatering))) {
    digitalWrite(tranPin, HIGH);
    theservo();
    digitalWrite(tranPin, LOW);
    lastTime = millis();
  }
  toggleleds();
  doiwaterit();
} 



  //----------------------------------------------------------------------------------


//This is to move the servo to 'x' position and turn on the motor for 'x' time to water the plants
void theservo() {

  for(pos = 0; pos < 180; pos += angle)  // goes from 0 degrees to 180 degrees in steps of 'x' degree 
  {                                   
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(servodelays[(pos/angle)]);                       // waits for 'x' time at that position
    digitalWrite(motor, HIGH);   // motor to HIGH to feed the plant
    delay(motordelays[(pos/angle)]); // delay for 'x' time determined by array
    digitalWrite(motor, LOW);    // motor to LOW to stop feeding
    delay(2000);   // delay for servo to move to next position
    Servo refresh();
  } 
}


  //----------------------------------------------------------------------------------


//This is used to toggle through the 5 LEDs that represent the 5 different plants
void toggleleds() {

  if (state==HIGH & lastState==LOW){               // The if then seqeunce for LED toggling (Note: the pins must be in sequence!!!)
    count=count+1;      // this could be re-written as just "count++;"
    remainder=count%ledcount;      // "%" is the symbol for modulo (remainder) division... so that 1/3=2, 2/3=2, 3/3=0, 4/3=1, 5/3=2, 6/3=0, 7/3=1
    digitalWrite(lastPin, LOW);
    digitalWrite(startpin + remainder, HIGH);
    lastPin= startpin + remainder;
  }
  lastState=state;      
  state=digitalRead(button);      
  delay(50);       // this delay is for debouncing



  //---------------------------------------------------------------------------------


//This is used with the selectorLED to select an LED - if selected the plant will not be watered
  if (digitalRead(LED1)==HIGH){       //LED1 is associated with spiderplant
    button2reading = digitalRead(button2);
    if (button2reading == HIGH && millis() - time > debounce) {
      if (ledstate1 == HIGH) {   // if the 'state' is TRUE to be HIGH then...
        ledstate1 = LOW;        // the 'state' is redefined to be LOW
      }
      else
        ledstate1 = HIGH;       // otherwise the 'state' is HIGH
      time = millis();    // the time in milliseconds is defined and recorded as 'time' to be used for the next loop where the "time > debounce"
    }
    digitalWrite(selectorLED, ledstate1); // the 'outpin', 13, is set to 'state' which is either HIGH or LOW based on the previous nested if then statement
  }

}


  //---------------------------------------------------------------------------------


//This is used to see if the selectorLED was used to select an LED via ledstate1
void doiwaterit() {
  
  if (ledstate1 == HIGH) {   // if the 'state' is TRUE to be HIGH then...
        spiderplant = 0;    // the motordelay for spiderplant=0 and will not be watered
  }
  if (ledstate1 == LOW) {
        spiderplant = 2000; // then it will be watered...
  }



}

You seem to think that the arrays, motordelays and servodelays are bound to the variable spiderplant. They are most certainly not. Changing the value of spiderplant after it is used to calculate the values to store in the arrays makes no difference to the values stored in the array. To do so would be like you being sent a bill if the price of gas goes up two weeks after you filled your car. You’d rightfully ignore the bill, as the arrays rightfully ignore changes to spiderplant. Which, by the way, is a dumb name for a variable that holds a time.

Ah! Thank you!

I had it in my head that this 'spiderplant' was a variable within the array and that whenever the code called for the array it would look at it separately each time instead of just once.

If this is the case, then is there any way to use the array within the main loop to continuously redefine the array with its changing variables?

Thanks!

If this is the case, then is there any way to use the array within the main loop to continuously redefine the array with its changing variables?

Of course you can assign new values to elements of the array. The new value can even be a based on the current contents of a variable.

void doiwaterit()
{
  if (ledstate1 == HIGH)
  {   // if the 'state' is TRUE to be HIGH then...
        spiderplant = 0;    // the motordelay for spiderplant=0 and will not be watered
  }
  if (ledstate1 == LOW) 
  {
        spiderplant = 2000; // then it will be watered...
  }

  motordelays[0] = spiderplant;
  servodelays[0] = spiderplant + 2000;
}

IT'S ALIVE!!!

Brilliant, thank you!

XD