EcoSphere Mark II Arduino Code

Forum dedicated to sharing project files such as templates, code, models, etc
User avatar
Josh Hess
Site Admin
Posts: 128
Joined: Sun May 24, 2015 6:00 pm
Location: Orlando, FL
Areas of Knowledge: Electronics, Product Design, Manufacturing
School/University: UCF

EcoSphere Mark II Arduino Code

Postby Josh Hess » Mon Apr 04, 2016 11:52 pm

Here is the current code for the EcoSphere arduino:

Code: Select all

#include <LiquidCrystal.h>
#include "Wire.h"
#include <RTClib.h>
#define DS1307_ADDRESS 0x68
#define PUSH_BUTTON 7
#define LED_PIN 10
#define TEMPS 3 // number of temp sensors

//Define Variables
double M,Y,D,MN,H,S;
double A,B,C,E,F;
double cA,cB,cC,cE,cF;
const int temps[TEMPS] = {0, 1, 3}; //  WATER{0}, AMBIENT{1}, LED{3}
int buttonState = LOW;
long days = 0;
int cMenu = 0;
int second = 0;
int minute = 0;
int hour = 0;
int weekDay = 0;
int monthDay = 0;
int month = 0;
int year = 0;

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

bool checkButton() {
    buttonState = digitalRead(PUSH_BUTTON);
    if (buttonState == HIGH) {
      cMenu++;
      cMenu %= 2;
      return true;
    }
  delay(100);
  return false;
}

float getVoltage(int pin) {
  return analogRead(pin) * 0.004882814;
}

void setup() {
  pinMode(PUSH_BUTTON, INPUT);
  lcd.begin(16, 2);
  pinMode(LED_PIN, OUTPUT);
  analogWrite(LED_PIN, 255);
  Wire.begin();
  Serial.begin(9600);
}

void loop() {
  float voltage, degC[TEMPS], degF[TEMPS];
  for (int pos = 0; pos < TEMPS; pos++) {
    voltage = getVoltage(temps[pos]);
    degC[pos] = degF[pos] = 0;
    degC[pos] = (voltage - 0.5) * 100.0;
    degF[pos] = degC[pos] * (9.0/5.0) + 32.0;
  }
 
  switch (cMenu) {
    start:
    case 0:
      menu0();
      break;

    case 1:
      lcd.clear();
      delay(150);
      lcd.setCursor(2,0);
      lcd.print("Temperature: ");
      for (int pos = 0; pos < TEMPS; pos++)
      {
        refreshLCD(0,1);
        tempPrinter(degF[pos], pos);
        for (int d = 0; d < 10; d++)
        {
          timeCheck();
          delay(100);
          if (checkButton()) {goto start;}
        }
      }
      break;
  }
}

void menu0() {
  lcd.clear();
  delay(150);
  lcd.setCursor(0,0);
  lcd.print("EcoSphere MRK II");
  lcd.setCursor(5, 1);
  lcd.print("Day:");

  for (;;) {
    refreshLCD(9, 1);
    lcd.setCursor(9,1);
    timeCheck();
    dayCount();
    lcd.print(days);
    delay(100);
    if (checkButton()) break;
  }
}

void refreshLCD(int x, int y) {
  lcd.setCursor(x,y);
  lcd.print("               ");
}

void tempPrinter(float deg, int pos) {
  switch (pos) {
    case 0:
      lcd.setCursor(2,1);
      lcd.print("Water: ");
      lcd.setCursor(9,1);
      break;
     
    case 1:
      lcd.setCursor(1,1);
      lcd.print("Ambient: ");
      lcd.setCursor(10,1);
      break;
     
    case 2:
      lcd.setCursor(3,1);
      lcd.print("LED: ");
      lcd.setCursor(8,1);
      break;
  }
  lcd.print(deg);
}

