Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.69 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <LiquidCrystal_I2C.h>
  3. #include "i2cEncoderLib.h"
  4.  
  5.  
  6.  
  7.  
  8. //All encoder setup
  9. #define ENCODER_N 2
  10. i2cEncoderLib encoder[ENCODER_N] = { i2cEncoderLib(0x3D), i2cEncoderLib(0x30)}; //Class initialization with the I2C addresses
  11.  
  12. uint8_t counter[ENCODER_N] = {0, 0};
  13. uint8_t maxvalue[ENCODER_N] = {100, 100};
  14. uint8_t minvalue[ENCODER_N] = {0, 0};
  15.  
  16. int32_t econfig[ENCODER_N] = {
  17. (INTE_DISABLE | LEDE_DISABLE | WRAP_DISABLE | DIRE_LEFT | IPUP_ENABLE | RMOD_X2),
  18. (INTE_DISABLE | LEDE_DISABLE | WRAP_DISABLE | DIRE_LEFT | IPUP_ENABLE | RMOD_X2)
  19. };
  20.  
  21. // These are currently unnecessary
  22. // const uint8_t Intpin_1 = 3;
  23. // const uint8_t Intpin_2 = 2;
  24.  
  25.  
  26. // Screen object
  27. LiquidCrystal_I2C LCD_Screen(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
  28.  
  29.  
  30. // Serial communication variables
  31. const uint32_t baudRate = 115200;
  32. const char HANDSHAKE = '@';
  33. const char VolumeRequest = '^';
  34. const char ChangeMarker[2] = {'#', '$'};
  35.  
  36.  
  37. // Program names and volume values
  38. // Volumes 1, 2, master
  39. uint8_t Volumes[3] = {0, 0, 0};
  40.  
  41. char *CurrName;
  42. char Name1[17];
  43. char Name2[17];
  44.  
  45. uint16_t incoming;
  46. char data;
  47.  
  48. const char VolumeBreak = '!';
  49.  
  50. // Master slider settings
  51. const uint8_t Slider_Pin = A2;
  52. const uint16_t LowerBound = 5;
  53. const uint16_t UpperBound = 1019;
  54.  
  55.  
  56. void setup(void){
  57.  
  58. for (uint8_t i = 0; i < ENCODER_N; i++) {
  59. encoder[i].begin(econfig[i]);
  60. encoder[i].writeCounter(counter[i]);
  61. encoder[i].writeMax(maxvalue[i]);
  62. encoder[i].writeMin(minvalue[i]);
  63. encoder[i].updateStatus();
  64. }
  65.  
  66. Name1[16] = '\0'; // Setting termination characters on the arrays
  67. Name2[16] = '\0';
  68.  
  69. LCD_Screen.begin(16, 2);
  70. LCD_Screen.backlight();
  71. LCD_Screen.setCursor(0, 0);
  72. LCD_Screen.print("Waiting for USB");
  73.  
  74. Serial.begin(baudRate);
  75. Serial.print(HANDSHAKE);
  76. LCD_Screen.clear();
  77. LCD_Screen.print("Press button");
  78. LCD_Screen.setCursor(0, 1);
  79. LCD_Screen.print("to start");
  80.  
  81. }
  82.  
  83.  
  84. void loop(){
  85.  
  86. if (Serial.available()) {
  87.  
  88. incoming = Serial.read();
  89. data = char(incoming);
  90.  
  91. if (data == VolumeRequest) {
  92.  
  93. sendVolumes();
  94.  
  95. }
  96.  
  97. else if (data == HANDSHAKE) {
  98.  
  99. Serial.print(HANDSHAKE);
  100.  
  101. }
  102.  
  103. else if (data == ChangeMarker[0]) {
  104.  
  105. receiveName(0);
  106.  
  107. }
  108.  
  109. else if (data == ChangeMarker[1]) {
  110.  
  111. receiveName(1);
  112.  
  113. }
  114.  
  115. }
  116.  
  117. else if (encoder[0].updateStatus()) {
  118.  
  119. readEncoder(0);
  120.  
  121. }
  122.  
  123. else if (encoder[1].updateStatus()) {
  124.  
  125. readEncoder(1);
  126.  
  127. }
  128.  
  129. }
  130.  
  131.  
  132.  
  133.  
  134. /*
  135. Reads encoder ii and if the button is pressed or the volume has changed
  136. ii is 0 or 1
  137. Returns the volume change or if the button has been pushed, calls change program and returns nothing
  138. */
  139. void readEncoder(uint8_t ii) {
  140.  
  141.  
  142. if (encoder[ii].readStatus(E_PUSH)) {
  143.  
  144. changeProgram(ii);
  145.  
  146. }
  147.  
  148. else {
  149.  
  150. encoder[ii].readStatus();
  151. Volumes[ii] = encoder[ii].readCounterByte();
  152.  
  153. }
  154.  
  155. return;
  156. }
  157.  
  158.  
  159. /*
  160. Tells PC to change program channel ii
  161. Called from ReadEncoder when button is pushed
  162. */
  163. void changeProgram(uint8_t ii) {
  164. //This is an awful way to do it but it works, different change marker for each channel
  165. serialFlush();
  166. Serial.print(ChangeMarker[ii]);
  167. delay(10); // Give PC time to read and respond with the next program name
  168. receiveName(ii); // Captures the name followed by the same change marker to end
  169. return;
  170. }
  171.  
  172.  
  173. /*
  174. Prints a list of all volumes to serial
  175. Starting with and seperated by ! - VolumeBreak
  176. Encoder 1, encoder 2 and then master
  177. */
  178. void sendVolumes(void) {
  179.  
  180. serialFlush();
  181. Serial.print(VolumeBreak);
  182.  
  183. uint16_t UnmappedVolume = analogRead(Slider_Pin);
  184.  
  185. Volumes[2] = map(UnmappedVolume, LowerBound, UpperBound, 0, 100);
  186.  
  187. for (uint8_t i = 0; i < 3; i++) {
  188.  
  189. Serial.print(Volumes[i]);
  190. Serial.print(VolumeBreak);
  191.  
  192. }
  193.  
  194. return;
  195. }
  196.  
  197.  
  198. /*
  199. Reads the name of the new program from serial and prints it to the LCD
  200. Expects characters followed by the change marker again to signal the end of the name
  201. Needs to know which channel it is changing, 0 - first, 1 - second
  202. Will only take the first 16 characters
  203. Expects volume after the marker, finished ith VolumeBreak, then will flush serial
  204. */
  205. void receiveName(uint8_t ii) {
  206.  
  207. uint8_t i = 0;
  208.  
  209. if (ii == 0){
  210. CurrName = Name1;
  211. }
  212.  
  213. else if (ii == 1){
  214. CurrName = Name2;
  215. }
  216.  
  217.  
  218. while (i < 16) {
  219.  
  220. if (Serial.available()) {
  221.  
  222. incoming = Serial.read();
  223. data = char(incoming);
  224.  
  225. if (data == ChangeMarker[ii]) {
  226.  
  227. while (i < 16) {
  228. CurrName[i] = ' ';
  229. i++;
  230.  
  231. }
  232. }
  233.  
  234. else {
  235.  
  236. CurrName[i] = data;
  237. i++;
  238.  
  239. }
  240. }
  241.  
  242. else {
  243.  
  244. delayMicroseconds(1);
  245.  
  246. }
  247.  
  248.  
  249. }
  250.  
  251.  
  252. // Prints the name in its position
  253. LCD_Screen.setCursor(0, ii);
  254. LCD_Screen.print(CurrName);
  255.  
  256. // Now to get the volume
  257. recieveVolume(ii);
  258.  
  259. return;
  260.  
  261. }
  262.  
  263.  
  264. /*
  265. Takes in 0 or 1 and expects there to be a volume waiting in serial
  266. Expects <4 digits with VolumeBreak at the end
  267. Converts to int and writes directly into the Volumes array
  268. */
  269. void recieveVolume(uint8_t ii) {
  270.  
  271. uint8_t i = 0;
  272. bool HaveVolume = false;
  273. char VolumeCollect[3];
  274.  
  275. while (HaveVolume == false) {
  276.  
  277. if (Serial.available()) {
  278.  
  279. incoming = Serial.read();
  280. data = char(incoming);
  281.  
  282. if (data == VolumeBreak || i >=3) {
  283.  
  284. HaveVolume = true;
  285.  
  286. }
  287.  
  288. else {
  289.  
  290. VolumeCollect[i] = data;
  291. i++;
  292.  
  293. }
  294.  
  295. }
  296.  
  297. }
  298.  
  299. uint8_t intvol = atoi(VolumeCollect);
  300.  
  301. Volumes[ii] = intvol;
  302. encoder[ii].writeCounter(intvol);
  303.  
  304. return;
  305. }
  306.  
  307.  
  308. /*
  309. Does exactly what you'd expect
  310. */
  311. void serialFlush(void){
  312. while (Serial.available() > 0) {
  313. Serial.read();
  314. }
  315. return;
  316. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement