Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Industruino Demo Code - Default code loaded onto Industruino
- Copyright (c) 2013 Loic De Buck <connect@industruino.com>
- Industruino is a DIN-rail mountable Arduino Leonardo compatible product
- Please visit www.industruino.com for further information and code examples.
- Standard peripherals connected to Industruino are:
- UC1701 compatible LCD; rst:D19 dc:D20 dn:D21 sclk:D22 (set pin configuration in UC1701 library header)
- 3-button membrane panel; D23, D24, D25
- */
- #include <Indio.h>
- #include <Wire.h>
- #include <I2C_eeprom.h>
- #include <UC1701.h>
- //Download libary from https://github.com/Industruino/
- static UC1701 lcd;
- //menu defines
- //- initial cursor parameters
- int coll = 0; //column counter for cursor - always kept at 0 in this demo (left side of the screen)
- 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
- int lastChannel = 0; //keeps track of previous 'channel'. Is used to detect change in state.
- //- initial menu level parameters
- int MenuLevel = 0; //Defines the depth of the menu tree
- int MenuID = 0; //Defines the unique identifier of each menu that resides on the same menu level
- 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.
- 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.
- //- initial parameters for 'value editing mode'
- int valueEditing = 0; //Flag to indicate if the interface is in 'value editing mode', thus disabling cursor movement.
- int row = 0; //Temporary location to store the current cursor position whilst in 'value editing mode'.
- int constrainEnc = 1; //Enable/disable constraining the button panel's counter to a lower and upper limit.
- float valueEditingInc = 0; //Increments of each button press when using 'value editing mode'.
- float TargetValue = 0; // Target value to be edited in 'value editing mode'
- //Membrane panel button defines
- int buttonUpState = 0; //status of "Up" button input
- int buttonEnterState = 0; //status of "Enter" button input
- int buttonDownState = 0; //status of "Down" button input
- int prevBtnUp = 0; //previous state of "Up" button
- int prevBtnEnt = 0; //previous state of "Enter" button
- int prevBtnDown = 0; //previous state of "Down" button
- int lastBtnUp = 0; //time since last "Up" pressed event
- int lastBtnEnt = 0; //time since last "Enter" pressed event
- int lastBtnDown = 0; //time since last "Down" pressed event
- int enterPressed = 0; //status of "Enter" button after debounce filtering : 1 = pressed 0 = unpressed
- int transEntInt = 250; //debounce treshold for "Enter" button
- int transInt = 100; //debounce for other buttons
- unsigned long lastAdminActionTime = 0; //keeps track of last button activity
- // These constants won't change. They're used to give names
- // to the pins used:
- const int analogInPin = A5; // Analog input pin that the button panel is attached to
- const int backlightPin = 26; // PWM output pin that the LED backlight is attached to
- const int buttonEnterPin = 24;
- const int buttonUpPin = 25;
- const int buttonDownPin = 23;
- const int D0 = 0;
- const int D1 = 1;
- const int D2 = 2;
- const int D3 = 3;
- const int D4 = 4;
- const int D5 = 5;
- const int D6 = 6;
- const int D7 = 7;
- const int D8 = 8;
- const int D9 = 9;
- const int D10 = 10;
- const int D11 = 11;
- const int D12 = 12;
- const int D14 = 14;
- const int D15 = 15;
- const int D16 = 16;
- const int D17 = 17;
- float anOutCh1 = 0;
- float anOutCh2 = 0;
- int anOutUpLimit = 0;
- int ButtonsAnalogValue = 0; // value read from mebrane panel buttons.
- int backlightIntensity = 5; // LCD backlight intesity
- int backlightIntensityDef = 5; // Default LCD backlight intesity
- unsigned long lastLCDredraw = 0; // keeps track of last time the screen was redrawn
- #define EEPROM_SIZE 255
- I2C_eeprom eeprom50(0x50, EEPROM_SIZE);
- int setpunt, ao1, rh, rhpct, rv, rvpct, setpunt_oud = 0;
- void setup() {
- eeprom50.begin();
- setpunt = eeprom50.readByte(1);
- SetInput();
- pinMode(buttonEnterPin, INPUT);
- pinMode(buttonUpPin, INPUT);
- pinMode(buttonDownPin, INPUT);
- pinMode(backlightPin, OUTPUT); //set backlight pin to output
- 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.
- //LCD init
- lcd.begin(); //sets the resolution of the LCD screen
- for (int y = 0; y <= 7; y++) {
- for (int x = 0; x <= 128; x++) {
- lcd.setCursor(x, y);
- lcd.print(" ");
- }
- }
- //debug
- SerialUSB.begin(9600); //enables port for debugging messages
- //Menu init
- MenuWelcome(); //load first menu
- }
- /*
- 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.
- 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.
- 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.
- To make your own menus you should take 2 steps:
- 1. make a new menu function, edit the parameters such MenuLevel and MenuID, scope of the cursor (number of rows, constraints etc).
- 2. Edit the 'Navigate' function to reflect the menu function that you just made and assigning an action to it.
- */
- void loop() {
- ReadButtons(); //check buttons
- Navigate(); //update menus and perform actions
- // delay(50);
- }
- //-----------------------------------------------------------------------------------------------------------------------------------------------------------
- //UI menu content - edit, add or remove these functions to make your own menu structure
- //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.
- //------------------------------------------------------------------------------------------------------------------------------------------------------------
- void MenuWelcome() { //this function draws the first menu - splash screen
- //menu inintialisers
- channel = 0; //starting row position of the cursor (top row) - controlled by the button panel counter
- channelUpLimit = 0; //upper row limit
- channelLowLimit = 0; //lower row limit
- MenuLevel = 0; //menu tree depth -> first level
- MenuID = 0; //unique menu id -> has to be unique for each menu on the same menu level.
- enterPressed = 0; //clears any possible accidental "Enter" presses that could have been caried over from the previous menu
- lcd.clear(); //clear the screen
- //actual user content on the screen
- lcd.setCursor(5, 1); //set the cursor to the fifth pixel from the left edge, third row.
- lcd.print("firma naam"); //print text on screen
- lcd.setCursor(5, 3); //set the cursor to the fifth pixel from the left edge, third row.
- lcd.print("Controller voor"); //print text on screen
- lcd.setCursor(5, 4); //set the cursor to the fifth pixel from the left edge, third row.
- lcd.print("low-power"); //print text on screen
- lcd.setCursor(5, 5); //set the cursor to the fifth pixel from the left edge, third row.
- lcd.print("apparaat."); //print text on screen
- delay(2000);
- }
- void MenuSelect() {
- channel = 3;
- channelLowLimit = 3;
- channelUpLimit = 4;
- MenuLevel = 1;
- MenuID = 1;
- enterPressed = 0;
- lcd.clear();
- ScrollCursor();
- lcd.setCursor(6, 0);
- lcd.print("Selecteer");
- lcd.setCursor(6, 1);
- lcd.print("uw wens:");
- lcd.setCursor(6, 3);
- lcd.print("Pas setpt aan (" + String(setpunt) + "%)");
- lcd.setCursor(6, 4);
- lcd.print("Live view");
- }
- void MenuLiveView() {
- LoadVars();
- channel = 0;
- channelUpLimit = 6;
- channelLowLimit = 6;
- MenuID = 2;
- MenuLevel = 2;
- enterPressed = 0;
- lcd.clear();
- ScrollCursor();
- lcd.setCursor(6, 1);
- lcd.print("RV: " + String(rvpct) + "% (" + String(rv) + ")");
- lcd.setCursor(6, 2);
- lcd.print("RH: " + String(rhpct) + "% (" + String(rh) + ")" );
- lcd.setCursor(6, 0);
- lcd.print("Setpunt: " + String(setpunt));
- lcd.setCursor(6, 3);
- lcd.print("AO1: " + String(ao1) + "Vdc/10Vdc");
- lcd.setCursor(6, 6);
- lcd.print("Terug");
- }
- void MenuSetPoint() {
- channel = 0;
- channelUpLimit = 2;
- channelLowLimit = 0;
- MenuID = 1;
- MenuLevel = 2;
- enterPressed = 0;
- lcd.clear();
- ScrollCursor();
- lcd.setCursor(6, 0);
- lcd.print("Setpunt: ");
- lcd.setCursor(6, 1);
- lcd.print(setpunt);
- lcd.setCursor(6, 2);
- lcd.print("Terug");
- }
- //---------------------------------------------------------------------------------------------------------------------------------------------------
- //UI control logic, please edit this function to reflect the specific menus that your created above and your desired actions for each cursor position
- //---------------------------------------------------------------------------------------------------------------------------------------------------
- void Navigate()
- {
- if (valueEditing != 1) {
- if (MenuLevel == 0)
- {
- {
- if (enterPressed == 1) MenuSelect();
- }
- }
- if (MenuLevel == 1) { //Main Menu
- if (channel == 3 && enterPressed == 1) MenuSetPoint();
- if (channel == 4 && enterPressed == 1) MenuLiveView();
- }
- if (MenuLevel == 2) {
- if (MenuID == 1) {
- if (channel == 1 && enterPressed == 1) //using 'value editing mode' to edit a variable using the UI
- {
- TargetValue = setpunt; //copy variable to be edited to 'Target value'
- setpunt = EditValue();
- if (setpunt != eeprom50.readByte(1))
- {
- eeprom50.writeByte(1, setpunt);
- delay(5);
- MenuSelect();
- }
- }
- if (channel == 2 && enterPressed == 1) MenuSelect();
- }
- }
- if (MenuLevel == 2) {
- if (MenuID == 2) {
- /* live view */
- if ((millis() - lastLCDredraw) > 268) {
- MenuLiveView();
- lastLCDredraw = millis();
- }
- if (channel == 6 && enterPressed == 1) MenuSelect();
- }
- }
- //dont remove this part
- if (channel != lastChannel && valueEditing != 1 && MenuID != 0) { //updates the cursor position if button counter changed and 'value editing mode' is not running
- ScrollCursor();
- }
- }
- }
- //---------------------------------------------------------------------------------------------------------------------------------------------
- //---------------------------------------------------------------------------------------------------------------------------------------------
- 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
- {
- row = channel; //save the current cursor position so that after using the buttons for 'value editing mode' the cursor position can be reinstated.
- channel = 0; //reset the button counter so to avoid carrying over a value from the cursor.
- constrainEnc = 0; //disable constrainment of button counter's range
- valueEditingInc = 1; //increment for each button press
- valueEditing = 1; //flag to indicate that we are going into 'value editing mode'.
- enterPressed = 0; //clears any possible accidental "Enter" presses that could have been caried over
- while (enterPressed != 1) { //stays in 'value editing mode' until enter is pressed
- ReadButtons(); //check the buttons for any change
- lcd.setCursor(0, row);
- lcd.print("*");
- if (channel != lastChannel) { //when up or down button is pressed
- if (channel < lastChannel && TargetValue <= 100) { //if 'Up' button is pressed, and is within constraint range.
- TargetValue += valueEditingInc; //increment target variable with pre-defined increment value
- }
- if (channel > lastChannel && TargetValue > 0) { //if 'Down' button is pressed, and is within constraint range.
- TargetValue -= valueEditingInc ; //decrement target variable with pre-defined increment value
- }
- //clear a section of a row to make space for updated value
- for (int i = 60; i <= 70; i++) {
- lcd.setCursor(i, row);
- lcd.print(" ");
- }
- //print updated value
- lcd.setCursor(66, row);
- SerialUSB.println(TargetValue);
- lcd.print(TargetValue, 0);
- lastChannel = channel;
- }
- //delay(50);
- }
- 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'
- constrainEnc = 1; //enable constrainment of button counter's range so to stay within the menu's range
- channelUpLimit = 2; //upper row limit
- valueEditing = 0; //flag to indicate that we are leaving 'value editing mode'
- enterPressed = 0; //clears any possible accidental "Enter" presses that could have been caried over
- return TargetValue; //return the edited value to the main 'Navigate' UI control function for further processing
- }
- //---------------------------------------------------------------------------------------------------------------------------------------------
- // Peripheral functions
- //---------------------------------------------------------------------------------------------------------------------------------------------
- void ReadButtons() {
- buttonEnterState = digitalRead(buttonEnterPin);
- buttonUpState = digitalRead(buttonUpPin);
- buttonDownState = digitalRead(buttonDownPin);
- if (buttonEnterState == HIGH && prevBtnEnt == LOW)
- {
- if ((millis() - lastBtnEnt) > transEntInt)
- {
- enterPressed = 1;
- }
- lastBtnEnt = millis();
- lastAdminActionTime = millis();
- SerialUSB.println("enterPressed");
- }
- prevBtnEnt = buttonEnterState;
- if (buttonUpState == HIGH && prevBtnUp == LOW)
- {
- if ((millis() - lastBtnUp) > transInt)
- {
- channel--;
- }
- lastBtnUp = millis();
- lastAdminActionTime = millis();
- SerialUSB.println("UpPressed");
- }
- prevBtnUp = buttonUpState;
- if (buttonDownState == HIGH && prevBtnDown == LOW)
- {
- if ((millis() - lastBtnDown) > transInt)
- {
- channel++;
- }
- lastBtnDown = millis();
- lastAdminActionTime = millis();
- SerialUSB.println("DownPressed");
- }
- prevBtnDown = buttonDownState;
- if (constrainEnc == 1) {
- channel = constrain(channel, channelLowLimit, channelUpLimit);
- }
- }
- void SetOutput() { // a simple function called to set a group of pins as outputs
- pinMode(D0, OUTPUT);
- pinMode(D1, OUTPUT);
- pinMode(D2, OUTPUT);
- pinMode(D3, OUTPUT);
- pinMode(D4, OUTPUT);
- pinMode(D5, OUTPUT);
- pinMode(D6, OUTPUT);
- pinMode(D7, OUTPUT);
- pinMode(D8, OUTPUT);
- pinMode(D9, OUTPUT);
- pinMode(D10, OUTPUT);
- pinMode(D11, OUTPUT);
- pinMode(D12, OUTPUT);
- pinMode(D14, OUTPUT);
- pinMode(D15, OUTPUT);
- pinMode(D16, OUTPUT);
- pinMode(D17, OUTPUT);
- }
- void SetInput() { // a simple function called to set a group of pins as inputs
- pinMode(D0, INPUT);
- pinMode(D1, INPUT);
- pinMode(D2, INPUT);
- pinMode(D3, INPUT);
- pinMode(D4, INPUT);
- pinMode(D5, INPUT);
- pinMode(D6, INPUT);
- pinMode(D7, INPUT);
- pinMode(D8, INPUT);
- pinMode(D9, INPUT);
- pinMode(D10, INPUT);
- pinMode(D11, INPUT);
- pinMode(D12, INPUT);
- pinMode(D14, INPUT);
- pinMode(D15, INPUT);
- pinMode(D16, INPUT);
- pinMode(D17, INPUT);
- }
- void LoadVars() {
- Indio.setADCResolution(12);
- Indio.analogReadMode(1, mA);
- Indio.analogReadMode(2, mA);
- rh = Indio.analogRead(1);
- rv = Indio.analogRead(2);
- Indio.analogReadMode(1, mA_p);
- Indio.analogReadMode(2, mA_p);
- rhpct = Indio.analogRead(1);
- rvpct = Indio.analogRead(2);
- }
- //---------------------------------------------------------------------------------------------------------------------------------------------
- // UI core functions
- //---------------------------------------------------------------------------------------------------------------------------------------------
- void ScrollCursor() //makes the cursor move
- {
- lastChannel = channel; //keep track button counter changes
- for (int i = 0; i <= 6; i++) { //clear the whole column when redrawing a new cursor
- lcd.setCursor(coll, i);
- lcd.print(" ");
- }
- lcd.setCursor(coll, channel); //set new cursor position
- lcd.print(">"); //draw cursor
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement