Arduino : Automatic cooling system
Schéma du Montage :
Photos :
Code :
/*
Sites utiles :
https://github.com/UniQHW/SH5461AS
https://github.com/ChecheSwap/Seven_Segments_4Digits_SMA420564L-ATMEGA328P
https://github.com/se4oev/4-Digit-7-Segment-Display-SH5461AS/blob/master/SH5461AS_edit_/SH5461AS.ino
https://osoyoo.com/2017/08/08/arduino-lesson-4-digit-7-segment-led-display/
http://thomas.bibby.ie/using-the-kyx-5461as-4-digit-7-segment-led-display-with-arduino/
*/
//https://www.electronicwings.com/arduino/thermistor-interfacing-with-arduino-uno
#include <math.h>
const int thermistor_output = A1;
//Определяем пины к которым подключены аноды (a, b, c...)
#define a 7 //1
#define b 11 //2
#define c 2 //3
#define d 4
#define e 5
#define f 8 //6
#define g 13 //7
#define p 3 //8
//Определяем пины к которым подключены катоды
#define d4 12 //9 // Первый разряд
#define d3 10
#define d2 9 //11
#define d1 6 //12
//Определяем переменные
//int del = 100; //задержка между сменой цифр
long n = 0;// n represents the value displayed on the LED display. For example, when n=0, 0000 is displayed. The maximum value is 9999.
int x = 100;
int del = 5;//Set del as 5; the value is the degree of fine tuning for the clock
int count = 0;//Set count=0. Here count is a count value that increases by 1 every 0.1 second, which means 1 second is counted when the value is 10
float temperature = 35.47;
void setup()
{
Serial.begin(9600);
pinMode(d1, OUTPUT); //определяем назначение цифровых пинов
pinMode(d2, OUTPUT);
pinMode(d3, OUTPUT);
pinMode(d4, OUTPUT);
pinMode(a, OUTPUT);
pinMode(b, OUTPUT);
pinMode(c, OUTPUT);
pinMode(d, OUTPUT);
pinMode(e, OUTPUT);
pinMode(f, OUTPUT);
pinMode(g, OUTPUT);
pinMode(p, OUTPUT);
pinMode(A0, OUTPUT); //RELAY PIN
digitalWrite(A0, LOW);
}
void loop()
{
//only read temp every 100 cycles
if(count%50 == 0) {
temperature = get_thermistor();
count = 0;
}
count ++;
displayTemperature();
Serial.print(temperature);
if (temperature > 29.0 ) {
digitalWrite(A0, HIGH);
Serial.println(" : > 28");
} else {
digitalWrite(A0, LOW);
Serial.println(" : < 28");
}
//delay(1000);
}
void displayTemperature() {
clearLEDs();
pickDigit(1);
pickNumber(tens(temperature));// get the value of thousand
delay(del);//delay 5ms
clearLEDs();
pickDigit(2);
dispDec(2);
pickNumber(ones(temperature));// get the value of hundred
delay(del);//delay 5ms
clearLEDs();
pickDigit(3);
//dispDec(3);
pickNumber(decimal(temperature));//get the value of ten
delay(del);//delay 5ms
clearLEDs();
pickDigit(4);
cee(); //degré celsius symbol
delay(del);//delay 5ms
}
void pickDigit(int x) //light up a 7-segment display
{
//The 7-segment LED display is a common-cathode one. So also use digitalWrite to set d1 as high and the LED will go out
digitalWrite(d1, HIGH);
digitalWrite(d2, HIGH);
digitalWrite(d3, HIGH);
digitalWrite(d4, HIGH);
switch(x)
{
case 1:
digitalWrite(d1, LOW);//Light d1 up
break;
case 2:
digitalWrite(d2, LOW); //Light d2 up
break;
case 3:
digitalWrite(d3, LOW); //Light d3 up
break;
default:
digitalWrite(d4, LOW); //Light d4 up
break;
}
}
void pickNumber(int x) //define pickNumber(x)to display number x
{
switch(x)
{
default:
zero();
break;
case 1:
one();
break;
case 2:
two();
break;
case 3:
three();
break;
case 4:
four();
break;
case 5:
five();
break;
case 6:
six();
break;
case 7:
seven();
break;
case 8:
eight();
break;
case 9:
nine();
break;
}
}
void dispDec(int x) //концигурация анодов для вывода точки
{
digitalWrite(p, HIGH);
}
void clearLEDs() //Функция очищающая табло, устанавливает низкий уровень на всех анодах
{
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
digitalWrite(p, LOW);
}
void zero() //конфигурация анодов для вывода нуля
{
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, LOW);
}
void one() //концигурация анодов для вывода единицы
{
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
}
void two() // define 2 as cathode pin switch
{
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, LOW);
digitalWrite(g, HIGH);
}
void three() // define 3 as cathode pin switch
{
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, HIGH);
}
void four() // define 4 as cathode pin switch
{
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
}
void five() // define 5 as cathode pin switch
{
digitalWrite(a, HIGH);
digitalWrite(b, LOW);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, LOW);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
}
void six() // define 6 as cathode pin switch
{
digitalWrite(a, HIGH);
digitalWrite(b, LOW);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
}
void seven() // define 7 as cathode pin switch
{
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
}
void eight() // define 8 as cathode pin switch
{
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
}
void nine() // define 9 as cathode pin switch
{
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, LOW);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
}
void cee()
{
digitalWrite(a, HIGH);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, LOW);
}
int tens(float x)
{
float divided = x/10.0;
return (int)divided;
}
int ones(float x)
{
float divided = x - (10.0 * tens(x));
return (int)divided;
}
int decimal(float x)
{
return int((x - ((int) x))*10);
}
double get_thermistor()
{
int thermistor_adc_val;
double output_voltage, thermistor_resistance, therm_res_ln, temperature;
thermistor_adc_val = analogRead(thermistor_output);
output_voltage = ( (thermistor_adc_val * 5.0) / 1023.0 );
thermistor_resistance = ( ( 5 * ( 10.0 / output_voltage ) ) - 10 ); /* Resistance in kilo ohms */
thermistor_resistance = thermistor_resistance * 1000 ; /* Resistance in ohms */
therm_res_ln = log(thermistor_resistance);
/* Steinhart-Hart Thermistor Equation: */
/* Temperature in Kelvin = 1 / (A + B[ln(R)] + C[ln(R)]^3) */
/* where A = 0.001129148, B = 0.000234125 and C = 8.76741*10^-8 */
temperature = ( 1 / ( 0.001129148 + ( 0.000234125 * therm_res_ln ) + ( 0.0000000876741 * therm_res_ln * therm_res_ln * therm_res_ln ) ) ); /* Temperature in Kelvin */
temperature = temperature - 273.15; /* Temperature in degree Celsius */
//Serial.print("Temperature in degree Celsius = ");
//Serial.print(temperature);
//Serial.print("\t\t");
//Serial.print("Resistance in ohms = ");
//Serial.print(thermistor_resistance);
//Serial.print("\n\n");
return temperature;
}
int points(float x) //not used
{
float divided = x - ((10.0 * tens(x)) + ones(x));
Serial.println(divided);
divided = divided*10;
Serial.println(divided);
divided = round(divided);
Serial.println(divided);
delay(1000);
return int(divided);
}