Button and Clock issues


so making cactus babysitter few things.

1. leaves fluorescent light on hour hour. uses realtime clock , relay.

2. pumps water vase watering system every sunday @ noon calculated amount of time rough amount of milliliters per plant needed. uses realtime clock, pump , relay.

3. has 2 rgb lights vary depending on desired temp. (both green if good, 1 or 2 blue if cold, , 1 or 2 red if hot)

4. has lcd screen display variables (time light on, time light off, milliliters, , desired temp)

5. has 4 buttons used change these variables on go (time light on, time light off, milliliters, , desired temp)


so works right except 2 things.

• buttons not change variables should. if press in button, turns off (lcd, leds, arduino leds, relay leds) release button , code resets. it's bizarre , don't know what's wrong. believe wiring right wrong. wiring complicated wanted avoid having post if obvious coding issue.

• clock randomly resets 1/1/2000 -- 00:00:00. last night had big light working @ 1am, programmed turn on @ 1am, @ 7:20am light turned on again, meaning clock reset hour earlier this. checked code morning , sure enough clock reset few hours ago. i'm not sure why going on either. 2 problems may related.


any insight guys provide awesome.


code: [select]
#include <wire.h>
#include <ds3231.h>
#include <liquidcrystal_i2c.h>

//initialize ds3231 clock using hardware interface
ds3231  rtc(sda, scl);

//set lcd address 0x27 20 char , 4 line display
liquidcrystal_i2c lcd(0x27, 20, 4);

//pin setup
const int templed1redpin = 3;
const int templed1greenpin = 5;
const int templed1bluepin = 6;
const int templed2redpin = 9;
const int templed2greenpin = 10;
const int templed2bluepin = 11;
const int relaypin = 7;
const int pumppin = 8;
const int timeonbuttonpin = 13;
const int timeoffbuttonpin = 2;
const int waterbuttonpin = 4;
const int tempbuttonpin = 12;


void setup(){
  
  //initialize rtc
  rtc.begin();
  
  //the following lines can uncommented set date , time
  //rtc.setdow(thursday);      // set day-of-week wednesday
  //rtc.settime(0, 11, 0);     // set time 12:00:00 (24hr format)
  //rtc.setdate(3, 8, 2017);   // set date january 1st, 2014
  
  //initialize lcd
  lcd.init();
  
  //open backlight
  lcd.backlight();
  
  //setup serial connection @ 115200 baud
  serial.begin(115200);
  
  while(!serial){
    //wait serial port connect
  }
 
  //setting inputs
  pinmode(timeonbuttonpin, input);
  pinmode(timeoffbuttonpin, input);
  pinmode(waterbuttonpin, input);
  pinmode(tempbuttonpin, input);
 
  //setting outputs
  pinmode(templed1redpin, output);
  pinmode(templed1greenpin, output);
  pinmode(templed1bluepin, output);
  pinmode(templed2redpin, output);
  pinmode(templed2greenpin, output);
  pinmode(templed2bluepin, output);
  pinmode(relaypin, output);
  pinmode(pumppin, output);

}


