// Cryoscopy_3TS_ MA (Mechanical Agitator)
// Using 3 temperature sensors and an mechanical agitator
// IDE 1.8.19
// Adapted and modified by Alberto Villalobos (IDE 1.8.19)
// References:
// 1. http://hombrosdegigantes.blogspot.com
// 2. http://cetroniconline.blogspot.com/2014/07/tutorial-arduino-iv-sensor-de.html?m=1
// 3. https://randomnerdtutorials.com/guide-for-ds18b20-temperature-sensor-with-arduino/
// 4. http://diymakers.es/arduino-bluetooth/
// 5. BR010038 HG7881 (L9110) Dual Channel Motor Driver Module
//    https://www.BananaRobotics.com/shop/HG7881-(L9110)-Dual-Channel-Motor-Driver-Module
//
//  Motor connections: 
//  Arduino digital output D2 to motor driver input B-IA.
//  Arduino digital output D3 to motor driver input B-IB.
//  Motor driver VCC to operating voltage 5V.
//  Motor driver GND to common ground.
//  Motor driver MOTOR B screw terminals to a small motor.

#include <AltSoftSerial.h>
#include <OneWire.h>
#include <DallasTemperature.h>

float TempSensorSamp1;//Sample 1 Sensor
float TempSensorSamp2;//Sample 2 Sensor
float TempSensorSamp3;//Sample 3 Sensor

float TempThres = 5;//***** threshold temperatuere to activate motor *****

float CorrTempSamp1 = 0.313; //Correction to the temperature initially measured by the Sample 1 sensor
float CorrTempSamp2 = 0.188; //Correction to the temperature initially measured by the Sample 2 sensor
float CorrTempSamp3 = 0.312; //Correction to the temperature initially measured by the Sample 3 sensor

String TempData = "";

AltSoftSerial BTserial(9, 8); // RX | TX

#define ONE_WIRE_BUS 4 // Data wire is conntec to the Arduino digital pin 4
                             
OneWire oneWire(ONE_WIRE_BUS); 

DallasTemperature sensores(&oneWire);

// ***** Indicate the directions of the sensors (each DS18B20 sensor has its own code, References 1 y 2) *****

DeviceAddress Samp1 = {0x28,0xFF,0xC1,0xEA,0x64,0x15,0x02,0x61};  //Sensor 1
DeviceAddress Samp2 = {0x28,0xFF,0x59,0x8F,0x64,0x15,0x02,0xF9};  //Sensor 2
DeviceAddress Samp3 = {0x28,0xFF,0x32,0xC9,0x64,0x15,0x02,0x71};  //Sensor 3

// Motor and drive wired connections

#define HG7881_B_IA 2 // D2 --> Motor B Input A --> MOTOR B +
#define HG7881_B_IB 3 // D3 --> Motor B Input B --> MOTOR B -
 
// functional connections
#define MOTOR_B_PWM HG7881_B_IA // Motor B PWM Speed
#define MOTOR_B_DIR HG7881_B_IB // Motor B Direction
 
// the actual values for "fast" and "slow" depend on the motor

#define PWM_SLOW 50  // arbitrary slow speed PWM duty cycle
#define PWM_FAST 200 // arbitrary fast speed PWM duty cycle

void setup(void)
{
  Serial.begin(9600);
  BTserial.begin(9600);  
  sensores.begin();
  
  pinMode( MOTOR_B_DIR, OUTPUT );
  pinMode( MOTOR_B_PWM, OUTPUT );
  digitalWrite( MOTOR_B_DIR, LOW );
  digitalWrite( MOTOR_B_PWM, LOW );
}

void loop(void)
{
  
  sensores.requestTemperatures();

  // ***** Establish the resolution for each sensor *****
  
  //9 bits 0.50 ºC
  //10 bits 0.25 ºC
  //11 bits 0.125 ºC
  //12 bits 0.0625 ºC
  
  sensores.setResolution(Samp1,12);
  sensores.setResolution(Samp2,12);
  sensores.setResolution(Samp3,12);
  
  TempSensorSamp1 = sensores.getTempC(Samp1) + CorrTempSamp1;//Sample 1 Sensor
  TempSensorSamp2 = sensores.getTempC(Samp2) + CorrTempSamp2;//Sample 2 Sensor
  TempSensorSamp3 = sensores.getTempC(Samp3) + CorrTempSamp3;//Sample 3 Sensor

  TempData = String(String(TempSensorSamp1,3) + "/" + String(TempSensorSamp2,3) + "/" + String(TempSensorSamp3,3) + "/");

  Serial.print(TempData);Serial.println(" , ");

  BTserial.print(TempData);


  // Checks if it has reached the threshold value
  
 if (TempSensorSamp1 < TempThres or TempSensorSamp2 < TempThres or TempSensorSamp3 < TempThres)
  {

  // set the motor speed and direction
        
  digitalWrite( MOTOR_B_DIR, LOW );
  digitalWrite( MOTOR_B_PWM, HIGH );
   
  }
  else
  {
   digitalWrite( MOTOR_B_DIR, LOW );
   digitalWrite( MOTOR_B_PWM, LOW );
  } 
  delay(500); // Determines sensor data capture rate
}
