Saturday, May 7, 2011

Final Project: DoseCalc 2000

Wiring Diagram of the DoseCalc 2000

Photograph of the DoseCalc 2000 Functional Prototype

Functional Block Diagram of the LCD screens within the DoseCalc 2000.

Code to create time and dose history:


#include "Wire.h"
#define DS1307_ADDRESS 0x68

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

float BG = 0;
float target = 100;
float a = 0;
float correctionfactor = 30;
float dose1 = 0;

float carbs = 0;
float insulintocarbratio = 10;
float dose2 = 0;
float dose = 0;

float history1 = 0;
float history2 = 0;
float history3 = 0;
float history4 = 0;
float history5 = 0;

int month1 = 0;
int month2 = 0;
int month3 = 0;
int month4 = 0;
int month5 = 0;

int monthDay1, monthDay2, monthDay3, monthDay4, monthDay5;
int hour1, hour2, hour3, hour4, hour5;
int minute1, minute2, minute3, minute4, minute5;

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


void loop(){
 
 
  Serial.println("Enter Blood Glucose"); 
 
  BG=0;
  while (Serial.available()==0){}
  while (Serial.available()>0) {
    while (Serial.available()>0) {
      BG=BG*10;
      a = Serial.read()-48;
      BG=BG+a;
      dose1= ((BG-target)/correctionfactor); }
    delay(5); }
   

 Serial.println("Enter Carbs");


 
  carbs=0;
  while (Serial.available()==0){}
  while (Serial.available()>0) {
    while (Serial.available()>0) {
      carbs=carbs*10;
      a = Serial.read()-48;
      carbs=carbs+a;
      dose2= (carbs/insulintocarbratio); }
    delay(5); }
   

dose = dose1 + dose2;

  Serial.print("The total dose is ");
  Serial.println(dose);
 
  history5 = history4;
  history4 = history3;
  history3 = history2;
  history2 = history1;
  history1 = dose;
 
  month5 = month4;
  month4 = month3;
  month3 = month2;
  month2 = month1;
  month1 = month;
 
  monthDay5 = monthDay4;
  monthDay4 = monthDay3;
  monthDay3 = monthDay2;
  monthDay2 = monthDay1;
  monthDay1 = monthDay;
 
  hour5 = hour4;
  hour4 = hour3;
  hour3 = hour2;
  hour2 = hour1;
  hour1 = hour;
 
  minute5 = minute4;
  minute4 = minute3;
  minute3 = minute2;
  minute2 = minute1;
  minute1 = minute;
 
  Serial.println(" ");
  Serial.println("Dose History");
  Serial.print(history1);
  Serial.print(" units at ");
  printDate();

  Serial.print(hour1);
  Serial.print(":");
  Serial.print(minute1);
  Serial.print(" on ");
  Serial.print(month1);
  Serial.print("/");
  Serial.println(monthDay1);
 
 
  Serial.print(history2);
  Serial.print(" units at ");
 
  Serial.print(hour2);
  Serial.print(":");
  Serial.print(minute2);
  Serial.print(" on ");
  Serial.print(month2);
  Serial.print("/");
  Serial.println(monthDay2);
 
 // Serial.println("time");
  Serial.print(history3);
  Serial.print(" units at ");
 
  Serial.print(hour3);
  Serial.print(":");
  Serial.print(minute3);
  Serial.print(" on ");
  Serial.print(month3);
  Serial.print("/");
  Serial.println(monthDay3);
 
//  Serial.println("time");
  Serial.print(history4);
  Serial.print(" units at ");
 
  Serial.print(hour4);
  Serial.print(":");
  Serial.print(minute4);
  Serial.print(" on ");
  Serial.print(month4);
  Serial.print("/");
  Serial.println(monthDay4);
 
//  Serial.println("time");
  Serial.print(history5);
  Serial.print(" units at ");
 
  Serial.print(hour5);
  Serial.print(":");
  Serial.print(minute5);
  Serial.print(" on ");
  Serial.print(month5);
  Serial.print("/");
  Serial.println(monthDay5);
 
//  Serial.println("time");
  Serial.println(" ");
 
}
 


void setDateTime(){

  byte second =      45; //0-59
  byte minute =      40; //0-59
  byte hour =        0; //0-23
  byte weekDay =     2; //1-7
  byte monthDay =    1; //1-31
  byte month =       3; //1-12
  byte year  =       11; //0-99

  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.send(0); //stop Oscillator

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

  Wire.send(0); //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.send(0);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_ADDRESS, 7);

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

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




}







Code for LCD Device:


#include "LCD_driver.h"

//#include "nokia_tester.h"
//    Included files
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include "WProgram.h"
#include "HardwareSerial.h"

