code_arduino.ino
2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
int incomingByte = 0; // for incoming serial data
int i;
char value[3];
char lengh[4];
float temperatureC;
int led1 = 2;
int led2 = 3;
int ledstate1 = 0;
int ledstate2 = 0;
unsigned long previousMillis = 0;
const long interval = 1000;
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
}
void loop()
{
if (Serial.available() > 0)
{
incomingByte = Serial.read();
if (incomingByte == 0)
{
int sensorValue = analogRead(A0);
int n = (log10(sensorValue) + 1); // nombre de chiffres composant le nombre
itoa(n, lengh, 10);
itoa(sensorValue, value, 10);
strcat(lengh,value);
Serial.println(lengh);
}
else if (incomingByte == 1)
{
int sensorValue = analogRead(A1);
int n = (log10(sensorValue) + 1); // nombre de chiffres composant le nombre
itoa(n, lengh, 10);
itoa(sensorValue, value, 10);
strcat(lengh,value);
Serial.println(lengh);
}
else if (incomingByte == 2)
{
int sensorValue = analogRead(A2);
int n = (log10(sensorValue) + 1); // nombre de chiffres composant le nombre
itoa(n, lengh, 10);
itoa(sensorValue, value, 10);
strcat(lengh,value);
Serial.println(lengh);
}
else if (incomingByte == 3)
{
int sensorValue = ledstate1;
int n = (log10(sensorValue) + 1);
itoa(n, lengh, 10);
itoa(sensorValue, value, 10);
strcat(lengh,value);
Serial.println(lengh);
}
else if (incomingByte == 4)
{
int sensorValue = ledstate2;
int n = (log10(sensorValue) + 1);
itoa(n, lengh, 10);
itoa(sensorValue, value, 10);
strcat(lengh,value);
Serial.println(lengh);
}
}
////////////////////////////////////
//FIN DE LA PARTIE DE COM SERIE
////////////////////////////////////
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval)
{
int valeur = analogRead(A2);
float tension = valeur * 5.0;
tension /= 1024.0;
float temperature = ((tension * 1000) - 500) / 10;
previousMillis = currentMillis;
Serial.println(temperature);
if (temperature < 27)
{
digitalWrite(led2, HIGH);
ledstate2=1;
}
else
{
digitalWrite(led2, LOW);
ledstate2=0;
}
if (analogRead(A1) < 300)
{
digitalWrite(led1, HIGH);
ledstate1=1;
}
else
{
digitalWrite(led1, LOW);
ledstate1=0;
}
}
}