Advertisement
CuriousScientist

Arduino menu with rotary encoder

Jul 23rd, 2020 (edited)
2,324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //If you found my video helpful, please SUBSCRIBE: https://www.youtube.com/c/CuriousScientist?sub_confirmation=1
  2. //The code belongs to this tutorial video: https://youtu.be/x2J4VAYQGh0
  3. //Please visit https://curiousscientist.tech
  4.  
  5. //16x2 LCD
  6. #include <LiquidCrystal_I2C.h> //SDA = A4, SCL = A5
  7. LiquidCrystal_I2C lcd(0x27, 16, 2);
  8.  
  9.  
  10. //Defining pins for rotary encoder
  11. const int RotaryCLK = 2; //CLK pin on the rotary encoder
  12. const int RotaryDT = 4; //DT pin on the rotary encoder
  13. const int RotarySW = 3; //SW pin on the rotary encoder (Button function)
  14.  
  15. //Defining variables for rotary encoder and button
  16. int ButtonCounter = 0; //counts the button clicks
  17. int RotateCounter = 0; //counts the rotation clicks
  18. bool rotated = true; //info of the rotation
  19. bool ButtonPressed = false; //info of the button
  20.  
  21. //Statuses
  22. int CLKNow;
  23. int CLKPrevious;
  24. int DTNow;
  25. int DTPrevious;
  26.  
  27. // Timers
  28. float TimeNow1;
  29. float TimeNow2;
  30.  
  31. //LED things
  32. //digital pins
  33. const int whiteLED = 8;
  34. const int blueLED = 9;
  35. const int greenLED = 10;
  36. const int yellowLED = 11;
  37. const int redLED = 12;
  38. //statuses (1/true: ON, 0/false: OFF)
  39. bool whiteLEDStatus = false;
  40. bool blueLEDStatus = false;
  41. bool greenLEDStatus = false;
  42. bool yellowLEDStatus = false;
  43. bool redLEDStatus = false;
  44. //------------------------------
  45.  
  46. //Drawing of the LCD layout
  47. //W  B  G  Y  R   CLK
  48. //0  0  0  0  0    1
  49.  
  50.  
  51. void setup()
  52. {
  53.  
  54.      //Serial.begin(9600); //we don't use the serial in this example
  55.  
  56.     //------------------------------------------------------
  57.     lcd.begin();                      // initialize the lcd    
  58.     lcd.backlight();
  59.     //------------------------------------------------------
  60.   lcd.setCursor(0,0); //Defining position to write from first row, first column .
  61.   lcd.print("W B G Y R  CLK");
  62.   lcd.setCursor(0,1); //second line, 1st block
  63.   lcd.print("0 0 0 0 0   0"); //You can write 16 Characters per line .
  64.   delay(3000); //wait 3 sec
  65.   //------------------------------------------------------
  66.    //setting up pins   
  67.    pinMode(2, INPUT_PULLUP);
  68.    pinMode(3, INPUT_PULLUP);
  69.    pinMode(4, INPUT_PULLUP);
  70.  
  71.    pinMode(whiteLED, OUTPUT); //white LED
  72.    pinMode(blueLED, OUTPUT); //blue LED
  73.    pinMode(greenLED, OUTPUT); //green LED
  74.    pinMode(yellowLED, OUTPUT); //yellow LED
  75.    pinMode(redLED, OUTPUT); //red LED
  76.    
  77.     //LOW pins = LEDs are off. (LED + is connected to the digital pin)
  78.    digitalWrite(whiteLED, LOW);
  79.    digitalWrite(blueLED, LOW);
  80.    digitalWrite(greenLED, LOW);
  81.    digitalWrite(yellowLED, LOW);
  82.    digitalWrite(redLED, LOW);
  83.    
  84.  
  85.   //Store states
  86.   CLKPrevious = digitalRead(RotaryCLK);
  87.   DTPrevious = digitalRead(RotaryDT);
  88.    
  89.   attachInterrupt(digitalPinToInterrupt(RotaryCLK), rotate, CHANGE);
  90.   attachInterrupt(digitalPinToInterrupt(RotarySW), buttonPressed, FALLING); //either falling or rising but never "change".
  91.  
  92.   TimeNow1 = millis(); //Start timer 1  
  93. }
  94.  
  95.  
  96. void loop()
  97. {
  98.   printLCD();
  99.   ButtonChecker();
  100. }
  101.  
  102. void buttonPressed()
  103. {  
  104.   //This timer is a "software debounce". It is not the most effective solution, but it works
  105.   TimeNow2 = millis();
  106.   if(TimeNow2 - TimeNow1 > 500)
  107.   {    
  108.     ButtonPressed = true;    
  109.   }
  110.   TimeNow1 = millis();  //"reset" timer; the next 500 ms is counted from this moment
  111. }
  112.  
  113. void rotate()
  114. {
  115.   CLKNow = digitalRead(RotaryCLK); //Read the state of the CLK pin
  116.  
  117.   // If last and current state of CLK are different, then a pulse occurred  
  118.     if (CLKNow != CLKPrevious  && CLKNow == 1)
  119.     {
  120.     // If the DT state is different than the CLK state then
  121.     // the encoder is rotating CCW so increase
  122.       if (digitalRead(RotaryDT) != CLKNow)
  123.       {        
  124.       RotateCounter++;
  125.  
  126.       if(RotateCounter > 4)
  127.       {
  128.        RotateCounter = 0;
  129.       }
  130.  
  131.       }
  132.       else
  133.       {        
  134.       RotateCounter--;
  135.            
  136.       if(RotateCounter < 0)
  137.       {
  138.         RotateCounter = 4;  
  139.       }    
  140.        
  141.       }      
  142.     }  
  143.  
  144.   CLKPrevious = CLKNow;  // Store last CLK state
  145.   rotated = true;
  146. }
  147.  
  148.  
  149. void printLCD()
  150. {
  151.     if(rotated == true) //refresh the CLK
  152.     {
  153.       lcd.setCursor(12,1);
  154.       lcd.print(RotateCounter);
  155.       rotated = false;
  156.     }
  157.    
  158. }
  159.  
  160.  
  161. void ButtonChecker() //this is basically the menu part. keep track of the buttonpressed and rotatecounter for navigation
  162. {
  163.   if(ButtonPressed == true)
  164.   {
  165.     switch(RotateCounter)
  166.     {
  167.       case 0:      
  168.       if(whiteLEDStatus == false)
  169.       {
  170.         whiteLEDStatus = true;
  171.         digitalWrite(whiteLED, HIGH); //white LED is turned ON        
  172.       }
  173.       else
  174.       {
  175.         whiteLEDStatus = false;
  176.         digitalWrite(whiteLED, LOW); //white LED is turned OFF          
  177.       }
  178.  
  179.       lcd.setCursor(0,1); // Defining positon to write from second row, first column .
  180.       lcd.print(whiteLEDStatus);
  181.      
  182.       break;
  183.      
  184.       case 1:
  185.       if(blueLEDStatus == false)
  186.       {
  187.         blueLEDStatus = true;
  188.         digitalWrite(blueLED, HIGH);  
  189.        
  190.       }
  191.       else
  192.       {
  193.         blueLEDStatus = false;
  194.         digitalWrite(blueLED, LOW);          
  195.       }
  196.  
  197.       lcd.setCursor(2,1); // Defining positon to write from second row, first column .
  198.       lcd.print(blueLEDStatus);
  199.       break;
  200.      
  201.       case 2:
  202.       if(greenLEDStatus == false)
  203.       {
  204.         greenLEDStatus = true;
  205.         digitalWrite(greenLED, HIGH);  
  206.        
  207.       }
  208.       else
  209.       {
  210.         greenLEDStatus = false;
  211.         digitalWrite(greenLED, LOW);          
  212.       }
  213.  
  214.       lcd.setCursor(4,1); // Defining positon to write from second row, first column .
  215.       lcd.print(greenLEDStatus);
  216.       break;
  217.      
  218.       case 3:
  219.       if(yellowLEDStatus == false)
  220.       {
  221.         yellowLEDStatus = true;
  222.         digitalWrite(yellowLED, HIGH);          
  223.       }
  224.       else
  225.       {
  226.         yellowLEDStatus = false;
  227.         digitalWrite(yellowLED, LOW);          
  228.       }
  229.       lcd.setCursor(6,1); // Defining positon to write from second row, first column .
  230.       lcd.print(yellowLEDStatus);
  231.      
  232.       break;
  233.      
  234.       case 4:
  235.       if(redLEDStatus == false)
  236.       {
  237.         redLEDStatus = true;
  238.         digitalWrite(redLED, HIGH);  
  239.        
  240.       }
  241.       else
  242.       {
  243.         redLEDStatus = false;
  244.         digitalWrite(redLED, LOW);          
  245.       }
  246.  
  247.       lcd.setCursor(8,1); // Defining positon to write from second row, first column .
  248.       lcd.print(redLEDStatus);
  249.       break;
  250.     }    
  251.   }  
  252.   ButtonPressed = false; //reset this variable
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement