Need some advice on my usage of if() and elapsed time.


hey all,

thanks reading - first want thanks, i've been reading forum since picked first arduino 2 months ago, , have been able solve every issue far. feel bad having question first post.

as stereotypical may - i'm building garduino. i'm hung on timing of water pump. have block of if statements meant control execution of code around water pump commands. highlighted code below it's evident i'm talking about...

what i'm trying accomplish this: evaluate sensor, if it's below value , either haven't watered plant in on half hour or it's first time program has run, water plant 5 seconds.

i'm except last part, water 5 seconds. think i'm stuck , not thinking how code executes, , how use millis() or library accomplish me without using delay. i've "mastered" not using delay elsewhere, reason "do x seconds" tripping me up. times find writing code, when problem kind of "corner in", , need nudge in right direction. hoping has idea here, or example point me to.

i've tried modifying blink without delay, elapsedmillis library, , using assortment of millis()'s try , control elapsed time need help.

if curious, i'm using mega 2560, cheap seeed tft graph temp, water , light, , seeed sd reader log data. it's mess of wires , i'm hoping tidied once figure out water pump issue! (note: tried posting entire code - long so. i'll try posting entire code in next comment)



your time appreciated!
-justin



quote



    if (lightval1<600 ){

      if(lastwater==0 || currentmillis-lastwater>waterinterval){

        if(timeelapsed>waterpumpinterval){
          pumpstate = !pumpstate;
          digitalwrite(waterpump,pumpstate);
          timeelapsed=0;
          //lastwater=currentmillis;
        }


      } 
    }
    else{
      digitalwrite(waterpump,low);
    }  


quote

#include <spi.h>

#include <elapsedmillis.h>

#include <tft.h>
#include <sd.h>
#include <stdint.h>
#include <touchscreen.h> 
#include <math.h>

#ifdef seeeduino
#define yp a2   // must analog pin, use "an" notation!
#define xm a1   // must analog pin, use "an" notation!
#define ym a0   // can digital pin, a0
#define xp a3   // can digital pin, a3
#endif

#ifdef mega
#define yp a2   // must analog pin, use "an" notation!
#define xm a1   // must analog pin, use "an" notation!
#define ym a0   // can digital pin, a0
#define xp a3   // can digital pin, a3
#endif 

//chipselect for sd reader
const int chipselect=53;

int ypos=131;
int therm1=0;
char therm2[5];
int lightmeter=a0;
int light=0;
int lightval1=0;
char lightval2[5];
int waterpump=32;

long previousgraph=0;
long graphinterval=300000;

long previoussensorread=0;
long sensorinterval=5000;

long previouswriteclear=0;
long clearinterval=4999;

long waterpumpcounter=0;
long waterpumpinterval=5000;    //amount of time turn water on for
long lastwater=0;              //store last watering time
long waterinterval=30000;     //time between waterings, not water if last water less int
boolean pumpstate=low;
long previousmillis=0;
elapsedmillis timeelapsed;
elapsedmillis timeelapsed1;


enum {
  t_kelvin=0,
  t_celsius,
  t_fahrenheit
};
float temperature(int analoginputnumber,int outputunit,float b,float t0,float r0,float r_balance)
{
  float r,t;

  //  r=1024.0f*r_balance/float(analogread(analoginputnumber)))-r_balance;
  r=r_balance*(1024.0f/float(analogread(analoginputnumber))-1);

  t=1.0f/(1.0f/t0+(1.0f/b)*log(r/r0));

  switch(outputunit) {
  case t_celsius :
    t-=273.15f;
    break;
  case t_fahrenheit :
    t=9.0f*(t-273.15f)/5.0f+32.0f;
    break;
  default:
    break;
  };

  return t;
}

