Bonjour,

J’essaye de créer un script relativement simple pour allumer et éteindre des LED en fonction de l’heure fourni par un module RTC. Ca fonctionne bien quand l’heure est définie dans void setup(), mais j’aimerais être en mesure de changer l’heure programmée via Matlab. Et ça, ça ne fonctionne pas. J’ai essayé plusieurs solutions trouvées sur le net dans des forums, mais quelque chose m’échappe. Mon manque de connaissance de l’environnement Arduino (et Matlab ?) m’empêche de trouver ce qui cloche et ce que je dois faire pour résoudre le problème.

En quelques mots j’essaye d’envoyer quatre valeurs (heures et minutes pour allumer et éteindre les LED) via serial. Le code posté ici est de toute évidence incorrect, mais c’est la dernière version en date après de multiple essais.

De manière ultime, j’aimerais aussi pouvoir envoyer de l’info de l’Arduino vers Matlab pour mettre à jour en temps réel les valeurs des heures et minutes.

Voici mon code Arduino :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "Wire.h"
#define DS3231_I2C_ADDRESS 0x68
 
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val) {
  return( (val/10*16) + (val%10) );
}
 
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val) {
  return( (val/16*10) + (val%16) );
}
 
// Renames digital pins 2, 4 and 7 as LED1, LED2 and redLED
int LED1 = 2;  // For controlling white LED1, using pin2 as an output
int LED2 = 4;  // For controlling white LED2, using pin4 as an output
int redLED = 7;  // For controlling red LED, using pin7 as an output
 
// Defines L/D cycle (no "0" before values < 10)
int hourON = 19; // Light ON at XX:.. (hour)
int minuteON = 8; // Light ON at ..:XX (minute)
int hourOFF = 19; // Light OFF at XX:.. (hour)
int minuteOFF = 9; // Light OFF at ..:XX (minute)
 
// Variables used to initiate the display time every second on serial monitor
unsigned long timer; unsigned long timer_ref = 0;
 
int answer[4];
 
void setup() {
  Wire.begin();
  Serial.begin(9600);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(redLED, OUTPUT);
}
 
void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year) {
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set DS3231 register pointer to 00h
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  // Request seven bytes of data from DS3231 starting from register 00h
  *second = bcdToDec(Wire.read() & 0x7f);
  *minute = bcdToDec(Wire.read());
  *hour = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month = bcdToDec(Wire.read());
  *year = bcdToDec(Wire.read());  
}
 
void LDcycle() {
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  // Retrieve data from DS3231
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
  &year);
  if (hour == hourON && minute == minuteON) {
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, HIGH);
    digitalWrite(redLED, LOW);
  }
  if (hour == hourOFF && minute == minuteOFF) {
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, LOW);
    digitalWrite(redLED, HIGH);
  }
}
 
void displayTime() {
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  // Retrieve data from DS3231
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
  &year);
  // Send it to the serial monitor
  Serial.print(hour, DEC);
  // Convert the byte variable to a decimal number when displayed
  Serial.print(":");
  if (minute<10) {
    Serial.print("0");
  }
  Serial.print(minute, DEC);
  Serial.print(":");
  if (second<10) {
    Serial.print("0");
  }
  Serial.print(second, DEC);
  Serial.print(" ");
  Serial.print(dayOfMonth, DEC);
  Serial.print("/");
  Serial.print(month, DEC);
  Serial.print("/");
  Serial.print(year, DEC);
  Serial.print(" Day of week: ");
  switch(dayOfWeek) {
  case 1:
    Serial.println("Sunday");
    break;
  case 2:
    Serial.println("Monday");
    break;
  case 3:
    Serial.println("Tuesday");
    break;
  case 4:
    Serial.println("Wednesday");
    break;
  case 5:
    Serial.println("Thursday");
    break;
  case 6:
    Serial.println("Friday");
    break;
  case 7:
    Serial.println("Saturday");
    break;
  }
}
 
void loop() {
  LDcycle();
  timer = millis();
  if (timer - timer_ref >= 1000) { // Every second,
     displayTime(); // display the real-time clock data on the Serial Monitor
     timer_ref = millis();
  }
 
  if (Serial.available() > 0) {
    for (int i=0; i<4; i++) {
      answer[i] = Serial.parseInt();
    }
 
  hourON = answer[0];
  minuteON = answer[1];
  hourOFF = answer[2];
  minuteOFF = answer[3];
  }
}
Et mon code Matlab :

Code MATLAB : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
clc
instrreset
 
arduino=serial('COM8','BaudRate',9600);
 
fopen(arduino); % initiate arduino communication
 
prompt = {'Hour ON','Min ON','Hour OFF','Min OFF'};
dlg_title = 'Input';
num_lines = 1;
defaultans = {'21','0','9','0'};
answer = inputdlg(prompt,dlg_title,num_lines,defaultans);
 
full_answer = [answer{1},',',answer{2},',',answer{3},',',answer{4}];
 
fprintf(arduino,'%s',char(full_answer));
 
fclose(arduino); % end communication with arduino

Merci beaucoup pour votre aide...