byte bcdToDec(byte val)  {
// Convert binary coded decimal to normal decimal numbers
  return ( (val/16*10) + (val%16) );
}
//Check time
void timeCheck(){

  // Reset the register pointer
  Wire.beginTransmission(DS1307_ADDRESS);

  byte zero = 0x00;
  Wire.write(zero);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_ADDRESS, 7);

  second = bcdToDec(Wire.read());
  minute = bcdToDec(Wire.read());
  hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
  weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
  monthDay = bcdToDec(Wire.read());
  month = bcdToDec(Wire.read());
  year = bcdToDec(Wire.read());
}

void dayCount() {
  //fill integers with start date
  M =  3; //Month
  Y = 16; //Year
  D = 12; //Month Day
  MN = 1; //Minute
  H = 1; //Hour
  S = 1; //Second
 
  if (M<3) {M+=12; Y-=1;}
 
  A = (long)(Y+2000)/100;
  B = (long)A/4;
  C = (long)2-A+B;
  E = (long)(365.25*(Y+4716));
  F = (long)(30.6001*(M+1));
  long StartJDN = C+D+E+F-1524.5;
  //calculate terms required for LST calcuation and calculate GMST using an approximation
  long Start_d = StartJDN - 2451545.0;
  long Start_T = Start_d / 36525;
 
  if (M<3) {M+=12; Y-=1;}
 
  cA = (long)(year+2000)/100;
  cB = (long)cA/4;
  cC = (long)2-cA+cB;
  cE = (long)(365.25*(year+4716));
  cF = (long)(30.6001*(month+1));
  long CurrentJDN = cC+monthDay+cE+cF-1524.5;
  //calculate terms required for LST calcuation and calculate GMST using an approximation
  long Current_d = CurrentJDN - 2451545.0;
 
  days = (CurrentJDN - StartJDN);
}


And Code to Set the Clock:

Code: Select all

//Arduino 1.0+ Only
//Arduino 1.0+ Only

#include "Wire.h"
#define DS1307_ADDRESS 0x68
byte zero = 0x00; //workaround for issue #527


void setup(){
  Wire.begin();
  Serial.begin(9600);
  setDateTime(); //MUST CONFIGURE IN FUNCTION
}

void loop(){
  printDate();
  delay(1000);
}

void setDateTime(){

  byte second =      10; //0-59
  byte minute =      57; //0-59
  byte hour =        18; //0-23
  byte weekDay =     6; //1-7
  byte monthDay =    19; //1-31
  byte month =       3; //1-12
  byte year  =       16; //0-99

  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero);

  Wire.write(decToBcd(second));
  Wire.write(decToBcd(minute));
  Wire.write(decToBcd(hour));
  Wire.write(decToBcd(weekDay));
  Wire.write(decToBcd(monthDay));
  Wire.write(decToBcd(month));
  Wire.write(decToBcd(year));

  Wire.write(zero); //start

  Wire.endTransmission();

}

byte decToBcd(byte val){
// Convert normal decimal numbers to binary coded decimal
  return ( (val/10*16) + (val%10) );
}

byte bcdToDec(byte val)  {
// Convert binary coded decimal to normal decimal numbers
  return ( (val/16*10) + (val%16) );
}

void printDate(){

  // Reset the register pointer
  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_ADDRESS, 7);

  int second = bcdToDec(Wire.read());
  int minute = bcdToDec(Wire.read());
  int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
  int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
  int monthDay = bcdToDec(Wire.read());
  int month = bcdToDec(Wire.read());
  int year = bcdToDec(Wire.read());

  //print the date EG   3/1/11 23:59:59
  Serial.print(month);
  Serial.print("/");
  Serial.print(monthDay);
  Serial.print("/");
  Serial.print(year);
  Serial.print(" ");
  Serial.print(hour);
  Serial.print(":");
  Serial.print(minute);
  Serial.print(":");
  Serial.println(second);

}


Final Code with dimmer support will be posted as soon as I complete it. Also a schematic will be included for wiring the arduino and corresponding electronics.
Science may set limits to knowledge, but should not set limits to imagination.

Return to “Project Files”

Who is online

Users browsing this forum: No registered users and 0 guests