Created
August 26, 2012 16:26
-
-
Save snatchev/3481486 to your computer and use it in GitHub Desktop.
Omortag's titration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Titrator manual detector based */ | |
//program statuses | |
#define WAITING 0 | |
#define WORKING 1 | |
int programStatus; | |
const int VOLUME = 10; //microliters - titrant | |
//the outputs pins | |
const int air = 2; | |
const int water = 4; | |
const int acid = 7; | |
const int base = 8; | |
//user button/light | |
const int status_button = 12; | |
const int status_light = 13; | |
//the inputs pins | |
const int mV_sensor = 3; //this is an analog input | |
//NOTE: it may be better to use decimals (double or float) for mV value | |
int mV_sensor_value = 0; | |
const int detector = 5; | |
int detector_value = 0; | |
const int measurements_count = 1; | |
//NOTE: likewise it might be better use an array of decimals | |
int mV[measurements_count]; //store the 20 measurements of the titration | |
int volume[measurements_count]; | |
//use this function to blink a light | |
void blinkLight(int pin, int delay_msec){ | |
digitalWrite(pin, HIGH); | |
delay(delay_msec); | |
digitalWrite(pin, LOW); | |
delay(delay_msec); | |
} | |
//use this function to send blinks to the status light | |
void blinkStatus(int count, int delay_msec){ | |
for(int i=0; i<count; i++){ | |
blinkLight(status_light, delay_msec); | |
} | |
} | |
void setup(){ | |
pinMode(air, OUTPUT); | |
pinMode(water, OUTPUT); | |
pinMode(acid, OUTPUT); | |
pinMode(base, OUTPUT); | |
pinMode(status_button, INPUT_PULLUP); //pushbutton - teensy++ only. | |
//pinMode(detector, INPUT_PULLUP); | |
//turn off all of the lights. | |
//pinMode(mV_sensor, INPUT); | |
Serial.begin(9600); //set up usb serial | |
} | |
void loop(){ | |
startProgram(); //wait until user starts the program | |
digitalWrite(air, HIGH); //open air | |
delay(3000); | |
//read 1 measurements | |
for(int i=0; i < measurements_count; i++){ | |
saveData(i); | |
digitalWrite(air, LOW); //close air | |
delay(5000); | |
digitalWrite(acid, HIGH); //open acid | |
waitUntilDetectorChanges(); | |
digitalWrite(acid, LOW); //close acid | |
} | |
Serial.print("end of program"); | |
endProgram(); //wait until user restarts the program | |
} | |
void saveData(int index){ //index is the consecutive measurement count | |
mV[index] = read_mV(); | |
if(index == 0) { | |
volume[index] = 0; | |
} | |
else { | |
volume[index] = volume[index - 1 ] + VOLUME; | |
} | |
Serial.print("measurement #"); | |
Serial.print(index); | |
Serial.print("\t mV "); | |
Serial.print(mV[index]); | |
Serial.print("\t uL "); | |
Serial.println(volume[index]); | |
} | |
/* | |
read mV - waits until equilibrium and returns mV of the sensor | |
*/ | |
int read_mV(){ | |
int delay_msec = 5000; //5 seconds | |
int threshold = 1; //when is the delta stable? | |
int value1 = 0; | |
int value2 = 0; | |
int delta = 0; | |
Serial.println("reading mV"); | |
//repeat the reading until the readings are stable | |
do{ | |
value1 = analogRead(mV_sensor); | |
delay(delay_msec); | |
value2 = analogRead(mV_sensor); | |
delta = value2 - value1; | |
Serial.print("delta "); | |
Serial.println(delta); | |
blinkStatus(1, 100); | |
} | |
while(threshold <= abs(delta)); | |
blinkStatus(5, 100); | |
return value2; | |
} | |
//this is used when we want to wait until something changes on the detector | |
//NOTE: this will need to change to analog in the original designs. | |
int waitUntilDetectorChanges(){ | |
int delay_msec = 200; //the shorter, the more accurate | |
//loop until the detector has changed | |
while(digitalRead(detector) == 1){ | |
//TODO: replace this with an analog design | |
blinkLight(status_light, delay_msec); | |
} | |
Serial.println("detector has been tripped"); | |
} | |
//loop to wait until user starts program | |
void startProgram(){ | |
setStatus(WAITING); | |
while(digitalRead(status_button)){ //loop until the button is pushed | |
blinkLight(status_light, 250); | |
} | |
//button has been pressed. blink 3x | |
blinkStatus(3, 100); | |
setStatus(WORKING); | |
} | |
//loop to wait for the user to restart the program | |
void endProgram(){ | |
setStatus(WAITING); | |
} | |
int setStatus(int status){ | |
programStatus = status; | |
if(programStatus == WAITING){ | |
digitalWrite(status_light, LOW); | |
} | |
if(programStatus == WORKING){ | |
Serial.println("program begins"); | |
digitalWrite(status_light, HIGH); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment