0% found this document useful (0 votes)
4 views

Use This Code (In Celsius)

Uploaded by

aamermasc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Use This Code (In Celsius)

Uploaded by

aamermasc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

use this code (in Celsius)

////////////////////////////////////////////////
// iThermowall Thermometer Firmware //
// Celsius Modification //
////////////////////////////////////////////////

#include <Adafruit_SSD1306.h> //library oled


#include <Adafruit_MLX90614.h> //library sensor temperature
#include <Wire.h> //library I2C
#include <millisDelay.h> //library looping

#define SCREEN_WIDTH 128 // OLED display width, in pixels


#define SCREEN_HEIGHT 64 // OLED display height, in pixels

const int GREEN_LED = 3; // pin D3 for green LED


const int RED_LED = 5; // pin D5 for red LED
const int buzzer = 7; // pin D7 for buzzer
const int statePin = 9; // pin D9 for IR proximity sensor

bool measurement = false; //check temperature measurement running

const unsigned long interval_sensor = 50; // interval refresh sensor in mS


millisDelay sensorDelay; // the delay object

const unsigned long interval_display = 500; // interval refresh OLED display in mS


millisDelay displayDelay; // the delay object

const unsigned long delay_hold_red = 5000; // delay hold red LED in mS


const unsigned long delay_hold_green = 1000; // delay hold green LED in mS
millisDelay holdDelay; // the delay object

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)


Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

// Declaration for an MLX90614 sensor connected to I2C (SDA, SCL pins)


Adafruit_MLX90614 mlx = Adafruit_MLX90614();

void setup() {
Serial.begin(9600);

if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64


Serial.println(F("OLED Display allocation failed"));
for(;;);
}

pinMode(statePin, INPUT);
pinMode(GREEN_LED, OUTPUT);
pinMode(RED_LED, OUTPUT);
pinMode(buzzer, OUTPUT);

display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(20, 20);
display.println("Initializing");
display.display();
delay(250);
display.clearDisplay();
mlx.begin(); //sensor initialization

displayDelay.start(interval_display); //refresh display


}

void loop()
{
static float temperature = -1; //initial condition

int state = digitalRead(statePin); //IR sensor condition

if (state == LOW && measurement == false)


{
sensorDelay.start(interval_sensor); //read sensor
displayDelay.finish(); //finish interval refresh for display
measurement = true; //change state of measurement
}

if (measurement == true)
{
temperature = GetTemp(); //get temperature in Celsius

} else {
temperature = -1; //marker if sensor not reading temperature
}

ShowTemp(temperature); //display temperature to OLED


holdReading(); //call holdReading function
}

float GetTemp()
{
static int index = 0;
static float temptot = 0;
float hasil = 0;

if (sensorDelay.justFinished())
{
sensorDelay.repeat(); // repeat
temptot += mlx.readObjectTempC(); // add the reading to the total
index++; // increment index
if(index == 19)
{
hasil = temptot / 20; // calculate average
temptot = 0; // reset total
index = 0; // reset index
sensorDelay.stop(); // stop reading
displayDelay.finish(); // complete the refresh interval so that it shows
immediately
return hasil; // return result
}
}
return hasil; // As long as there are no results, give a value of result = 0
}

void ShowTemp(float temperature)


{
if (displayDelay.justFinished())
{
displayDelay.repeat(); // repeat
if (temperature == -1)
{
display.clearDisplay();
display.setTextSize(2);
display.setCursor(35, 5);
display.print("-----");
display.setCursor(35, 40);
display.print("-----");
display.display();
}
else if (temperature == 0)
{
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(20, 25);
display.println("WAIT ....");
display.display();
}
else
{
display.clearDisplay();
display.setTextSize(3);
display.setCursor(10, 20);
display.print(temperature, 1);
display.print(" C");
display.display();

if (temperature > 38)


{
digitalWrite(RED_LED, HIGH);
holdDelay.start(delay_hold_red);
} else {
digitalWrite(GREEN_LED, HIGH);
holdDelay.start(delay_hold_green);
}
digitalWrite(buzzer, HIGH);
displayDelay.stop(); // stop refresh display
}
}
}

void holdReading()
{
if (holdDelay.justFinished()) {
digitalWrite(RED_LED, LOW);
digitalWrite(GREEN_LED, LOW);
digitalWrite(buzzer, LOW);
measurement = false;
displayDelay.start(interval_display); // restart the OLED display
}
}

You might also like