Combatting a Real World Problem
Project: pH Regulator
About this Project:
Material Needed for this Project:
- pH Sensor/ Meter(pH Probe ad pH Circuit Board)
- Water Pump(built completely by hand)
- 9V Battery
- UNO R3 Arduino
- Jumper wires
- 5 Volt Relay
- Computer with Arduino Software(I have mentioned how to download Arduino software previously on my blog)
- USB Cord
- Bread Board
Design Process and Diagram Set Up:
We saw that we could connect the motor directly to the Arduino which would make our design more efficient and simple, eliminating other components what could possibly fail. This final design was the layout of our finished project.
Picture of Project Laid Out:
Arduino Code Needed for this Project:
//Some pH meter code from https://2020.igem.org/wiki/images/6/64/T--Botchan_Lab_Tokyo--Poster_Arduinosketch_pdf_pdf.pdf
// 5 Volt DC motor code was written using prior knowledge from previous projects
//Initial required setup
const int inPin = A1;
const int outPin = 9;
//Initializing needed variables
int sensorValue = 0;
int outputValue = 0;
#define SensorPin A1
#define Offset 0.00
#define interval 20
#define printInt 800
#define len 40
int pH[len];
int pHIndex=0;
int motor = 4;
//initializes the program with the motor being off
bool motorON = false;
//Basic Setup
void setup(void) {
pinMode(motor, OUTPUT);
Serial.begin(9600);
//Show that the program is starting
Serial.println("pH Regulator has started:");
}
//calculate the pH and voltage while considering millis()
void loop() {
static unsigned long firstTime = millis();
static unsigned long printTime = millis();
static float pHValue,voltage;
if(millis()-firstTime > interval)
{
//calculate voltage and pH value
pH[pHIndex++]=analogRead(SensorPin);
if(pHIndex==len)pHIndex=0;
voltage = avergearray(pH, len)*5.0/1024;
pHValue = 3.5*voltage+Offset;
firstTime=millis();
}
//Condition to print the voltage and pH value
if(millis() - printTime > printInt)
{
//prints voltage and pH values
Serial.print("Voltage:");
Serial.print(voltage,2);
Serial.print(" pH value: ");
Serial.println(pHValue);
//Condition to start the motor
if(pHValue <= 5.00){
analogWrite(motor,255);
delay(5000);
//prints so the user knows that the motor has started
Serial.println("Inside if condition");
//adds a delay to the code so that the sensor has time to collect its data
delay(1000);
}
//turns the motor off
else {
analogWrite(motor,0);
}
printTime=millis();
}
}
//Determining and returning the voltage
double avergearray(int* arr, int num){
int i;
int max,min;
double avg;
long amount=0;
//Return an error if the value is not determinable
if(num<=0){
Serial.println("Error");
return 0;
}
if(num<5){
for(i=0;i<num;i++){
amount+=arr[i];
}
avg = amount/num;
return avg;
}else{
if(arr[0]<arr[1]){
min = arr[0];max=arr[1];
}
else{
min=arr[1];max=arr[0];
}
for(i=2;i<num;i++){
if(arr[i]<min){
amount+=min;
min=arr[i];
}else {
if(arr[i]>max){
amount+=max;
max=arr[i];
}else{
amount+=arr[i];
}
}
}
avg = (double)amount/(num-2);
}
return avg;
}
Comments
Post a Comment