Advertisement
Guest User

Untitled

a guest
Apr 10th, 2020
612
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 16.92 KB | None | 0 0
  1. /*
  2.   Industruino Demo Code - Default code loaded onto Industruino
  3.  
  4.   Copyright (c) 2013 Loic De Buck <connect@industruino.com>
  5.  
  6.   Industruino is a DIN-rail mountable Arduino Leonardo compatible product
  7.   Please visit www.industruino.com for further information and code examples.
  8.   Standard peripherals connected to Industruino are:
  9.   UC1701 compatible LCD; rst:D19 dc:D20 dn:D21 sclk:D22 (set pin configuration in UC1701 library header)
  10.   3-button membrane panel; D23, D24, D25
  11. */
  12. #include <Indio.h>
  13. #include <Wire.h>
  14. #include <I2C_eeprom.h>
  15. #include <UC1701.h>
  16. //Download libary from https://github.com/Industruino/
  17.  
  18. static UC1701 lcd;
  19.  
  20. //menu defines
  21.  
  22. //- initial cursor parameters
  23. int coll = 0; //column counter for cursor - always kept at 0 in this demo (left side of the screen)
  24. int channel = 0; //Counter is controlled by the up&down buttons on the membrane panel. Has double use; 1. As row controller for the cursor (screen displays 6 rows of text, counting from 0 to 5). 2. As editor for numerical values shown on screen
  25. int lastChannel = 0; //keeps track of previous 'channel'. Is used to detect change in state.
  26.  
  27. //- initial menu level parameters
  28. int MenuLevel = 0; //Defines the depth of the menu tree
  29. int MenuID = 0; //Defines the unique identifier of each menu that resides on the same menu level
  30. int channelUpLimit = 5; //Defines the upper limit of the button counter: 1. To limit cursor's downward row movement 2. To set the upper limit of value that is beeing edited.
  31. int channelLowLimit = 0; //Defines the lower limit of the button counter: 1. To limit cursor's upward row movement 2. To set the lower limit of value that is beeing edited.
  32.  
  33. //- initial parameters for 'value editing mode'
  34. int valueEditing = 0; //Flag to indicate if the interface is in 'value editing mode', thus disabling cursor movement.
  35. int row = 0; //Temporary location to store the current cursor position whilst in 'value editing mode'.
  36. int constrainEnc = 1; //Enable/disable constraining the button panel's counter to a lower and upper limit.
  37. float valueEditingInc = 0; //Increments of each button press when using 'value editing mode'.
  38. float TargetValue = 0; // Target value to be edited in 'value editing mode'
  39.  
  40. //Membrane panel button defines
  41.  
  42. int buttonUpState = 0; //status of "Up" button input
  43. int buttonEnterState = 0; //status of "Enter" button input
  44. int buttonDownState = 0; //status of "Down" button input
  45.  
  46. int prevBtnUp = 0; //previous state of "Up" button
  47. int prevBtnEnt = 0; //previous state of "Enter" button
  48. int prevBtnDown = 0; //previous state of "Down" button
  49.  
  50. int lastBtnUp = 0; //time since last "Up" pressed event
  51. int lastBtnEnt = 0; //time since last "Enter" pressed event
  52. int lastBtnDown = 0; //time since last "Down" pressed event
  53.  
  54. int enterPressed = 0; //status of "Enter" button after debounce filtering : 1 = pressed 0 = unpressed
  55.  
  56. int transEntInt = 250; //debounce treshold for "Enter" button
  57. int transInt = 100; //debounce for other buttons
  58. unsigned long lastAdminActionTime = 0; //keeps track of last button activity
  59.  
  60. // These constants won't change.  They're used to give names
  61. // to the pins used:
  62. const int analogInPin = A5;  // Analog input pin that the button panel is attached to
  63. const int backlightPin = 26; // PWM output pin that the LED backlight is attached to
  64. const int buttonEnterPin = 24;
  65. const int buttonUpPin = 25;
  66. const int buttonDownPin = 23;
  67. const int D0 = 0;
  68. const int D1 = 1;
  69. const int D2 = 2;
  70. const int D3 = 3;
  71. const int D4 = 4;
  72. const int D5 = 5;
  73. const int D6 = 6;
  74. const int D7 = 7;
  75. const int D8 = 8;
  76. const int D9 = 9;
  77. const int D10 = 10;
  78. const int D11 = 11;
  79. const int D12 = 12;
  80. const int D14 = 14;
  81. const int D15 = 15;
  82. const int D16 = 16;
  83. const int D17 = 17;
  84.  
  85. float anOutCh1 = 0;
  86. float anOutCh2 = 0;
  87. int anOutUpLimit = 0;
  88.  
  89. int ButtonsAnalogValue = 0;        // value read from mebrane panel buttons.
  90. int backlightIntensity = 5;        // LCD backlight intesity
  91. int backlightIntensityDef = 5;     // Default LCD backlight intesity
  92. unsigned long lastLCDredraw = 0;   // keeps track of last time the screen was redrawn
  93. #define EEPROM_SIZE 255
  94.  
  95. I2C_eeprom eeprom50(0x50, EEPROM_SIZE);
  96. int setpunt, ao1, rh, rhpct, rv, rvpct, setpunt_oud = 0;
  97. void setup() {
  98.   eeprom50.begin();
  99.   setpunt = eeprom50.readByte(1);
  100.  
  101.   SetInput();
  102.   pinMode(buttonEnterPin, INPUT);
  103.   pinMode(buttonUpPin, INPUT);
  104.   pinMode(buttonDownPin, INPUT);
  105.   pinMode(backlightPin, OUTPUT); //set backlight pin to output
  106.   analogWrite(backlightPin, (map(backlightIntensity, 5, 1, 255, 0))); //convert backlight intesity from a value of 0-5 to a value of 0-255 for PWM.
  107.   //LCD init
  108.   lcd.begin();  //sets the resolution of the LCD screen
  109.  
  110.   for (int y = 0; y <= 7; y++) {
  111.     for (int x = 0; x <= 128; x++) {
  112.       lcd.setCursor(x, y);
  113.       lcd.print(" ");
  114.     }
  115.   }
  116.  
  117.   //debug
  118.   SerialUSB.begin(9600); //enables port for debugging messages
  119.  
  120.   //Menu init
  121.   MenuWelcome(); //load first menu
  122.  
  123. }
  124.  
  125. /*
  126.   1. The loop function calls a function to check the buttons (this could also be driven by timer interrupt) and updates the button counter (variable called 'channel'), which increases when 'Down' button is pressed and decreases when "Up" buttons is pressed.
  127.   2. Next, the loop function calls the 'Navigate' function which draws the cursor in a position based on the button counter, and when the "Enter" button is pressed checks which new menu should be  loaded or what other action to perform.
  128.   3. Each menu's content and scope is defined in a separate function. Each menu should have a defined 'MenuLevel' (depth of the menu tree, starting from 0) and unique MenuID so that the Navigate function can discern which menu is active.
  129.   To make your own menus you should take 2 steps:
  130.   1. make a new menu function, edit the parameters such MenuLevel and MenuID, scope of the cursor (number of rows, constraints etc).
  131.   2. Edit the 'Navigate' function to reflect the menu function that you just made and assigning an action to it.
  132. */
  133.  
  134. void loop() {
  135.  
  136.   ReadButtons(); //check buttons
  137.   Navigate(); //update menus and perform actions
  138.   //  delay(50);
  139. }
  140.  
  141.  
  142. //-----------------------------------------------------------------------------------------------------------------------------------------------------------
  143. //UI menu content - edit, add or remove these functions to make your own menu structure
  144. //These functions only generate the content that is printed to the screen, please also edit the "Navigate" function further below to add actions to each menu.
  145. //------------------------------------------------------------------------------------------------------------------------------------------------------------
  146.  
  147.  
  148. void MenuWelcome() { //this function draws the first menu - splash screen
  149.   //menu inintialisers
  150.   channel = 0; //starting row position of the cursor (top row) - controlled by the button panel counter
  151.   channelUpLimit = 0; //upper row limit
  152.   channelLowLimit = 0; //lower row limit
  153.   MenuLevel = 0; //menu tree depth -> first level
  154.   MenuID = 0; //unique menu id -> has to be unique for each menu on the same menu level.
  155.   enterPressed = 0; //clears any possible accidental "Enter" presses that could have been caried over from the previous menu
  156.   lcd.clear(); //clear the screen
  157.   //actual user content on the screen
  158.   lcd.setCursor(5, 1); //set the cursor to the fifth pixel from the left edge, third row.
  159.   lcd.print("firma naam"); //print text on screen
  160.   lcd.setCursor(5, 3); //set the cursor to the fifth pixel from the left edge, third row.
  161.   lcd.print("Controller voor"); //print text on screen
  162.   lcd.setCursor(5, 4); //set the cursor to the fifth pixel from the left edge, third row.
  163.   lcd.print("low-power"); //print text on screen
  164.   lcd.setCursor(5, 5); //set the cursor to the fifth pixel from the left edge, third row.
  165.   lcd.print("apparaat."); //print text on screen
  166.   delay(2000);
  167. }
  168.  
  169.  
  170. void MenuSelect() {
  171.   channel = 3;
  172.   channelLowLimit = 3;
  173.   channelUpLimit = 4;
  174.   MenuLevel = 1;
  175.   MenuID = 1;
  176.   enterPressed = 0;
  177.   lcd.clear();
  178.   ScrollCursor();
  179.   lcd.setCursor(6, 0);
  180.   lcd.print("Selecteer");
  181.   lcd.setCursor(6, 1);
  182.   lcd.print("uw wens:");
  183.   lcd.setCursor(6, 3);
  184.   lcd.print("Pas setpt aan (" + String(setpunt) + "%)");
  185.   lcd.setCursor(6, 4);
  186.   lcd.print("Live view");
  187. }
  188.  
  189. void MenuLiveView() {
  190.   LoadVars();
  191.   channel = 0;
  192.   channelUpLimit = 6;
  193.   channelLowLimit = 6;
  194.   MenuID = 2;
  195.   MenuLevel = 2;
  196.   enterPressed = 0;
  197.   lcd.clear();
  198.   ScrollCursor();
  199.   lcd.setCursor(6, 1);
  200.   lcd.print("RV: " + String(rvpct) + "% (" + String(rv) + ")");
  201.   lcd.setCursor(6, 2);
  202.   lcd.print("RH: " + String(rhpct) + "% (" + String(rh) + ")" );
  203.   lcd.setCursor(6, 0);
  204.   lcd.print("Setpunt: " + String(setpunt));
  205.   lcd.setCursor(6, 3);
  206.   lcd.print("AO1: " + String(ao1) + "Vdc/10Vdc");
  207.   lcd.setCursor(6, 6);
  208.   lcd.print("Terug");
  209. }
  210.  
  211. void MenuSetPoint() {
  212.   channel = 0;
  213.   channelUpLimit = 2;
  214.   channelLowLimit = 0;
  215.   MenuID = 1;
  216.   MenuLevel = 2;
  217.   enterPressed = 0;
  218.   lcd.clear();
  219.   ScrollCursor();
  220.   lcd.setCursor(6, 0);
  221.   lcd.print("Setpunt: ");
  222.   lcd.setCursor(6, 1);
  223.   lcd.print(setpunt);
  224.   lcd.setCursor(6, 2);
  225.   lcd.print("Terug");
  226. }
  227.  
  228. //---------------------------------------------------------------------------------------------------------------------------------------------------
  229. //UI control logic, please edit this function to reflect the specific menus that your created above and your desired actions for each cursor position
  230. //---------------------------------------------------------------------------------------------------------------------------------------------------
  231.  
  232.  
  233.  
  234. void Navigate()
  235. {
  236.  
  237.   if (valueEditing != 1) {
  238.  
  239.     if (MenuLevel == 0)
  240.     {
  241.       {
  242.         if (enterPressed == 1) MenuSelect();
  243.       }
  244.     }
  245.     if (MenuLevel == 1) { //Main Menu
  246.       if (channel == 3 && enterPressed == 1) MenuSetPoint();
  247.       if (channel == 4 && enterPressed == 1) MenuLiveView();
  248.     }
  249.     if (MenuLevel == 2) {
  250.       if (MenuID == 1) {
  251.         if (channel == 1 && enterPressed == 1) //using 'value editing mode' to edit a variable using the UI
  252.         {
  253.           TargetValue = setpunt; //copy variable to be edited to 'Target value'
  254.           setpunt = EditValue();
  255.           if (setpunt != eeprom50.readByte(1))
  256.           {
  257.             eeprom50.writeByte(1, setpunt);
  258.             delay(5);
  259.             MenuSelect();
  260.           }
  261.         }
  262.         if (channel == 2 && enterPressed == 1) MenuSelect();
  263.       }
  264.     }
  265.  
  266.     if (MenuLevel == 2) {
  267.       if (MenuID == 2) {
  268.         /* live view */
  269.         if ((millis() - lastLCDredraw) > 268) {
  270.           MenuLiveView();
  271.           lastLCDredraw = millis();
  272.         }
  273.         if (channel == 6 && enterPressed == 1) MenuSelect();
  274.       }
  275.     }
  276.     //dont remove this part
  277.     if (channel != lastChannel && valueEditing != 1 && MenuID != 0) { //updates the cursor position if button counter changed and 'value editing mode' is not running
  278.       ScrollCursor();
  279.     }
  280.   }
  281. }
  282.  
  283.  
  284. //---------------------------------------------------------------------------------------------------------------------------------------------
  285. //---------------------------------------------------------------------------------------------------------------------------------------------
  286.  
  287.  
  288. float EditValue() //a function to edit a variable using the UI - function is called by the main 'Navigate' UI control function and is loaded with a variable to be edited
  289. {
  290.   row = channel; //save the current cursor position so that after using the buttons for 'value editing mode' the cursor position can be reinstated.
  291.   channel = 0; //reset the button counter so to avoid carrying over a value from the cursor.
  292.   constrainEnc = 0; //disable constrainment of button counter's range
  293.   valueEditingInc = 1; //increment for each button press
  294.   valueEditing = 1; //flag to indicate that we are going into 'value editing mode'.
  295.   enterPressed = 0; //clears any possible accidental "Enter" presses that could have been caried over
  296.   while (enterPressed != 1) { //stays in 'value editing mode' until enter is pressed
  297.     ReadButtons(); //check the buttons for any change
  298.     lcd.setCursor(0, row);
  299.     lcd.print("*");
  300.     if (channel != lastChannel) { //when up or down button is pressed
  301.       if (channel < lastChannel && TargetValue <= 100) { //if 'Up' button is pressed, and is within constraint range.
  302.         TargetValue += valueEditingInc; //increment target variable with pre-defined increment value
  303.       }
  304.       if (channel > lastChannel && TargetValue > 0) { //if 'Down' button is pressed, and is within constraint range.
  305.         TargetValue -= valueEditingInc ; //decrement target variable with pre-defined increment value
  306.       }
  307.       //clear a section of a row to make space for updated value
  308.       for (int i = 60; i <= 70; i++) {
  309.         lcd.setCursor(i, row);
  310.         lcd.print("   ");
  311.       }
  312.       //print updated value
  313.       lcd.setCursor(66, row);
  314.       SerialUSB.println(TargetValue);
  315.       lcd.print(TargetValue, 0);
  316.       lastChannel = channel;
  317.     }
  318.     //delay(50);
  319.   }
  320.   channel = row; //load back the previous row position to the button counter so that the cursor stays in the same position as it was left before switching to 'value editing mode'
  321.   constrainEnc = 1; //enable constrainment of button counter's range so to stay within the menu's range
  322.   channelUpLimit = 2; //upper row limit
  323.   valueEditing = 0; //flag to indicate that we are leaving 'value editing mode'
  324.   enterPressed = 0; //clears any possible accidental "Enter" presses that could have been caried over
  325.   return TargetValue; //return the edited value to the main 'Navigate' UI control function for further processing
  326. }
  327.  
  328.  
  329.  
  330. //---------------------------------------------------------------------------------------------------------------------------------------------
  331. // Peripheral functions
  332. //---------------------------------------------------------------------------------------------------------------------------------------------
  333. void ReadButtons() {
  334.  
  335.   buttonEnterState = digitalRead(buttonEnterPin);
  336.   buttonUpState = digitalRead(buttonUpPin);
  337.   buttonDownState = digitalRead(buttonDownPin);
  338.  
  339.   if (buttonEnterState == HIGH && prevBtnEnt == LOW)
  340.   {
  341.     if ((millis() - lastBtnEnt) > transEntInt)
  342.     {
  343.       enterPressed = 1;
  344.     }
  345.     lastBtnEnt = millis();
  346.     lastAdminActionTime = millis();
  347.     SerialUSB.println("enterPressed");
  348.   }
  349.   prevBtnEnt = buttonEnterState;
  350.  
  351.  
  352.   if (buttonUpState == HIGH && prevBtnUp == LOW)
  353.   {
  354.     if ((millis() - lastBtnUp) > transInt)
  355.     {
  356.       channel--;
  357.     }
  358.     lastBtnUp = millis();
  359.     lastAdminActionTime = millis();
  360.     SerialUSB.println("UpPressed");
  361.   }
  362.   prevBtnUp = buttonUpState;
  363.  
  364.  
  365.   if (buttonDownState == HIGH && prevBtnDown == LOW)
  366.   {
  367.     if ((millis() - lastBtnDown) > transInt)
  368.     {
  369.       channel++;
  370.     }
  371.     lastBtnDown = millis();
  372.     lastAdminActionTime = millis();
  373.     SerialUSB.println("DownPressed");
  374.   }
  375.   prevBtnDown = buttonDownState;
  376.  
  377.   if (constrainEnc == 1) {
  378.     channel = constrain(channel, channelLowLimit, channelUpLimit);
  379.   }
  380.  
  381. }
  382.  
  383.  
  384.  
  385. void SetOutput() { // a simple function called to set a group of pins as outputs
  386.   pinMode(D0, OUTPUT);
  387.   pinMode(D1, OUTPUT);
  388.   pinMode(D2, OUTPUT);
  389.   pinMode(D3, OUTPUT);
  390.   pinMode(D4, OUTPUT);
  391.   pinMode(D5, OUTPUT);
  392.   pinMode(D6, OUTPUT);
  393.   pinMode(D7, OUTPUT);
  394.   pinMode(D8, OUTPUT);
  395.   pinMode(D9, OUTPUT);
  396.   pinMode(D10, OUTPUT);
  397.   pinMode(D11, OUTPUT);
  398.   pinMode(D12, OUTPUT);
  399.   pinMode(D14, OUTPUT);
  400.   pinMode(D15, OUTPUT);
  401.   pinMode(D16, OUTPUT);
  402.   pinMode(D17, OUTPUT);
  403. }
  404.  
  405. void SetInput() { // a simple function called to set a group of pins as inputs
  406.   pinMode(D0, INPUT);
  407.   pinMode(D1, INPUT);
  408.   pinMode(D2, INPUT);
  409.   pinMode(D3, INPUT);
  410.   pinMode(D4, INPUT);
  411.   pinMode(D5, INPUT);
  412.   pinMode(D6, INPUT);
  413.   pinMode(D7, INPUT);
  414.   pinMode(D8, INPUT);
  415.   pinMode(D9, INPUT);
  416.   pinMode(D10, INPUT);
  417.   pinMode(D11, INPUT);
  418.   pinMode(D12, INPUT);
  419.   pinMode(D14, INPUT);
  420.   pinMode(D15, INPUT);
  421.   pinMode(D16, INPUT);
  422.   pinMode(D17, INPUT);
  423. }
  424.  
  425. void LoadVars() {
  426.   Indio.setADCResolution(12);
  427.   Indio.analogReadMode(1, mA);
  428.   Indio.analogReadMode(2, mA);
  429.   rh = Indio.analogRead(1);
  430.   rv = Indio.analogRead(2);
  431.   Indio.analogReadMode(1, mA_p);
  432.   Indio.analogReadMode(2, mA_p);
  433.   rhpct = Indio.analogRead(1);
  434.   rvpct = Indio.analogRead(2);
  435. }
  436.  
  437.  
  438.  
  439.  
  440.  
  441. //---------------------------------------------------------------------------------------------------------------------------------------------
  442. // UI core functions
  443. //---------------------------------------------------------------------------------------------------------------------------------------------
  444.  
  445.  
  446.  
  447. void ScrollCursor() //makes the cursor move
  448. {
  449.   lastChannel = channel; //keep track button counter changes
  450.   for (int i = 0; i <= 6; i++) { //clear the whole column when redrawing a new cursor
  451.     lcd.setCursor(coll, i);
  452.     lcd.print(" ");
  453.   }
  454.   lcd.setCursor(coll, channel); //set new cursor position
  455.   lcd.print(">"); //draw cursor
  456.  
  457. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement