Advertisement
Ollivier

Ajustement horloge DeLorean pas BP

Apr 4th, 2020
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.47 KB | None | 0 0
  1.  
  2. // --------------------------------------------------------------------------------
  3. // Ecran BACK TO THE FUTURE _ Ajustement par BP
  4. // --------------------------------------------------------------------------------
  5. // Objectif : créer un affichage type "Retour vers le Futur"
  6. // https://media.giphy.com/media/dgEIhYAo3lZiE/giphy.gif
  7. // avec possibilité d'ajuster la date avec des boutons poussoirs
  8. // A ma disposition :
  9. // https://www.elecfreaks.com/learn-en/microbitOctopus/sensor/octopus_ef04056.html
  10. // Un mini clavier 5 boutons poussoirs DFR0075
  11. // https://wiki.dfrobot.com/ADKeyboard_Module__SKU__DFR0075_
  12. // sur Arduino Mega
  13. // ---------------------------------------------------------------------------------
  14.  
  15. // --------------------------------------------
  16. // Instructions de montage --------------------
  17. // --------------------------------------------
  18. // Afficheur 7 digits 4 segments OCTOPUS EF04056
  19. // --------------------------------------------
  20. // D14 (CLK)
  21. // D15 (DIO)
  22. // 5V
  23. // GND
  24. //
  25. // --------------------------------------------
  26. // ADKeyboard DFR0075
  27. // --------------------------------------------
  28. // A0
  29. // 5V
  30. // Gnd
  31. // --------------------------------------------
  32.  
  33. // Chargement des librairies nécessaires -------------------------------------------
  34.  
  35. #include <Wire.h>                           // Librairie de câblage simplifié (??)
  36. #include <TM1637Display.h>                  // Librairie d'affichage pour afficheur LCD OCTOPUS EF04056
  37.  
  38. // --------------------------------------------------------------------------------------------------------
  39. // Afficheur LCD Octopus EF04056
  40. // --------------------------------------------------------------------------------------------------------
  41.  
  42. // on définit le pin de connexion sur lequel est CLK
  43. #define CLK 14
  44. // on définit le pin de connexion sur lequel est DIO
  45. #define DIO 15
  46.  
  47. int annee = 1956;
  48. uint8_t positionPoint ;
  49. const uint8_t posPoint[4] = {0b10000000 , 0b01000000, 0b00100000, 0b00010000};
  50. int curseur = 0;
  51.  
  52. // on initialise l'afficheur
  53. TM1637Display display(CLK, DIO);
  54.  
  55. // --------------------------------------------------------------------------------------------------------
  56. // AD keyboard DFR0075
  57. // --------------------------------------------------------------------------------------------------------
  58.  
  59. int adc_key_val[5] = {600, 650, 700,  800, 900 };
  60. int NUM_KEYS = 5;
  61. int adc_key_in;
  62. int key = -1;
  63. int oldkey = -1;
  64.  
  65. void setup() {
  66.  
  67.   Serial.begin(9600);               // on initialise et synchronise la communication entre le PC et Arduino
  68.   display.clear();
  69.   display.setBrightness(1);
  70.   display.showNumberDecEx(annee, false, false, 4, 0);
  71.  
  72. }
  73.  
  74. void loop() {
  75.  
  76.   ajustAffichage();
  77. }
  78.  
  79. // --------------------------------------------------------------------------------------------------------
  80. // - Fonctions -------------------------------------------------------------------------------- Fonctions -
  81. // --------------------------------------------------------------------------------------------------------
  82.  
  83. void ajustAffichage() {
  84.   adc_key_in = analogRead(0);    // read the value from the sensor
  85.   digitalWrite(13, LOW);
  86.   key = get_key(adc_key_in);  // convert into key press
  87.  
  88.   if (key != oldkey)   // if keypress is detected
  89.   {
  90.     delay(50);  // wait for debounce time
  91.     adc_key_in = analogRead(0);    // read the value from the sensor
  92.     key = get_key(adc_key_in);    // convert into key press
  93.     if (key != oldkey)
  94.     {
  95.       oldkey = key;
  96.       if (key >= 0) {
  97.         digitalWrite(13, HIGH);
  98.         switch (key)
  99.         {
  100.           case 0: Serial.println("S1 OK");
  101.             aGauche ();
  102.             break;
  103.           case 1: Serial.println("S2 OK");
  104.             moinsUn ();
  105.             break;
  106.           case 2: Serial.println("S3 OK");
  107.             aDroite();
  108.             break;
  109.           case 3: Serial.println("S4 OK");
  110.             plusUn();
  111.             break;
  112.           case 4: Serial.println("S5 OK");
  113.             afficheAnnee();
  114.             break;
  115.         }
  116.       }
  117.     }
  118.   }
  119.   delay(100);
  120. }
  121.  
  122. void afficheAnnee (){
  123.  
  124.   // on efface les donnée à afficher
  125.   display.clear();
  126.  
  127.   // on règle la luminosité à la moitié
  128.   display.setBrightness(3);
  129.  
  130.   // on détermine la position du curseur en marquant le point
  131.   positionPoint = posPoint[0];
  132.   curseur=0;
  133.  
  134.   // on affiche
  135.   display.showNumberDecEx(annee, positionPoint, false, 4, 0);
  136. }
  137.  
  138. void aDroite (){
  139.    // on efface les donnée à afficher
  140.   display.clear();
  141.  
  142.   // on règle la luminosité à la moitié
  143.   display.setBrightness(3);
  144.  
  145.   // on détermine la position du curseur en marquant le point
  146.   if (curseur < 3){
  147.     curseur += 1;
  148.   }else{
  149.     curseur = 0;
  150.   }
  151.   positionPoint = posPoint[curseur];
  152.  
  153.   // on affiche
  154.   display.showNumberDecEx(annee, positionPoint, false, 4, 0);
  155. }
  156.  
  157. void aGauche (){
  158.    // on efface les donnée à afficher
  159.   display.clear();
  160.  
  161.   // on règle la luminosité à la moitié
  162.   display.setBrightness(3);
  163.  
  164.   // on détermine la position du curseur en marquant le point
  165.   if (curseur > 0){
  166.     curseur -= 1;
  167.   }else{
  168.     curseur = 3;
  169.   }
  170.   positionPoint = posPoint[curseur];
  171.  
  172.   // on affiche
  173.   display.showNumberDecEx(annee, positionPoint, false, 4, 0);
  174. }
  175.  
  176. void plusUn () {
  177.   // on efface les donnée à afficher
  178.   display.clear();
  179.  
  180.   // on règle la luminosité à la moitié
  181.   display.setBrightness(3);
  182.  
  183.   // on détermine la position du curseur en marquant le point
  184.   if (curseur == 3){
  185.     annee += 1;
  186.   } else if (curseur == 2){
  187.     annee += 10;
  188.   } else if (curseur == 1){
  189.     annee += 100;
  190.   } else if (curseur == 0){
  191.     annee += 1000;
  192.   }
  193.   positionPoint = posPoint[curseur];
  194.  
  195.   // on affiche
  196.   display.showNumberDecEx(annee, positionPoint, false, 4, 0);
  197. }
  198.  
  199. void moinsUn () {
  200.   // on efface les donnée à afficher
  201.   display.clear();
  202.  
  203.   // on règle la luminosité à la moitié
  204.   display.setBrightness(3);
  205.  
  206.   // on détermine la position du curseur en marquant le point
  207.   if (curseur == 3){
  208.     annee -= 1;
  209.   } else if (curseur == 2){
  210.     annee -= 10;
  211.   } else if (curseur == 1){
  212.     annee -= 100;
  213.   } else if (curseur == 0){
  214.     annee -= 1000;
  215.   }
  216.   positionPoint = posPoint[curseur];
  217.  
  218.   // on affiche
  219.   display.showNumberDecEx(annee, positionPoint, false, 4, 0);
  220. }
  221.  
  222. int get_key(unsigned int input)
  223. {
  224.   int k;
  225.   for (k = 0; k < NUM_KEYS; k++)
  226.   {
  227.     if (input < adc_key_val[k])
  228.     {
  229.       return k;
  230.     }
  231.   }
  232.   if (k >= NUM_KEYS)k = -1;  // No valid key pressed
  233.   return k;
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement