Advertisement
claudiusmarius

DoublePotentiometreNumerique

Jul 9th, 2023
898
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.66 KB | None | 0 0
  1.  // ===========================================================================================================================================
  2.   // Double potentiomètre numérique utilisant un DS1803 et un ATtiny85
  3.   // Par Claude DUFOURMONT
  4.   // SVP : N'OUBLIEZ PAS DE LIKER LA VIDEO ET DE VOUS ABONNER - MERCI
  5.   // Chaine YouTube : https://www.youtube.com/c/ClaudeDufourmont
  6.   // Vidéo : https://youtu.be/2uEe2bbEKXY
  7.   // ===========================================================================================================================================
  8.   // Datasheet DS1803 : https://www.analog.com/media/en/technical-documentation/data-sheets/ds1803.pdf
  9.   // Pour info : le DS1803 possède 256 positions.(2 fois).
  10.   // IMPERATIF : NOK si pas de résistances PULL UP Sur SDA et SCK
  11.   // on fait des bonds superieurs ou égaux à 19 mV (5000/255).
  12.   // ===============================================Standard Resistance Values DEBUT=============================================================
  13.   // DS1803-010 10kΩ
  14.   // DS1803-050 50kΩ
  15.   // DS1803-100 100kΩ
  16.   // =================================================Standard Resistance Values FIN==============================================================
  17.  
  18.   // ====================================================ADRESSES DEBUT===========================================================================
  19.   //  A2  A1  A0  Adresse I2C
  20.   //  0   0   0   0x28
  21.   //  0   0   1   0x29
  22.   //  0   1   0   0x2A
  23.   //  0   1   1   0x2B
  24.   //  1   0   0   0x2C
  25.   //  1   0   1   0x2D
  26.   //  1   1   0   0x2E
  27.   //  1   1   1   0x2F
  28.   // =====================================================ADRESSES FIN============================================================================
  29.  
  30.   //  ===================================================WIRE COMMAND WORDS DEBUT=================================================================  
  31.   //  COMMAND                   COMMAND VALUE
  32.   //  Write Potentiometer-0     10101001
  33.   //  Write Potentiometer-1     10101010
  34.   //  Write Both Potentiometers 10101111
  35.   //  IMPORTANT : mettre un B devant le mot pour bien faire apparaître qu'il sagit d'un Byte, un Octet
  36.   //  =====================================================WIRE COMMAND WORDS FIN=================================================================
  37.  
  38.   // Système volatiles : pas de mémorisation des dernières valeurs suite à coupure
  39.    
  40.   #include <TinyWireM.h>
  41.   #include <Adafruit_NeoPixel.h>
  42.  
  43.   #define DS1803_ADDRESS 0x2F                       // A0,A1,A2 de DS1803 = HIGH
  44.   #define VR A3
  45.   #define LED_PIN 1
  46.   #define BrocheBuzzer 4
  47.   #define NUM_LEDS 16
  48.   #define BRIGHTNESS 4
  49.   Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
  50.  
  51.   int positionPot0 = 0;
  52.   int positionPot1 = 0;
  53.  
  54.   float Vselect;
  55.  
  56.   bool UPP0 = LOW;
  57.   bool UPP1 = LOW;
  58.   bool DOWNP0 = LOW;
  59.   bool DOWNP1 = LOW;
  60.   bool RAS = HIGH;
  61.  
  62.   void setup()
  63.   {
  64.   strip.begin();
  65.   strip.setBrightness(BRIGHTNESS);
  66.  
  67.   TinyWireM.begin();
  68.  
  69.   pinMode (VR, INPUT);
  70.   pinMode (BrocheBuzzer, OUTPUT);
  71.   delay(300);
  72.    
  73.   strip.begin();
  74.   strip.clear();
  75.   strip.show();
  76.   for (int i = 0; i < NUM_LEDS; i++)
  77.   {
  78.   strip.setPixelColor(i, strip.Color(0, 0, 0));
  79.   }
  80.   strip.show(); // Afficher les modifications sur la barre de LEDs
  81.   delay(300);
  82.  
  83.   digitalWrite (BrocheBuzzer, HIGH);
  84.   delay(10);
  85.   digitalWrite (BrocheBuzzer, LOW);
  86.   delay(80);
  87.  
  88.   digitalWrite (BrocheBuzzer, HIGH);
  89.   delay(10);
  90.   digitalWrite (BrocheBuzzer, LOW);
  91.   delay(80);
  92.  
  93.   digitalWrite (BrocheBuzzer, HIGH);
  94.   delay(10);
  95.   digitalWrite (BrocheBuzzer, LOW);
  96.   delay(80);
  97.   }
  98.  
  99.   void loop()
  100.   {
  101.   if (positionPot0 <1)
  102.   {
  103.   INIT0();
  104.   }
  105.  
  106.   if (positionPot1 <1)
  107.   {
  108.   INIT1();
  109.   }
  110.  
  111.   Vselect = analogRead(A3);
  112.  
  113.   if (Vselect <200)
  114.   {
  115.   UPP0 = HIGH;
  116.   DOWNP0 = RAS = UPP1 = DOWNP1 = LOW;
  117.   delay(10);
  118.   }
  119.  
  120.   if (Vselect >= 200 && Vselect <280)
  121.   {
  122.   DOWNP0 = HIGH;
  123.   UPP0 = RAS = UPP1 = DOWNP1 = LOW;
  124.   delay(5);
  125.   }
  126.  
  127.   if (Vselect >= 280 && Vselect <540)
  128.   {
  129.   RAS = HIGH;
  130.   UPP0 = UPP1 = DOWNP0 = DOWNP1 = LOW;
  131.   delay(5);
  132.   }
  133.  
  134.   if (Vselect >= 540 && Vselect <620)
  135.   {
  136.   DOWNP1 = HIGH;
  137.   UPP0 = RAS = DOWNP0 = UPP1 = LOW;
  138.   delay(5);
  139.   }
  140.  
  141.   if (Vselect >= 620 && Vselect <690)
  142.   {
  143.   RAS = HIGH;
  144.   UPP0 = DOWNP0 = UPP1 = DOWNP1 = LOW;
  145.   delay(5);
  146.   }
  147.  
  148.   if (Vselect >= 690 && Vselect <760)
  149.   {
  150.   UPP1 = HIGH;
  151.   UPP0 = DOWNP1 = RAS = DOWNP0 = LOW;
  152.   delay(5);
  153.   }
  154.  
  155.   if (Vselect >= 760)
  156.   {
  157.   RAS = HIGH;
  158.   UPP0 = DOWNP0 = UPP1 = DOWNP1 = LOW;
  159.   delay(5);
  160.   }
  161.  
  162.   if (UPP0 == HIGH)
  163.   {
  164.   DOWNP0 = RAS = UPP1 = DOWNP1 = LOW;
  165.   incrementerPosition0();
  166.   UPP0=LOW;
  167.   }
  168.  
  169.   if (DOWNP0 == HIGH)
  170.   {
  171.   UPP0 = RAS = UPP1 = DOWNP1 = LOW;
  172.   decrementerPosition0();
  173.   DOWNP0 = LOW;
  174.   }
  175.  
  176.   if (UPP1 == HIGH)
  177.   {
  178.   UPP0 = RAS = DOWNP0 = DOWNP1 = LOW;
  179.   incrementerPosition1();
  180.   UPP1 =LOW;
  181.   }
  182.  
  183.   if (DOWNP1 == HIGH)
  184.   {
  185.   UPP0 = UPP1 = RAS = DOWNP0=LOW;
  186.   decrementerPosition1();
  187.   DOWNP1 =LOW;
  188.   }
  189.   }
  190.  
  191.   void incrementerPosition0()
  192.   {
  193.   if (positionPot0 < 255)
  194.   {
  195.   positionPot0++;
  196.   delay(200);
  197.   TinyWireM.beginTransmission(DS1803_ADDRESS);
  198.   TinyWireM.send(B10101001);
  199.   TinyWireM.send(positionPot0);
  200.   MAJBarrette0();
  201.   TinyWireM.endTransmission();
  202.   }
  203.   }
  204.  
  205.   void decrementerPosition0()
  206.   {
  207.   if (positionPot0 > 0)
  208.   {
  209.   positionPot0--;
  210.   delay(200);
  211.   TinyWireM.beginTransmission(DS1803_ADDRESS);
  212.   TinyWireM.send(B10101001);
  213.   TinyWireM.send(positionPot0);
  214.   MAJBarrette0();
  215.   TinyWireM.endTransmission();
  216.   }
  217.   }
  218.  
  219.   void incrementerPosition1()
  220.   {
  221.   if (positionPot1 < 255)
  222.   {
  223.   positionPot1++;
  224.   delay(200);
  225.   TinyWireM.beginTransmission(DS1803_ADDRESS);
  226.   TinyWireM.send(B10101010);
  227.   TinyWireM.send(positionPot1);
  228.   MAJBarrette1();
  229.   TinyWireM.endTransmission();
  230.   }
  231.   }
  232.  
  233.   void decrementerPosition1()
  234.   {
  235.   if (positionPot1 > 0)
  236.   {
  237.   positionPot1--;
  238.   delay(200);
  239.   TinyWireM.beginTransmission(DS1803_ADDRESS);
  240.   TinyWireM.send(B10101010);
  241.   TinyWireM.send(positionPot1);
  242.   MAJBarrette1();
  243.   TinyWireM.endTransmission();
  244.   }
  245.   }
  246.  
  247.   void MAJBarrette0()
  248.   {
  249.   for (int i = 0; i < 8; i++)
  250.   {
  251.   if (positionPot0 & (1 << (7 - i)))
  252.   {
  253.   strip.setPixelColor(i, strip.Color(0, 255, 0)); // Allumer la LED correspondante en vert
  254.   }
  255.   else
  256.   {
  257.   strip.setPixelColor(i, strip.Color(0, 0, 0)); // Éteindre la LED
  258.   }
  259.   }
  260.   strip.show(); // Afficher les modifications sur la barre de LEDs
  261.   delay(300);
  262.   }
  263.  
  264.   void MAJBarrette1()
  265.   {
  266.   for (int i = 8; i < NUM_LEDS; i++)
  267.   {
  268.  
  269.   if (positionPot1 & (1 << (15 - i)))
  270.   {
  271.   strip.setPixelColor(i, strip.Color(255, 0, 0)); // Allumer la LED correspondante en vert
  272.   }
  273.   else
  274.   {
  275.   strip.setPixelColor(i, strip.Color(0, 0, 0)); // Éteindre la LED
  276.   }
  277.   }
  278.   strip.show(); // Afficher les modifications sur la barre de LEDs
  279.   delay(300);
  280.   }
  281.  
  282.   void INIT0 ()
  283.   {
  284.   positionPot0 = 0;
  285.   TinyWireM.beginTransmission(DS1803_ADDRESS);
  286.   TinyWireM.send(B10101001);
  287.   TinyWireM.send(positionPot0);
  288.   TinyWireM.endTransmission();
  289.   for (int i = 0; i <7; i++)
  290.   {
  291.   strip.setPixelColor( i, strip.Color(0, 0, 0)); // Éteindre la LED
  292.   strip.show(); // Afficher les modifications sur la barre de LEDs
  293.   }
  294.   delay(300);
  295.   }
  296.  
  297.   void INIT1 ()
  298.   {
  299.   positionPot1 = 0;
  300.   TinyWireM.beginTransmission(DS1803_ADDRESS);
  301.   TinyWireM.send(B10101010);
  302.   TinyWireM.send(positionPot1);
  303.   TinyWireM.endTransmission();
  304.   for (int i = 8; i <15; i++)
  305.   {
  306.   strip.setPixelColor( i, strip.Color(0, 0, 0)); // Éteindre la LED
  307.   strip.show(); // Afficher les modifications sur la barre de LEDs
  308.   }
  309.   delay(300);
  310.   }
  311.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement