CigarroPuro

Untitled

Mar 30th, 2021
667
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 11.51 KB | None | 0 0
  1.  
  2.  
  3. /******************************************************************************
  4.     Copyright (C) 2020  Daryl Hanlon ([email protected])
  5.  
  6.     This program is free software: you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation, either version 3 of the License, or
  9.     (at your option) any later version.
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program.  If not, see <https://www.gnu.org/licenses/>.
  18.  
  19. *********************************************************************************/
  20. #include <Control_Surface.h>
  21. #include <images.h>
  22. #include <ILI9341_due_config.h>
  23. #include <SystemFont5x7.h>
  24. #include <ILI9341_due.h>
  25. #include <fonts/Arial_bold_14.h>
  26. #include <EEPROM.h>
  27. #include <SPI.h>
  28. #include <MIDI.h>
  29. #include <AnalogSmooth.h>
  30. #include <ResponsiveAnalogRead.h>
  31. #include <AnalogMultiButton.h>
  32. #include <LcdBarGraphX.h>                                 //Used for display Bar graphs on Lcd screen
  33. #include <LiquidCrystal.h>                            
  34. #include <LiquidCrystal_I2C.h>                            //Used for LCD I2C screen
  35. #include <Encoder.h>                                      //Used for encoder
  36. #include <Arduino_Helpers.h>                               //Include the Arduino Helpers library.
  37. #include <LCD.h>
  38.  
  39. USBMIDI_Interface LaPedaleraTioElias;                                        
  40.                                      
  41. /**Declare ENCODER.connect pins 18, 19 and named outputA
  42.  * and outputB and connect pin 4 on arduino(encoder sw button) */              
  43. #define outputA 18 //  CLK. Clock signal.
  44. #define outputB 19 //  DA. Data signal. (detects move change)
  45. #define sw 4       //  SW. Presure signal (button).
  46.  
  47.  
  48. const int speedMultiplier = 1;  // Variable for the Encoder  
  49.  
  50.  
  51.  
  52. // Pin definitions and wiring for TFT:
  53. #define TFT_CS   53
  54. #define TFT_DC   49
  55. #define TFT_RST  48 //Can also be connected to reset on Arduino board.
  56.  
  57.  
  58. /*Hardware MOSI and SCK pins for MEGA (can't be freely assigned):
  59.   MOSI -> pin 51
  60.   SCK -> pin 52.
  61.   LED & VCC -> 3.3v
  62.   GND -> GND.
  63.   SCK, MOSI, DC and CS pins must go through 5v -> 3.3v level shifter.
  64.   MISO can be left disconnected if not using SD card.*/
  65.  
  66. // Create instance of TFT library:
  67. ILI9341_due tft = ILI9341_due(TFT_CS, TFT_DC, TFT_RST);
  68.  
  69. //LED pin array:
  70. const byte led[9] = {24, 25, 22, 23, 20, 21, 18, 19, 31};
  71. const byte ledCnt = 9;
  72.  
  73. long unsigned midiLedTime = 0;
  74.  
  75. //Button arrays and pins:
  76. const byte pressed[10] = {5, 4, 7, 6, 9, 8, 36, 37, 11, 10};
  77. const byte pressCount = 10;
  78.  
  79. //Expression pedal variables:
  80. byte pedalControlNum = 0;
  81. bool pedalState = 0;
  82. const int pedalPin = A0;
  83. ResponsiveAnalogRead pedal(pedalPin, true, 0.01);
  84. int pedalVal = 0;
  85. int lastPedalVal = 0;   //Initial wah values. One for the first value and another to test if there's been a change.
  86. int pedalRead = 0;
  87. int pedalBar = 0;
  88. char sensorPrintout[4];    //Char array to print wah values.
  89. char oldSensorPrintout[4]; //Char array to print wah values.
  90. int pedalMin = 1023;       // Reference minimum pedal value for calibration.
  91. int pedalMax = 0;          // Maximum pedal value for calibration.
  92. long unsigned calibrateTime = 0;
  93. //eeprom address locations:
  94. #define ADDR_MIN_VAL         0                               //Starts at address zero.
  95. #define ADDR_MAX_VAL         ADDR_MIN_VAL + sizeof(pedalMin) //Use the next free space.
  96. //#define ADDR_X  ADDR_MAX_VAL + sizeof(pedalMax)            //For hypothetical use of more eeprom for something else.
  97.  
  98. //Digital volume:
  99. int volVal = 64;
  100. int lastVolVal = 0;
  101. int volBar = 0;
  102. int volPerc = 0;
  103.  
  104. //Battery stuff:
  105. int battPin = 15; //Analog pin to read battery voltage.
  106. AnalogSmooth as = AnalogSmooth(); //Average value of 10 readings.
  107. float battRead = 0;
  108. char voltagePrintout[5];//Char array for battery voltage.
  109. char volPercPrintout[4];//Char array for battery percentage.
  110. int voltageBar = 0;
  111. int voltagePerc = 0;
  112. int lastVoltagePerc = 0;
  113. long unsigned lastReading = -30000;
  114. long unsigned currentReading = 0;
  115. long unsigned battStartupTime = 0;
  116. bool usbOn = true;
  117.  
  118. //Variables for bank changes:
  119. bool statusBank[5] = {true, false, false, false, false};
  120.  
  121. int bankNumber = 0;
  122. byte oldBankNumber = 1;
  123.  
  124. byte bankButtonState = 0;
  125. byte lastBankButton = 0;
  126.  
  127. byte lastBank = 0; //Variable to enable returning to last bank active before leaving preset mode.
  128. byte b = bankNumber;
  129.  
  130. const char *smallPresetName[5][6] =
  131. {
  132.   {"MAST. PUP", "B. in BLACK", "STONER", "CLEAN", "CLEANw/EFF", "ECHOEY"}, //Bank 1.
  133.   {"TOOL", "KYUSS", "CYCO", "OCTA", "SANDMAN", "NEUROSIS"},                //Bank 2.
  134.   {"TRIP.DEL", "CRUNCH", "FUZZY", "EXTREME", "SLUDGE", "DOOM"},            //3
  135.   {"PRES.19", "PRES.20", "PRES.21", "PRES.22", "PRES.23", "PRES.24"},      //4
  136.   {"PRES.25", "PRES.26", "PRES.27", "PRES.28", "PRES.29", "PRES.30"}       //5
  137. };                                                                         //For more banks, add more sets (6 per bank) and reflect no. in first parameter after bankNames.
  138.  
  139. const char *bigPresetName[] =                                                         //Large name printed in centre.
  140. { "MASTER OF PUPPETS", "BACK IN BLACK", "STONER", "CLEAN", "CLEAN w/EFF", "ECHOEY",   //Bank 1. These differ from the arrays above in that here individual names are printed, not sets.
  141.   "TOOL", "KYUSS", "CYCO", "OCTA", "SANDMAN", "NEUROSIS",                             //Bank 2.
  142.   "TRIPLE DELAY", "CRUNCH", "FUZZY", "EXTREME", "SLUDGE", "DOOM",                     //3
  143.   "PRESET 19", "PRESET 20", "PRESET 21", "PRESET 22", "PRESET 23", "PRESET 24",       //4
  144.   "PRESET 25", "PRESET 26", "PRESET 27", "PRESET 28", "PRESET 29", "PRESET 30"        //5
  145. };                                                                                    //Add more names if adding more banks (6 per bank).
  146.  
  147. int activeLed = -1;    //Currently active LED. Set at boot to a value not corresponding to any LED.
  148. int lastLed = -1;      //LED to turn off when another one becomes active.
  149. int activePreset = -2; //To return to the last preset active before leaving preset mode. Set to -2 only at boot to stop it printing wrong preset.
  150. int lastActivePreset = 0;
  151. bool presetChanged = false;
  152.  
  153. //Pedalboard mode variable:
  154. bool stompStatus = false;
  155.  
  156. //Variables to toggle stomp buttons:
  157. bool stompState[6] = {true, true, true, true, true, true};
  158.  
  159. long unsigned clearNameTime = 0;
  160.  
  161. //Variable to toggle loop mode:
  162. bool loopStatus = false;
  163.  
  164. long unsigned loopTime = 0;
  165.  
  166. //Variables to toggle loop buttons:
  167. bool loopState[6] = {true, true, true, true, true, true};
  168.  
  169. //Variables used to control blinking LEDs:
  170. bool ledOn[6] = {false, false, false, false, false, false};
  171. bool ledStatus[6] = {LOW, LOW, LOW, LOW, LOW, LOW};
  172.  
  173. byte loopLed = 0;
  174.  
  175. //Variables to hold the times for blinking LEDs:
  176. unsigned long timeNow = 0;
  177. unsigned long timePrev = 0;
  178. unsigned int timeWait = 400;
  179.  
  180. //Variables to hold the new and old switch states for debouncing:
  181. bool newSwitchState[30] = {LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW,
  182.                            LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW,
  183.                            LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW
  184.                           };
  185.  
  186. bool oldSwitchState[10] = {LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW};
  187.  
  188. long unsigned currentDebounceTime = 0;
  189. long unsigned lastDebounceTime = 0;
  190.  
  191. //Switch states for debouncing:
  192. bool keyPressed[10] = {false, false, false, false, false, false, false, false, false, false};
  193.  
  194. //Long press variables:
  195. long buttonTimer = 0;
  196. long longPressTime = 700;
  197.  
  198. boolean button6Active = false;
  199. boolean longPress6Active = false;
  200. boolean button7Active = false;
  201. boolean longPress7Active = false;
  202.  
  203. //Set up MIDI communication:
  204. MIDI_CREATE_DEFAULT_INSTANCE();
  205.  
  206. void setup()
  207. {
  208.  
  209.    
  210.   for (int p = 0; p < ledCnt; p++)
  211.   {
  212.     pinMode(led[p], OUTPUT);
  213.   }
  214.  
  215.   for (int b = 0; b < pressCount; b++)
  216.   {
  217.     pinMode(pressed[b], INPUT);
  218.   }
  219. Control_Surface.begin();    // Control surface library init
  220.   //Initialise TFT:
  221.   tft.begin();
  222.  
  223.   tft.setRotation(iliRotation270); //Flip screen upside down because of interfering wires.
  224.  
  225.   bootSequence();
  226.  
  227.   MIDI.begin();           //Set up MIDI output.
  228.   //Serial.begin(115200); //For debugging.
  229.  
  230.   pedalMin = EEPROM.get(ADDR_MIN_VAL, pedalMin); //Load pedal min and max values from eeprom.
  231.   delay(100);
  232.   pedalMax = EEPROM.get(ADDR_MAX_VAL, pedalMax);
  233.   delay(100);
  234.   //Serial.println(pedalMin);
  235.   //Serial.println(pedalMax);
  236.  
  237.  
  238.  
  239.  
  240.  
  241.    
  242.  
  243. }
  244.  
  245. void loop()
  246. {
  247.  
  248.  Control_Surface.loop();
  249.   currentDebounceTime = millis();
  250.   debounceTime();
  251.   wah();
  252.  
  253.   //Every 10 readings of the analog pin connected to the battery's + terminal, average them:
  254.   float analogSmooth = as.analogReadSmooth(battPin);
  255.   battRead = analogSmooth;
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.   if (millis() - battStartupTime > 60000 && millis() - battStartupTime < 60100)
  264.   {
  265.     if (battRead < 640) //If it reads less than 3.1v it's on USB power.
  266.     {
  267.       tft.fillRect(1, 223, 35, 16, ILI9341_BLACK);
  268.       tft.setFont(Arial_bold_14);
  269.       tft.setTextColor(ILI9341_CYAN);
  270.       tft.setTextScale(1);
  271.       tft.printAt("USB", 33, 200);
  272.       tft.printAt("POWER", 20, 219);
  273.     }
  274.  
  275.     else
  276.     {
  277.       tft.drawRoundRect(20, 185, 53, 25, 3, ILI9341_WHITE); //Draw battery
  278.       tft.drawRoundRect(21, 186, 51, 23, 3, ILI9341_WHITE);
  279.       tft.fillRect(73, 193, 4, 8, ILI9341_WHITE);
  280.  
  281.       tft.setTextColor(ILI9341_CYAN, ILI9341_BLACK);
  282.       tft.setTextScale(1);
  283.       tft.printAt("Batt:", 1, 223);
  284.      
  285.       usbOn = false;
  286.     }
  287.   }
  288.  
  289.   //Print averaged battery voltage every 30 seconds:
  290.   currentReading = millis();
  291.   readBattery();
  292.  
  293.   tft.setFont(Arial_bold_14);
  294.  
  295.   //See which mode is active for bank buttons 7 & 8). Press wah and button 7 to cycle through controller bank change mode (no MIDI), and bank & preset up/down in software (MIDI sent):
  296.   bankMode();
  297.  
  298.   //What happens when cycling through preset banks (not software banks):
  299.   presetBanks();
  300.  
  301.   //Determine which bank is active:
  302.   presetBankStatus();
  303.  
  304.   //Check if pedalboard mode has been activated:
  305.   if (keyPressed[8])
  306.   {
  307.     stompActive();
  308.     keyPressed[8] = false;
  309.   }
  310.  
  311.   //Check if loop mode has been activated:
  312.   if (keyPressed[9])
  313.   {
  314.     loopActive();
  315.     keyPressed[9] = false;
  316.   }
  317.  
  318.   //Turn MIDI LED off if it's on:
  319.   midiLedOff();
  320.  
  321.   if (loopStatus == false)
  322.   {
  323.     lastLed = activeLed;
  324.   }
  325. }
  326.  
  327.  
  328.  
  329. //END OF LOOP().
  330.  
  331.  
  332. //CLEAR SPACES:
  333. void clearSmallPresets()   //Clear small preset names but leave large one.
  334. {
  335.   tft.fillRect(1, 1, 320, 16, ILI9341_BLACK);
  336.   tft.fillRect(1, 90, 320, 16, ILI9341_BLACK);
  337. }
  338.  
  339. void clearTopHalf()
  340. {
  341.   tft.fillRect(0, 1, 320, 152, ILI9341_BLACK);
  342. }
  343.  
  344. void clearLargeName()     //Clear large preset name.
  345. {
  346.   tft.fillRect(1, 40, 320, 40, ILI9341_BLACK);
  347. }
  348.  
  349. void clearBankSpace()     //Clear bank number space.
  350. {
  351.   tft.fillRect(95, 152, 85, 22, ILI9341_BLACK);
  352. }
  353.  
  354. void clearMode()          //Clear loop/stomp/preset mode on message.
  355. {
  356.   tft.fillRect(194, 154, 120, 20, ILI9341_BLACK);
  357. }
  358.  
  359. void clearBankMode()      //Bank up/down buttons' status space.
  360. {
  361.   tft.fillRect(1, 130, 190, 20, ILI9341_BLACK);
  362. }
  363.  
  364. void clearPedalName()
  365. {
  366.   tft.fillRect(230, 222, 40, 18, ILI9341_BLACK);
  367. }
Advertisement
Add Comment
Please, Sign In to add comment