void loop(){
  
  //defining variables
  int lighthouron = 1;
  int lighthouroff = 22;
  boolean lightson = false;
 
  //these variables don't change buttons may have change them here.
  string wateringday = "sunday";
  int wateringhour = 12;
  
  int milliliters = 0;
  int watertime = milliliters*500; //this desired ml / 6ml/sec flow * 3 pots * 1000ms/sec
  boolean gavewater = false;
  
  float desiredtemp = 75;
  float tempc = 0;
  float tempf = 0;
  
  //infinite checks
  while(true){
    
    //getting day , hour strings
    string currentday = rtc.getdowstr();
    string currenttimestring = rtc.gettimestr();
    string currenthourstring = currenttimestring;
    currenthourstring.remove(2);
    
    //converting hour strings int
    int currenthour = currenthourstring.toint();
    
    //getting temp in celsius
    tempc = rtc.gettemp();
    
    //converting fahrenheit
    tempf = (tempc * 9/5) + 32;
    
    //starting button statuses
    int timeonbuttoncurrentstate = 0;    
    int timeonbuttonlaststate = 0;
    int timeoffbuttoncurrentstate = 0;    
    int timeoffbuttonlaststate = 0;
    int waterbuttoncurrentstate = 0;    
    int waterbuttonlaststate = 0;
    int tempbuttoncurrentstate = 0;    
    int tempbuttonlaststate = 0;
    
    //reading timeonbutton
    timeonbuttoncurrentstate = digitalread(timeonbuttonpin);
    //if button pushed in , different last read
    if(timeonbuttoncurrentstate != timeonbuttonlaststate && timeonbuttoncurrentstate == low){
      //up time 1 hour or loop back
      if(lighthouron != 23){
        lighthouron = lighthouron + 1;
      }
      else{
         lighthouron = 0;
      }
    }
    //set our state our last state next check
    timeonbuttonlaststate = timeonbuttoncurrentstate;
    
    //reading timeoffbutton
    timeoffbuttoncurrentstate = digitalread(timeoffbuttonpin);
    //if button pushed in , different last read
    if(timeoffbuttoncurrentstate != timeoffbuttonlaststate && timeoffbuttoncurrentstate == low){
      //up time 1 hour or loop back
      if(lighthouroff != 23){
        lighthouroff = lighthouroff + 1;
      }
      else{
         lighthouroff = 0;
      }
    }
    //set our state our last state next check
    timeoffbuttonlaststate = timeoffbuttoncurrentstate;
    
    //reading waterbutton
    waterbuttoncurrentstate = digitalread(waterbuttonpin);
    //if button pushed in , different last read
    if(waterbuttoncurrentstate != waterbuttonlaststate && waterbuttoncurrentstate == low){
      //up water level 10ml
      if(milliliters != 200){
        milliliters = milliliters + 10;
      }
      else{
         milliliters = 0;
      }
    }
    //set our state our last state next check
    waterbuttonlaststate = waterbuttoncurrentstate;
    
    //reading tempbutton
    tempbuttoncurrentstate = digitalread(tempbuttonpin);
    //if button pushed in , different last read
    if(tempbuttoncurrentstate != tempbuttonlaststate && tempbuttoncurrentstate == low){
      //up temp 5º
      if(desiredtemp != 100){
        desiredtemp = desiredtemp + 5;
      }
      else{
         desiredtemp = 50;
      }
    }
    //set our state our last state next check
    tempbuttonlaststate = tempbuttoncurrentstate;
    
    //checking lights
    if(currenthour == lighthouron && lightson == false){
      digitalwrite(relaypin, high);
      lightson = true;
    }
    
    //resets lights when lighthouroff comes
    else if(currenthour == lighthouroff && lightson == true){
      digitalwrite(relaypin, low);
      lightson = false;
    }
    
    //checking water
    if(currentday == wateringday && currenthour == wateringhour && gavewater == false){
      digitalwrite(pumppin, high);
      //water time calculated based on input ml (ratio specific pump system)
      delay(watertime);
      digitalwrite(pumppin, low);
      gavewater = true;
    }
    
    //resets watering boolean next hour
    else if(currenthour != wateringhour && gavewater == true){
      gavewater = false;
    }
    
    //checking if temperature in tolerable 5º range
    if(tempf >= (desiredtemp-5.0) && tempf < (desiredtemp+5.0)){
      //setting 2 green leds
      setcolor1(0, 0, 100);
      setcolor2(0, 0, 100);
    }
    
    //checking if temperature 5-10º colder desired
    else if(tempf > desiredtemp-10 && tempf <= desiredtemp-5){
      //setting 1 blue , 1 green led
      setcolor1(150, 0, 0);
      setcolor2(0, 0, 100);
    }
    
    //checking if temperature 5-10º hotter desired
    else if(tempf < desiredtemp+10 && tempf >= desiredtemp+5){
      //setting 1 green , 1 red led
      setcolor1(0, 0, 100);
      setcolor2(0, 255, 0);
    }

    //checking if temperature 10º colder desired or more
    else if(tempf <= desiredtemp-10){
      //setting 2 blue leds
      setcolor1(150, 0, 0);
      setcolor2(150, 0, 0);
    }
    
    //checking if temperature 10º hotter desired or more
    else if(tempf >= desiredtemp+10){
      //setting 2 red leds
      setcolor1(0, 255, 0);
      setcolor2(0, 255, 0);
    }
  
  
  }
  
}
 

 

***note*** removed debugging serial code, rgb code, , lcd code able fit in 1 post.

also looks rgb values wrong wired them wrong gives correct colors.


Arduino Forum > Using Arduino > Programming Questions > Button and Clock issues


arduino

Comments

Popular posts from this blog

Flip address is out of range arduino uno r3

Arduino Uno not uploading

Indesign and MathType fonts