Last spot in an array not workings as intended


hello, have trouble code. code plant watering system. work having pots 1 plant in each, in each pot there humidity sensor. idea can decide how water each plant needs , arduino water when needed.
you change settings on small standard lcd(16x2) using 3 buttons:

*nastaplanta-means next plant. simple counter going 0 3 , 3 directly 0. with    decide plant's setting appear on screen.
*mervatten-means more water. array myplanta keeps track of plant's settings. happens when press button. myplanta[plantcounter]++. goes 0 10.
*mindrevatten-means less water. above decreases myplanta[plantcounter].

now on problem. when code runs , press nastaplanta button, works fine
(you can increase , decrease water setting in array myplanta[]) untill plantcounter
becomes 3. reason, myplanta[plantcounter] when plantcounter==3, myplanta[3]=3. (it supposed 0). if plantcounter==3 , press button mervatten(more water button) plantcounter increases 4(should go 0 3). when has happend on myplanta[4]
and here can change water setting no problem, , go plantcounter==0. if press mindrevatten(less water) button when plantcounter==3, plantcounter decreases 2(plantcounter==)  , again can change water settings without problems.

i have tested code on 2 different arduinos, 1 uno , 1 mega(non android version)
i have not been able find problem. code commented in swedish(and english), if have questions please ask.

code: [select]

#include <liquidcrystal.h>
#include <button.h>

//analog in på pin 0,1,2,3,4,5,6 /0-3 vatten, 4-5 foto, 6 vatten tank
//knappar på 22-24
//relä och pump på 25-29
liquidcrystal lcd(12,11,5,4,3,2); //integlömma begin loop

int myplanta[3];
int plantcounter;
int myplantafukt[3];
int myplantafoto[1];
unsigned long timer, lasttimer;
double fotomedel;
string mystringone; //strings till skärmen
string mystringtwoa;
string mystringtwob;

button nasta = button(22,pullup); //kollar knapparna, returener true om knappen trycks ner
button mer = button(23,pullup);   //tack vare button button.h
button mindre = button(24,pullup);

void kollafukt(){
  for(int i=0;i<3;i++){  //läser in
    myplantafukt[i] = analogread(i);
  }
  for(int i=0;i<1;i++){
    myplantafoto[i] = analogread(i+4);
  }
  for(int i=0;i<3;i++){
    if (myplantafukt[i]<myplanta[i]){  //ignore
      vattna(i); //vattnar
    }
  }
}

int vattna(int n){
  double fotomedel = (myplantafoto[0] + myplantafoto[1])/2;
  double tempfukt = myplantafukt[n];
  if (fotomedel > 500){
    tempfukt=1.1*myplantafukt[n];
  } //500 till vi har ett bättre värde
  map(tempfukt,1023,0,1,10); //gör om värde mellan 0-1023 till mellan 0 och 10
  if (tempfukt<myplanta[n] & kollatank()){
    double tiddelay=((myplanta[n]-tempfukt)*200)+100; //hur länge den ska vattna watering time
    digitalwrite((25+n),high);
    digitalwrite(29,high);
    //string till skärmen som säger "arbetar"
    lcd.clear();
    lcd.setcursor(0,0); 
    lcd.print("arbetar..."); //"working"
    delay(tiddelay);
    texttillskarm();
    //string som återställer skärmen
    digitalwrite(29,low);   
    digitalwrite((25+n),low);


  }
}

void kollavatten(){
  if (millis() >= lasttimer+3000000){ //körs om det var mer än 5 minuter sen sist
    lasttimer=millis();
    kollafukt();
  }
}

void nastaplanta(){  //bytar planta när man trycker på knappen  changes plant
  if (nasta.ispressed()){
    if (plantcounter < 3){
      plantcounter++;
    }
    else if (plantcounter>=3){
      plantcounter=0;
    }
    texttillskarm();
    delay(50);
  }
}

void mervatten(){ //ökar plantans variabel för vatten  increases water setting
  if (mer.ispressed()){
    if (myplanta[plantcounter] < 10){
      myplanta[plantcounter]++;
    }
    else if (myplanta[plantcounter] >= 10){
      myplanta[plantcounter]=10;
    }
    texttillskarm();
    delay(50);   
  }
}

void mindrevatten(){ //som ovan fast minskar decreases water setting
  if (mindre.ispressed()){
    if (myplanta[plantcounter] > 0){
      myplanta[plantcounter]--;
    }
    else if (myplanta[plantcounter] >= 0){
      myplanta[plantcounter]=0;
    }
    texttillskarm();
    delay(50);
  }
}

void texttillskarm(){
  //mystringone = string(mystringone + (plantcounter+1)); //första raden ignore now
  //mystringtwoa = string(myplanta[plantcounter]); //andra raden första halva
  //mystringtwob = string(map(myplantafukt[plantcounter],1023,0,0,100)+"%");
  lcd.clear(); //skriver ut allt
  lcd.setcursor(0,0);
  lcd.print("planta nr:");
  lcd.setcursor(11,0);
  lcd.print(plantcounter+1);
  lcd.setcursor(0,1);
  lcd.print(myplanta[plantcounter]);
  //lcd.setcursor(8,1);
  //lcd.print(mystringtwob);
  serial.print("planta nr:");
  serial.println(plantcounter+1);
  serial.print("inställning:"); //"setting"
  serial.println(myplanta[plantcounter]);
}

boolean kollatank(){ //kollar vatten tanken (checks there water in tank)
  if (analogread(6)>1000){
    return 0;
  }
  else {
    return 1;
  }
}

void setup(){
  pinmode(13,output);//debug
  pinmode(25,output);//25-28 relaypins
  pinmode(26,output);
  pinmode(27,output);
  pinmode(28,output);
  pinmode(29,output);//pump

  lcd.begin(16,2); //initierar skärmen
  serial.begin(9600);
}

void loop(){
  nastaplanta();
  mervatten();
  mindrevatten();
  kollavatten();
  texttillskarm();  //sends text lcd.
}


hi wildiness

code: [select]
int myplanta[3];

your array has 3 elements, accessed myplanta[0], myplanta[1] , myplanta[2]

you must not let index 3, seems happen later in code.

regards

ray


Arduino Forum > Using Arduino > Programming Questions > Last spot in an array not workings as intended


arduino

Comments

Popular posts from this blog

invalid use of void expresion in FlexiTimer2 library

error: a function-definition is not allowed here before '{' token

LED Strip Code