void setup()
{
  pinmode(53, output);
  pinmode(30,output);
  pinmode(31,output);
  pinmode(waterpump,output);
  digitalwrite(waterpump,low);

  if (!sd.begin(chipselect)) {
    digitalwrite(31,high);
    delay(500);
    digitalwrite(31,low);
    //return;
  }

  //serial.begin(9600);

  tft.init();  //init tft library

  tft.fillrectangle(0,0,240,320,white);
  tft.setdisplaydirect(up2down);

  tft.drawstring("light:",230,20,2,black);
  //tft.drawstring("780",200,25,3,red);
  tft.drawhorizontalline(170,130,65,black);
  tft.drawverticalline(170,130,185,black);

  tft.drawstring("temp:",153,20,2,black);
  //tft.drawstring("70",123,25,3,red);
  tft.drawhorizontalline(93,130,65,black);
  tft.drawverticalline(93,130,185,black);


  tft.drawstring("water:",78,20,2,black);
  tft.drawstring("400",48,25,3,gray2);
  tft.drawhorizontalline(18,130,65,black);
  tft.drawverticalline(18,130,185,black);

  tft.drawstring("scale @ 10 min",10,155,1,black);


}

void loop()
{
  string datastring="";

  unsigned long currentmillis=millis();

  if(currentmillis-previoussensorread > sensorinterval) {
    previouswriteclear=currentmillis;


    tft.drawstring(therm2,123,25,3,white);
    tft.drawstring(lightval2,200,25,3,white);

  }

  if(currentmillis-previoussensorread > sensorinterval) {
    previoussensorread=currentmillis;

    //read thermistor temp
    therm1=temperature(a2,t_fahrenheit,3950.0f,298.15f,4700.0f,4700.0f);
    itoa(therm1,therm2,10);
    datastring+=therm2;
    datastring+=",";


    //read photoresistor light
    lightval1=analogread(lightmeter);
    itoa(lightval1,lightval2,10);
    datastring+=lightval2;
    //datastring+=",";


    //print light & temp
    tft.drawstring(therm2,123,25,3,0x0731);
    tft.drawstring(lightval2,200,25,3,0x0731);


    if (lightval1<600 ){

      if(lastwater==0 || currentmillis-lastwater>waterinterval){

        if(timeelapsed>waterpumpinterval){
          pumpstate = !pumpstate;
          digitalwrite(waterpump,pumpstate);
          timeelapsed=0;
          //lastwater=currentmillis;
        }

      } 
    }
    else{
      digitalwrite(waterpump,low);
    }  



    file datafile = sd.open("testa.txt", file_write);
    if (datafile) {
      digitalwrite(30,high); 
      datafile.println(datastring);
      datafile.close();
      digitalwrite(30,low);
    }   
    else{
      digitalwrite(31,high);
    }



  }


  //do graphing every x (based on counter)

  if(currentmillis-previousgraph > graphinterval) {
    previousgraph=currentmillis;

    //populate light graph
    light=analogread(a0);
    int maplight=map(light,0,1023,0,65);
    tft.fillrectangle(171,ypos,maplight,3,0x032d);

    //populate temp graph
    therm1=temperature(a2,t_fahrenheit,3950.0f,298.15f,4700.0f,4700.0f);
    int maptherm = map(therm1,30,100,0,65);
    tft.fillrectangle(94,ypos,maptherm,3,0x0731); 


    //move bar down screen
    if (ypos >= 310) {
      tft.fillrectangle(171,131,66,186,white);
      tft.fillrectangle(94,131,66,186,white);
      ypos = 131;
      //delay(100);
    } 
    else {
      // increment horizontal position:
      ypos=ypos+2;
    }
  }


}






Arduino Forum > Using Arduino > Programming Questions > Need some advice on my usage of if() and elapsed time.


arduino

Comments

Popular posts from this blog

VIDIOC_S_FMT error 16, Device or resource busy - Raspberry Pi Forums

using a laptop skeleton to build a pi laptop - Raspberry Pi Forums

Forum for Joomla? - Joomla! Forum - community, help and support