From e0eb0be69b57aa430c01da01c82c70eb3f1564fb Mon Sep 17 00:00:00 2001 From: ThirstyShark Date: Fri, 1 Jun 2018 19:15:22 +0000 Subject: [PATCH] Add dogfood and titration project. --- dogfood-timer.ino | 357 ++++++++++++++++++++++++++++++++++++++++ pneumatic-titration.ino | 180 ++++++++++++++++++++ 2 files changed, 537 insertions(+) create mode 100644 dogfood-timer.ino create mode 100644 pneumatic-titration.ino diff --git a/dogfood-timer.ino b/dogfood-timer.ino new file mode 100644 index 0000000..4b43cf0 --- /dev/null +++ b/dogfood-timer.ino @@ -0,0 +1,357 @@ +// include the library code: +#include +#include +// initialize the library by associating any needed LCD interface pin +// with the arduino pin number it is connected to +const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2; +int a = 1, b = 5, c = 2, d = 5, e = 0, f = 4, g = 4, h = 1, i = 2, j = 7; +unsigned long timeset = 0; +LiquidCrystal lcd(rs, en, d4, d5, d6, d7); +const int trigPin = 9; +const int echoPin = 8; +long duration; +int distance = 0; +unsigned long wait; +time_t last = 0; +unsigned long lastlong = 0; +int prevphoto = 0; +int today = 0; +int oldday; +boolean check = false; +void setup() { + Serial.begin(9600); + // set up the LCD's number of columns and rows: + lcd.begin(16, 2); + // Print a message to the LCD. + pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output + pinMode(echoPin, INPUT); // Sets the echoPin as an Input + pinMode(10, OUTPUT); // pin that enables backlight + digitalWrite(10, HIGH); + setuptime(); +} + +void loop() { + +} +void setuptime() { + lcd.clear(); + lcd.setCursor(0, 1); + lcd.print("Enter UNIX time"); + lcd.setCursor(0, 0); + lcd.print(a); + while (digitalRead(7) == 0) { + if(digitalRead(6) == 1) { + a+=1; + if(a == 10) { + a = 0; + } + lcd.setCursor(0, 0); + lcd.print(a); + delay(200); + } + } + delay(250); + lcd.setCursor(1, 0); + lcd.print(b); + while (digitalRead(7) == 0) { + if(digitalRead(6) == 1) { + b+=1; + if(b == 10) { + b = 0; + } + lcd.setCursor(1, 0); + lcd.print(b); + delay(200); + } + } + delay(250); + lcd.setCursor(2, 0); + lcd.print(c); + while (digitalRead(7) == 0) { + if(digitalRead(6) == 1) { + c+=1; + if(c == 10) { + c = 0; + } + lcd.setCursor(2, 0); + lcd.print(c); + delay(200); + } + } + delay(250); + lcd.setCursor(3, 0); + lcd.print(d); + while (digitalRead(7) == 0) { + if(digitalRead(6) == 1) { + d+=1; + if(d == 10) { + d = 0; + } + lcd.setCursor(3, 0); + lcd.print(d); + delay(200); + } + } + delay(250); + lcd.setCursor(4, 0); + lcd.print(e); + while (digitalRead(7) == 0) { + if(digitalRead(6) == 1) { + e+=1; + if(e == 10) { + e = 0; + } + lcd.setCursor(4, 0); + lcd.print(e); + delay(200); + } + } + delay(250); + lcd.setCursor(5, 0); + lcd.print(f); + while (digitalRead(7) == 0) { + if(digitalRead(6) == 1) { + f+=1; + if(f == 10) { + f = 0; + } + lcd.setCursor(5, 0); + lcd.print(f); + delay(200); + } + } + delay(250); + lcd.setCursor(6, 0); + lcd.print(g); + while (digitalRead(7) == 0) { + if(digitalRead(6) == 1) { + g+=1; + if(g == 10) { + g = 0; + } + lcd.setCursor(6, 0); + lcd.print(g); + delay(200); + } + } + delay(250); + lcd.setCursor(7, 0); + lcd.print(h); + while (digitalRead(7) == 0) { + if(digitalRead(6) == 1) { + h+=1; + if(h == 10) { + h = 0; + } + lcd.setCursor(7, 0); + lcd.print(h); + delay(200); + } + } + delay(250); + lcd.setCursor(8, 0); + lcd.print(i); + while (digitalRead(7) == 0) { + if(digitalRead(6) == 1) { + i+=1; + if(i == 10) { + i = 0; + } + lcd.setCursor(8, 0); + lcd.print(i); + delay(200); + } + } + delay(250); + lcd.setCursor(9, 0); + lcd.print(j); + while (digitalRead(7) == 0) { + if(digitalRead(6) == 1) { + j+=1; + if(j == 10) { + j = 0; + } + lcd.setCursor(9, 0); + lcd.print(j); + delay(200); + } + } // on the next line, for f I have to divide by 10 and add a zero otherwise it gives a negative number for some reason + timeset = (1000000000 * a)+(100000000 * b)+(10000000 * c)+(1000000 * d)+(100000 * e)+(100000 * f / 10)+(1000 * g)+(100 * h)+(10 * i)+j; + unsigned long timesetcst = timeset - 18000; //convert to central time from utc + setTime(timesetcst); + delay(250); + lcd.clear(); + lcd.setCursor(0, 0); + lcd.print(timeset); + lcd.setCursor(0, 1); + lcd.print("is this ok?"); + while (digitalRead(7) == 0) { + if(digitalRead(6) == 1) { + setuptime(); + } + } + delay(250); + unsigned long oldtime = 0; + unsigned long secper5 = now(); + check = false; + while(1 == 1) { + time_t t = now(); + digitalWrite(trigPin, LOW); + delayMicroseconds(2); + // Sets the trigPin on HIGH state for 10 micro seconds + digitalWrite(trigPin, HIGH); + delayMicroseconds(10); + digitalWrite(trigPin, LOW); + + // Reads the echoPin, returns the sound wave travel time in microseconds + duration = pulseIn(echoPin, HIGH); + // Calculating the distance + distance = duration*0.034/2; + // Prints the distance on the Serial Monitor + //Serial.print("Distance: "); + //Serial.println(distance); + + + if(distance < 30 || analogRead(3) >= 150 || digitalRead(7) == 1 || digitalRead(6) == 1) { + digitalWrite(10, HIGH); + wait = now(); + } + if(digitalRead(7) == 1) { + if(check == false){ + check = true; + delay(100); + } else { + check = false; + delay(100); + } + } + if(digitalRead(6) == 1 && check == true) { + delay(100); + setuptime(); + } + if(now() != oldtime) { + //if(check == false) { + //Serial.println(analogRead(3)); + if(now()-350 >= secper5) { + t = t + 1; + secper5 = now(); + } + if(now()-15 >= wait && analogRead(3) < 150) { + digitalWrite(10, LOW); + } + if(day(t) != oldday) { + today = 0; + } + lcd.setCursor(0,0); + lcd.clear(); /* + lcd.print(hourFormat12(t)); + lcd.print(":"); + if(minute(t) < 10) { + lcd.print("0"); + } + lcd.print(minute(t)); + lcd.print(" "); + if(isAM(t) == true) { + lcd.print("AM"); + } + else { + lcd.print("PM"); + } */ + + if (analogRead(3) < 150 && prevphoto >=150) { + last = now(); + lastlong = now(); + today ++; + } + if(lastlong == 0) { + lcd.print("LF: "); + if (analogRead(3) > 100) { + if(now() > 1526169600 && now() < 1526274000) { + lcd.clear(); + lcd.print("Happy Mother's"); + lcd.setCursor(0, 1); + lcd.print("Day! :)"); + } else { + lcd.print("Can is open"); + } + } else { + lcd.print("never"); + } + } + else { + lcd.print("LF: "); + if (analogRead(3) > 100) { + if(now() > 1526169600 && now() < 1526274000) { + lcd.clear(); + lcd.print("Happy Mother's"); + lcd.setCursor(0, 1); + lcd.print("Day! :)"); + } else { + lcd.print("Can is open"); + } + } else { + int hours = (int)(now() / 3600) - (int)(lastlong / 3600); + int minutes = (now() / 60) - (lastlong / 60) - hours * 60; + while(minutes < 0) { + minutes = minutes + 60; + hours --; + } + if(hours > 24) { + lcd.print("over 24 hours ago"); + } else { + lcd.print(hours); + lcd.print(" hr "); + lcd.print(minutes); + lcd.print(" min"); + } + } + if((now() < 1526169600 || now() > 1526274000) || analogRead(3) < 100) { + lcd.setCursor(0, 1); + lcd.print(" Today: "); + lcd.print(today); + lcd.print(" Meal"); + if(today != 1) { + lcd.print("s"); + } + } + } + //lcd.setCursor(0, 1); + //lcd.print(now() + 18000); //display unix time stamp on line 1 + oldday = day(); + oldtime = now(); + prevphoto = analogRead(3); + if(check == true) { + //check == true now + lcd.clear(); + lcd.setCursor(0, 0); + lcd.print(hourFormat12(t)); + lcd.print(":"); + if(minute(t) < 10) { + lcd.print("0"); + } + lcd.print(minute(t)); + lcd.print(" "); + if(isAM(t) == true) { + lcd.print("AM"); + } + else { + lcd.print("PM"); + } + lcd.setCursor(0, 1); + lcd.print(now()); + + //delay(50); + } + + //} //else { + + } + else { + //int staticmillis = millis(); + //while(staticmillis + 50 > millis()) { + delay(50); + //} + } + + } +} \ No newline at end of file diff --git a/pneumatic-titration.ino b/pneumatic-titration.ino new file mode 100644 index 0000000..7c4e484 --- /dev/null +++ b/pneumatic-titration.ino @@ -0,0 +1,180 @@ +int timeInterval = 0; +int analogPin = 5; +int drips = 0; +float grams = 30; +float totgrams = 0; +float phSense; +float mass = 39.99711; +bool titrate = false; +void setup() { +// put your setup code here, to run once: + if (drips > 0) { + grams = grams - (2.333333/20 * drips); + } + pinMode(2, OUTPUT); + pinMode(3, OUTPUT); + pinMode(4, OUTPUT); + pinMode(9, INPUT); //stream; 2 seconds + pinMode(10, OUTPUT); + pinMode(11, INPUT); //squirt; 0.5 seconds + pinMode(12, INPUT); //drip; 0.01 seconds + Serial.begin(9600); + digitalWrite(4, LOW); // start extended +} +void loop() { + timeInterval=0; + if(digitalRead(7) == 0) { + digitalWrite(4, HIGH); // up and down + delay(2500); + digitalWrite(3, HIGH); // number 3 is spring-loaded + delay(1500); + digitalWrite(4, LOW); + delay(250); + motor(255); + delay(3000); + titrate = true; //true, false for now + if(digitalRead(7) == 0 || titrate == false) { + Serial.println("CANCELLING"); + stopmotor(); + titrate = false; + digitalWrite(4, HIGH); // up and down + delay(2500); + digitalWrite(3, LOW); // number 3 is spring-loaded + delay(1500); + digitalWrite(4, LOW); + delay(500); + while(true) { + loop(); + } + } + } + if(digitalRead(9) == 0) + { + motor(255); + delay(2000); + stopmotor(); + //timeInterval = 175 * grams; + //totgrams = totgrams + grams; + } + else if(digitalRead(11) == 0 || titrate == true) + { + takepH(); + float rate = 1 - (phSense / 7)*(phSense / 7); + while (rate > 0 && rate <=1) { + motor(255); + if(digitalRead(7) == 0) { + Serial.println("CANCELLING"); + titrate = false; + stopmotor(); + digitalWrite(4, HIGH); // up and down + delay(1500); + digitalWrite(3, LOW); // number 3 is spring-loaded + delay(2000); + digitalWrite(4, LOW); + while(true) { + loop(); + } + } + + if(rate > 0.8) { + rate = rate * 2; + } + if(phSense > 3) { + rate = 0.01; + } + timeInterval = rate * 175; + if (timeInterval < 30) { + timeInterval = 15; + } + totgrams = totgrams + timeInterval / 175; + digitalWrite(2, HIGH); + delay(timeInterval); + digitalWrite(2, LOW); + for(int i = 0; i < 10; i += 1){ + delay(rate * 900); + if(digitalRead(7) == 0) { + Serial.println("CANCELLING"); + titrate = false; + stopmotor(); + digitalWrite(4, HIGH); // up and down + delay(1500); + digitalWrite(3, LOW); // spring-loaded + delay(2000); + digitalWrite(4, LOW); + while(true) { + loop(); + } + } + } + takepH(); + rate = 1 - (phSense / 7)*(phSense / 7); + delay(125); + } + Serial.println("done, returning pneumatics to holding point"); + titrate = false; + stopmotor(); + digitalWrite(4, HIGH); // up and down + delay(1500); + digitalWrite(3, LOW); // spring-loaded + delay(1500); + digitalWrite(4, LOW); + timeInterval = 0; + //timeInterval = 500; + //totgrams = totgrams + 2.857143; + } + else if (digitalRead(12) == 0 || drips > 0) + { + timeInterval = 15; + totgrams = totgrams + 2.333333/20; + drips --; + } + //Serial.print("Time Valve is open = "); + //Serial.println(timeInterval); + if(timeInterval > 0) + { + digitalWrite(2, HIGH); + delay(timeInterval); + digitalWrite(2, LOW); + takepH(); + } + delay(50); +} +float takepH() +{ + int samples = 30; + int aRead = 0; + for (int i = 0; i < samples ; i++) + { + aRead += analogRead(analogPin); + delay(100); + } + phSense = 0; + float phvolt = 5.0 * aRead/ (1023 * samples); // assuming 5V reference + phSense = 14 - phvolt/0.25; // convert voltage to pH + phSense = phSense - 1.3; + Serial.print("Time Interval = "); + Serial.println(timeInterval); + Serial.print("Analog in reading: "); + Serial.print(aRead/20); // print pH value on serial monitor + Serial.print(" - Calculated pH Level: "); + Serial.println(phSense, 2); // 1 = one decimal, 2 = two decimals, etc + Serial.print("Grams: "); + Serial.println(totgrams * 0.87, 4); + float molarity = 0.0025 * totgrams * 0.9 / 49.6; + Serial.print("Molarity of 20 mL of acid: "); + Serial.println(molarity, 10); + // removed the /10 + delay(500); +} +void motor(int speed) { + analogWrite(10, speed); + //delay(timerun); + //analogWrite(10, 0); +} +void stopmotor() { + analogWrite(10, 0); + //delay(timerun); + //analogWrite(10, 0); +} + +