Advertisement
claudiusmarius

DS1803_ATtiny85

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