#include "Wire.h"
#define DS1307_ADDRESS 0x68

int hour, minute, month, monthDay;

void setup() {
  Wire.begin();
  ioinit();           //Initialize I/O
  LCDInit();        //Initialize the LCD
 
        LCDContrast(44);
        LCDClear(BLACK);   
        LCDPutStr("DoseCalc", 0, 4, GREEN, BLACK);
        LCDPutStr("Home > ", 2, 80, WHITE, BLACK);
        LCDPutStr("2000", 16, 4, GREEN, BLACK);
        LCDPutStr("History > ", 32, 56, WHITE, BLACK);
        LCDPutStr("New Dose > ", 62, 48, WHITE, BLACK);
        LCDPutStr("Diabetes Dose", 78, 4, GREEN, BLACK);
        LCDPutStr("Calculator", 94, 4, GREEN, BLACK);
        LCDPutStr("Home Screen", 110, 20, ORANGE, BLACK); 
 //setDateTime(); //MUST CONFIGURE IN FUNCTION      
Serial.begin(9600);

}


int num = 1;
int s1, s2, s3;

int BG = 0;
int target = 100;
int a = 0;
int correctionfactor = 30;
int dose1 = 0;
int carbs = 0;
int insulintocarbratio = 10;
int dose2 = 0;
int dose = 0;

int history1 = 0;
int history2 = 0;
int history3 = 0;
int history4 = 0;
int history5 = 0;

int month1, month2, month3, month4, month5;
int day1, day2, day3, day4, day5;
int hour1, hour2, hour3, hour4, hour5;
int minute1, minute2, minute3, minute4, minute5;

