Testeur.ino 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #include "My_Eeprom.h"
  2. #include <EEPROM.h>
  3. #include "Testeur.h"
  4. #include <Wire.h>
  5. newPin tabPin[NBPIN];
  6. int pinTECBA = 22;
  7. unsigned long valRandom;
  8. void setup() {
  9. Serial.begin(115200);
  10. Wire.begin();
  11. initTabPin();
  12. pinMode(pinTECBA,OUTPUT);
  13. valRandom = (long)1000*60*MINUTE/DELAYLOOP;
  14. randomSeed(analogRead(0));
  15. }
  16. void loop() {
  17. for(int i = 0; i<NBPIN;i++){
  18. toogle(i);
  19. }
  20. NiveauBas();//TODO niveau bas tech
  21. if (Serial.available() > 0) {
  22. int incomingByte = Serial.read();
  23. if(incomingByte=='A'){
  24. AfficheEEprom();
  25. }
  26. if(incomingByte=='E'){
  27. erase();
  28. }
  29. if(incomingByte=='I'){
  30. instantane();
  31. }
  32. }
  33. delay(DELAYLOOP);
  34. }
  35. /* pinBRAS1, pinBRAS2, pinOSMOL, pinCHAUF, pinECUME, pinREMON, pinLUMIE
  36. pinTECBA, pinTECHA, pinCUVAU, pinRESBA */
  37. void initTabPin(void){
  38. for(int i = 0; i<NBPIN;i++){
  39. tabPin[i].num=i*2+23;
  40. pinMode(tabPin[i].num,INPUT);
  41. tabPin[i].last = digitalRead(tabPin[i].num);
  42. }
  43. }
  44. uint8_t toogle(uint8_t pin){
  45. uint8_t val = digitalRead(tabPin[pin].num);
  46. if(tabPin[pin].last!=val){
  47. Serial.println(pin);
  48. tabPin[pin].last = val;
  49. switch(pin){
  50. case 0:
  51. fc_BRAS1(val);
  52. break;
  53. case 1:
  54. fc_BRAS2(val);
  55. break;
  56. case 2:
  57. fc_OSMO(val);
  58. break;
  59. case 3:
  60. fc_CHAUFF(val);
  61. break;
  62. case 4:
  63. fc_ECUM(val);
  64. break;
  65. case 5:
  66. fc_REMO(val);
  67. break;
  68. case 6:
  69. fc_LUMI(val);
  70. break;
  71. case 7:
  72. fc_TECBA(val);
  73. break;
  74. case 8:
  75. fc_TECHA(val);
  76. break;
  77. case 9:
  78. fc_CUVAU(val);
  79. break;
  80. case 10:
  81. fc_RESBA(val);
  82. break;
  83. }
  84. return 1;
  85. }
  86. return 0;
  87. }
  88. void NiveauBas(void){
  89. unsigned long tempsActuel = millis();
  90. // si le signal est actuellement haut, l'éteindre lorsque le temps imparti est écoulé
  91. if (digitalRead(pinTECBA) == HIGH && tempsActuel - tempsSignal >= intervalleMinimum) {
  92. digitalWrite(pinTECBA, LOW);
  93. }
  94. // si le signal est actuellement bas, attendre un temps aléatoire avant de le mettre en marche
  95. if (digitalRead(pinTECBA) == LOW && tempsActuel - tempsPrecedent >= tempsAttente) {
  96. tempsSignal = tempsActuel;
  97. digitalWrite(pinTECBA, HIGH);
  98. tempsAttente = random(intervalleAttenteMinimum, intervalleAttenteMaximum + 1); // temps d'attente aléatoire avant le prochain signal
  99. }
  100. // si le signal a été éteint, attendre 1 seconde avant de recommencer
  101. if (digitalRead(pinTECBA) == LOW && tempsActuel - tempsPrecedent >= 1000) {
  102. tempsPrecedent = tempsActuel;
  103. }
  104. // si le signal a été allumé, attendre un temps aléatoire avant de l'éteindre
  105. if (digitalRead(pinTECBA) == HIGH && tempsActuel - tempsSignal < intervalleMinimum) {
  106. tempsPrecedent = tempsActuel;
  107. }
  108. // si le signal a été allumé pendant le temps imparti, attendre un temps aléatoire avant de recommencer
  109. if (digitalRead(pinTECBA) == HIGH && tempsActuel - tempsSignal >= intervalleMinimum) {
  110. digitalWrite(pinTECBA, LOW);
  111. tempsPrecedent = tempsActuel;
  112. nbSignaux++;
  113. if (nbSignaux >= 5) {
  114. while (true) {} // arrêter le programme
  115. }
  116. tempsAttente = random(intervalleAttenteMinimum, intervalleAttenteMaximum + 1); // temps d'attente aléatoire avant le prochain signal
  117. }
  118. }
  119. void fc_BRAS1(int val) {
  120. if(val){
  121. Enregistre(BR1ON);
  122. }
  123. else{
  124. Enregistre(BR1OF);
  125. }
  126. }
  127. void fc_BRAS2(int val) {
  128. if(val){
  129. Enregistre(BR2ON);
  130. }
  131. else{
  132. Enregistre(BR2OF);
  133. }
  134. }
  135. void fc_OSMO(int val) {
  136. if(val){
  137. Enregistre(OSMON);
  138. }
  139. else{
  140. Enregistre(OSMOF);
  141. }
  142. }
  143. void fc_CHAUFF(int val) {
  144. if(val){
  145. Enregistre(CHAON);
  146. }
  147. else{
  148. Enregistre(CHAOF);
  149. }
  150. }
  151. void fc_ECUM(int val) {
  152. if(val){
  153. Enregistre(ECUON);
  154. }
  155. else{
  156. Enregistre(ECUOF);
  157. }
  158. }
  159. void fc_REMO(int val) {
  160. if(val){
  161. Enregistre(REMON);
  162. }
  163. else{
  164. Enregistre(REMOF);
  165. }
  166. }
  167. void fc_LUMI(int val) {
  168. if(val){
  169. Enregistre(LUMON);
  170. }
  171. else{
  172. Enregistre(LUMOF);
  173. }
  174. }
  175. void fc_TECBA(int val) {
  176. if(val){
  177. Enregistre(TEBON);
  178. }
  179. else{
  180. Enregistre(TEBOF);
  181. }
  182. }
  183. void fc_TECHA(int val) {
  184. if(val){
  185. Enregistre(TEHON);
  186. }
  187. else{
  188. Enregistre(TEHOF);
  189. }
  190. }
  191. void fc_CUVAU(int val) {
  192. if(val){
  193. Enregistre(CUHON);
  194. }
  195. else{
  196. Enregistre(CUHOF);
  197. }
  198. }
  199. void fc_RESBA(int val) {
  200. if(val){
  201. Enregistre(REBON);
  202. }
  203. else{
  204. Enregistre(REBOF);
  205. }
  206. }