Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define TEMPERATURE_SENSOR A14
- #define HEATER A4
- #define PH_SENSOR_INPUT A13
- #define PH_PUMP_ACID A5
- #define PH_PUMP_BASE A3
- #define STIRRING_PHOTO_SENSOR A8
- #define STIRRER RED_LED
- double sum = 0;
- boolean Heater = false;
- void setup() {
- Serial.begin(115200);
- pinMode(STIRRING_PHOTO_SENSOR, OUTPUT);
- pinMode(STIRRER, OUTPUT);
- }
- void loop() {
- // put your main code here, to run repeatedly:
- double targetTemperature;
- double targetpH;
- int targetStirringSpeed;
- double currentpH = 0;
- double currentTemperature = readTemperature();
- controlHeater(currentTemperature, targetTemperature);
- int count = 0;
- for(count = 0; count < 10; count++){
- currentpH = readpH(currentTemperature);
- sum = sum + currentpH;
- }
- currentpH = sum / 10;
- controlpHPumps(currentpH, targetpH);
- double currentStirringSpeed = readStirringSpeed();
- controlStirrer(currentStirringSpeed, targetStirringSpeed);
- }
- double readTemperature() {
- const int Nortemperature = 25;
- const double resistance = 10200;
- const double B = 4220;
- double Thermistor = analogRead(TEMPERATURE_SENSOR);
- Thermistor = (Thermistor * resistance)/(3.3-Thermistor);
- double Reverse_currentTemperature = (log(Thermistor/10000))/B + 1/(Nortemperature);
- return 1/Reverse_currentTemperature;
- }
- void controlHeater(double currentTemperature, double targetTemperature) {
- if(currentTemperature >= targetTemperature){
- digitalWrite(HEATER, LOW);
- Heater = false;
- }
- else {
- digitalWrite(HEATER, HIGH);
- Heater = true;
- }
- }
- double readpH(double currentTemperature) {
- float reading = analogRead(PH_SENSOR_INPUT);
- reading = reading * 3 / 1023;
- reading = (reading - 0.4) / (0.0592);
- return reading;
- }
- void controlpHPumps(double currentpH, double targetpH) {
- if(currentpH > targetpH){
- digitalWrite(PH_PUMP_BASE, LOW);
- digitalWrite(PH_PUMP_ACID, HIGH);
- }
- else if(currentpH == targetpH){
- digitalWrite(PH_PUMP_BASE, LOW);
- digitalWrite(PH_PUMP_ACID, LOW);
- }
- else {
- digitalWrite(PH_PUMP_ACID, LOW);
- digitalWrite(PH_PUMP_BASE, HIGH);
- }
- }
- bool lastStirringSensorState = false;
- long lastTimeSawLight = millis();
- int lastStirringSpeedReading = 0;
- int readStirringSpeed() {
- int readingAnalog = analogRead(STIRRING_PHOTO_SENSOR);
- bool state = readingAnalog > 600;
- if (lastStirringSensorState != state) {
- if (state) { //We just saw light
- long currentTime = millis();
- int timeForHalfRev = int(currentTime - lastTimeSawLight);
- lastTimeSawLight = currentTime;
- lastStirringSpeedReading = (60*1000)/(timeForHalfRev*2);
- Serial.println(lastStirringSpeedReading);
- }
- }
- lastStirringSensorState = state;
- return lastStirringSpeedReading;
- }
- int currentStirringPWM = 1;
- long lastTimeChangedPWM = 0;
- const long PWMChangeTimeout = 100;
- void controlStirrer(int currentStirringSpeed, int targetStirringSpeed) {
- if (millis() - lastTimeChangedPWM >= PWMChangeTimeout) {
- lastTimeChangedPWM = millis();
- if (targetStirringSpeed >= currentStirringSpeed) {
- currentStirringPWM++;
- }
- else{
- currentStirringPWM--;
- }
- analogWrite(STIRRER, currentStirringPWM);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement