/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: "Temperature Control"
	- Source Code compiled for: Arduino Uno
	- Source Code created on: 2024-04-14 12:23:05

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* if temp is greater than 38 degree celcius or less */
	/* than 16 degree celcius turn red light otherwise */
	/* turn green */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include <DS18B20.h> //https://github.com/matmunk/DS18B20
#include <Servo.h>   //https://github.com/arduino-libraries/Servo

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateOutputs(void);

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t temp_sensor_DS18B20_DQ_PIN_D2 = 2;

/***** DEFINITION OF PWM OUTPUT PINS *****/
const uint8_t my_servo_Servomotor_PWMSignal_PIN_D3 = 3;
const uint8_t my_servo_Servomotor_PWMSignal_PIN_D5 = 5;

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
/***** used to store raw data *****/
uint8_t my_servo_Servomotor_PWMSignal_PIN_D3_rawData = 0;
uint8_t my_servo_Servomotor_PWMSignal_PIN_D5_rawData = 0;

/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
/***** used to store data after characteristic curve transformation *****/
float my_servo_Servomotor_PWMSignal_PIN_D3_phyData = 0.0;
float my_servo_Servomotor_PWMSignal_PIN_D5_phyData = 0.0;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
DS18B20 ds(temp_sensor_DS18B20_DQ_PIN_D2);
Servo my_servo_D3;
Servo my_servo_D5;

/****** SYSTEM REQUIREMENTS *****/
const float HIGH_TEMP_THRESHOLD = 38.0;  // High temperature threshold in degree Celsius
const float LOW_TEMP_THRESHOLD = 16.0;   // Low temperature threshold in degree Celsius

void setup(void)
{
  // put your setup code here, to run once:
  Serial.begin(9600);

  ds.resetSearch();
  while (ds.selectNext())
  {
    ds.setAlarms(LOW_TEMP_THRESHOLD, HIGH_TEMP_THRESHOLD);
  }

  my_servo_D3.attach(my_servo_Servomotor_PWMSignal_PIN_D3);
  my_servo_D5.attach(my_servo_Servomotor_PWMSignal_PIN_D5);

  pinMode(temp_sensor_DS18B20_DQ_PIN_D2, INPUT);

  pinMode(my_servo_Servomotor_PWMSignal_PIN_D3, OUTPUT);
  pinMode(my_servo_Servomotor_PWMSignal_PIN_D5, OUTPUT);
}

void loop(void)
{
  // put your main code here, to run repeatedly:
  ds.doConversion();

  ds.resetSearch();
  while (ds.selectNextAlarm())
  {
    Serial.print("Alarm Low: ");
    Serial.print(ds.getAlarmLow());
    Serial.println(" C");
    Serial.print("Alarm High: ");
    Serial.print(ds.getAlarmHigh());
    Serial.println(" C");
    Serial.print("Temperature: ");
    Serial.print(ds.getTempC());
    Serial.println(" C\n");
  }

  float currentTemp = ds.getTempC(); // Get the current temperature reading

  // Check if the temperature is within the desired range
  if (currentTemp > HIGH_TEMP_THRESHOLD || currentTemp < LOW_TEMP_THRESHOLD)
  {
    // Turn on the red light
    my_servo_D3.write(180); // Assuming 180 degrees corresponds to the red light position
    my_servo_D5.write(0);   // Assuming 0 degrees corresponds to the off position
  }
  else
  {
    // Turn on the green light
    my_servo_D3.write(0);   // Assuming 0 degrees corresponds to the off position
    my_servo_D5.write(180); // Assuming 180 degrees corresponds to the green light position
  }

  delay(10000);
}

void updateOutputs()
{
  my_servo_D3.write(my_servo_Servomotor_PWMSignal_PIN_D3_rawData);
  my_servo_D5.write(my_servo_Servomotor_PWMSignal_PIN_D5_rawData);
}