void setDateTime(){

  byte second =      45; //0-59
  byte minute =      26; //0-59
  byte hour =        12; //0-23
  byte weekDay =     4; //1-7
  byte monthDay =    28; //1-31
  byte month =       4; //1-12
  byte year  =       11; //0-99

  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.send(0); //stop Oscillator

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

  Wire.send(0); //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 loop() {


  
//start of LCD display
s1 = !digitalRead(kSwitch1_PIN);
s2 = !digitalRead(kSwitch2_PIN);
s3 = !digitalRead(kSwitch3_PIN);


if (s1) {

   LCDClear(BLACK);
   LCDPutStr("Home > ", 2, 80, WHITE, BLACK);
   LCDPutStr("Dose Calculator", 110, 6, ORANGE, BLACK);
  
   Serial.println("Enter Blood Glucose"); 
 
  BG=0;
  while (Serial.available()==0){}
  while (Serial.available()>0) {
    while (Serial.available()>0) {
      BG=BG*10;
      a = Serial.read()-48;
      BG=BG+a;
      dose1= ((BG-target)/correctionfactor); }
    delay(5); }
   

 Serial.println("Enter Carbs");


 
  carbs=0;
  while (Serial.available()==0){}
  while (Serial.available()>0) {
    while (Serial.available()>0) {
      carbs=carbs*10;
      a = Serial.read()-48;
      carbs=carbs+a;
      dose2= (carbs/insulintocarbratio); }
    delay(5); }
   

dose = dose1 + dose2;

  Serial.print("The total dose is ");
  Serial.println(dose);


  history5 = history4;
  history4 = history3;
  history3 = history2;
  history2 = history1;
  history1 = dose;
 
      hour= getHour();
  minute=getMinute();
  month=getMonth();
  monthDay=getDay();

  month5 = month4;
  month4 = month3;
  month3 = month2;
  month2 = month1;
  month1 = month;
 
  day5 = day4;
  day4 = day3;
  day3 = day2;
  day2 = day1;
  day1 = monthDay;
 
  hour5 = hour4;
  hour4 = hour3;
  hour3 = hour2;
  hour2 = hour1;
  hour1 = hour;
 
  minute5 = minute4;
  minute4 = minute3;
  minute3 = minute2;
  minute2 = minute1;
  minute1 = minute;


  LCDPutStr("Dose = 6", 50, 4, WHITE, BLACK);
//  LCDPutStr(dose, 50, 60, WHITE, BLACK);

}


 else if (s3) {

  LCDClear(BLACK);    // Clear LCD to a solid color
        LCDPutStr("DoseCalc", 0, 4, GREEN, BLACK);
        LCDPutStr("Home > ", 2, 80, WHITE, BLACK);
        LCDPutStr("2000", 16, 4, GREEN, BLACK);
        LCDPutStr("History > ", 32, 56, WHITE, BLACK);
        LCDPutStr("New Dose > ", 62, 48, WHITE, BLACK);
        LCDPutStr("Diabetes Dose", 78, 4, GREEN, BLACK);
        LCDPutStr("Calculator", 94, 4, GREEN, BLACK);
        LCDPutStr("Home Screen", 110, 20, ORANGE, BLACK); 
       
         Serial.println("Dose History");
  Serial.print(history1);
  Serial.print(" units at ");

  Serial.print(hour1);
  Serial.print(":");
  Serial.print(minute1);
  Serial.print(" on ");
  Serial.print(month1);
  Serial.print("/");
  Serial.println(day1);
 
 
  Serial.print(history2);
  Serial.print(" units at ");
 
  Serial.print(hour2);
  Serial.print(":");
  Serial.print(minute2);
  Serial.print(" on ");
  Serial.print(month2);
  Serial.print("/");
  Serial.println(day2);
 
// Serial.println("time");
  Serial.print(history3);
  Serial.print(" units at ");
 
  Serial.print(hour3);
  Serial.print(":");
  Serial.print(minute3);
  Serial.print(" on ");
  Serial.print(month3);
  Serial.print("/");
  Serial.println(day3);
 
//  Serial.println("time");
  Serial.print(history4);
  Serial.print(" units at ");
 
  Serial.print(hour4);
  Serial.print(":");
  Serial.print(minute4);
  Serial.print(" on ");
  Serial.print(month4);
  Serial.print("/");
  Serial.println(day4);
 
////  Serial.println("time");
  Serial.print(history5);
  Serial.print(" units at ");
 
  Serial.print(hour5);
  Serial.print(":");
  Serial.print(minute5);
  Serial.print(" on ");
  Serial.print(month5);
  Serial.print("/");
  Serial.println(day5);
 
////  Serial.println("time");
  Serial.println(" ");
 
  delay(1000);

}

 else if (s2) {
 // We also want to figure out the time thing so it saves the times alongside the doses.
 
  LCDClear(BLACK);
    LCDPutStr("Home > ", 2, 80, WHITE, BLACK);

        LCDPutStr("History", 20, 4, MAGENTA, BLACK);
        LCDPutStr("History", 36, 4, WHITE, BLACK);
        LCDPutStr("History", 52, 4, MAGENTA, BLACK);
        LCDPutStr("History", 68, 4, WHITE, BLACK);
        LCDPutStr("History", 84, 4, MAGENTA, BLACK);
   


   LCDPutStr("History", 110, 38, ORANGE, BLACK);
  
  Serial.println("Dose History");
  Serial.print(history1);
  Serial.print(" units at ");
  Serial.println("time");
  Serial.print(history2);
  Serial.print(" units at ");
  Serial.println("time");
  Serial.print(history3);
  Serial.print(" units at ");
  Serial.println("time");
  Serial.print(history4);
  Serial.print(" units at ");
  Serial.println("time");
  Serial.print(history5);
  Serial.print(" units at ");
  Serial.println("time");
 
 

//D6 = (char *)&history5;
//D5 = (char *)&history4;
//D4 = (char *)&history3;
//D3 = (char *)&history2;
//D2 = (char *)&history1;
//D1 = (char *)&dose;

//dtostrf(dose, 5, 2, D1);
// dtostrf(history1, 5, 2, D2);
// dtostrf(history2, 5, 2, D3);
// dtostrf(history3, 5, 2, D4);
// dtostrf(history4, 5, 2, D5);
// dtostrf(history5, 5, 2, D6);
     
//LCDPutChar(*D1, 16, 4, WHITE, BLACK);

//LCDPutStr("Your Last Dose was:", 20, 4, WHITE, BLACK);
//LCDPutStr(D1, 36, 10, WHITE, BLACK);

// LCDPutStr(D2, 20, 3, WHITE, BLACK);
// LCDPutStr(D3, 36, 3, WHITE, BLACK);
// LCDPutStr(D4, 52, 3, WHITE, BLACK);
// LCDPutStr(D5, 68, 3, WHITE, BLACK);
// LCDPutStr(D6, 84, 3, WHITE, BLACK);



//  history5 = history4;
//  history4 = history3;
//  history3 = history2;
//  history2 = history1;
//  history1 = dose;

 }


 
    s1=0;
    s2=0;
    s3=0;


 delay(5); }


int getHour(){

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

  Wire.requestFrom(DS1307_ADDRESS, 7);

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

  return(hour);
}

int getMinute(){

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

  Wire.requestFrom(DS1307_ADDRESS, 7);

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

  return(minute);
}

int getMonth(){

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

  Wire.requestFrom(DS1307_ADDRESS, 7);

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

  return(month);
}

int getDay(){

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

  Wire.requestFrom(DS1307_ADDRESS, 7);

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

  return(monthDay);
}