Advertisement
WaffleMast3r

Untitled

Mar 28th, 2020
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. #include <LiquidCrystal.h>
  2. #include <String.h>
  3.  
  4. //stepper
  5. #define DIR 6
  6. #define STEP 7
  7. #define ANGLE 1.8
  8. const float STEPS_PER_REVOLUTION = 360 / ANGLE;
  9. int speeed = 25;
  10. //
  11.  
  12. //control
  13. #define X A0
  14. #define Y A1
  15. #define BTN 10
  16. //
  17.  
  18. //Define the LCD Screen
  19. const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
  20. LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
  21. //
  22.  
  23. //presets
  24. const int preset_count = 6;
  25. struct pair {
  26. int F;
  27. int C;
  28. char n[20];
  29. } presets[preset_count];
  30.  
  31. char* names[preset_count] = {"Nou-nascut", "Copil", "Adolescent", "Adult", "Varstnic", "Custom"};
  32. int Fs[preset_count] = {1, 2, 3, 4, 5, 3};
  33. int Cs[preset_count] = {1, 2, 3, 4, 5, 3};
  34. //
  35. int cPreset = 0;
  36. int stage = 0;
  37. int selection = 0;
  38.  
  39. void setup() {
  40. Serial.begin(9600);
  41.  
  42. //stepper
  43. pinMode(STEP, OUTPUT);
  44. pinMode(DIR, OUTPUT);
  45.  
  46. //Control
  47. pinMode(BTN, INPUT);
  48. digitalWrite(BTN, HIGH);
  49.  
  50. //presets
  51. for (int i = 0; i < preset_count; i++) {
  52. strcpy(presets[i].n, names[i]);
  53. presets[i].F = Fs[i];
  54. presets[i].C = Cs[i];
  55. }
  56.  
  57. //lcd
  58. lcd.begin(16, 2);
  59. lcd.print("OxiGeniuine v1.0");
  60.  
  61. delay(2000);
  62. updateDisplay();
  63. }
  64.  
  65. void loop() {
  66. int btn = buttonPressed(analogRead(X), analogRead(Y), digitalRead(BTN));
  67. if (stage == 0) {
  68. if (btn != -1) {
  69. switch (btn) {
  70. case 1:
  71. cPreset--;
  72. if (cPreset < 0) {
  73. cPreset = preset_count - 1;
  74. }
  75. updateDisplay();
  76. break;
  77. case 4:
  78. cPreset++;
  79. if (cPreset > preset_count - 1) {
  80. cPreset = 0;
  81. }
  82. updateDisplay();
  83. break;
  84. case 5:
  85. stage++;
  86. updateDisplay();
  87. break;
  88. }
  89. }
  90. } else if (stage == 1) {
  91. switch (btn) {
  92. case 1:
  93. selection = 0;
  94. break;
  95. case 4:
  96. selection = 1;
  97. break;
  98. case 5:
  99. switch (selection) {
  100. case 0:
  101.  
  102. break;
  103. case 1:
  104.  
  105. break;
  106. }
  107. break;
  108. }
  109. delay(300);
  110. } else if (stage == 2) {
  111. if (btn == 3) {
  112. if (!changingValue)
  113. if (++selection > 3) selection = 0;
  114. updateDisplay();
  115. delay(300);
  116. } else if (btn == 2) {
  117. if (!changingValue)
  118. if (--selection < 0) selection = 3;
  119. updateDisplay();
  120. delay(300);
  121. } else if (btn == 5) {
  122. if (selection == 0) {
  123. changingValue = !changingValue;
  124. } else if (selection == 1) {
  125. changingValue = !changingValue;
  126. } else if (selection == 2) {
  127. if (!changingValue) stage--;
  128. } else if (selection == 3) {
  129. //TODO: Start The motor
  130. }
  131. delay(300);
  132. }
  133. }
  134. }
  135.  
  136. void updateDisplay() {
  137. if (stage == 0) {
  138. lcd.clear();
  139. lcd.setCursor(0, 0);
  140. lcd.print("Programe:");
  141. lcd.setCursor(2, 1);
  142. lcd.print(presets[cPreset].n);
  143. } else if (stage == 1) {
  144. lcd.clear();
  145. lcd.setCursor(0, 0);
  146. lcd.print(presets[cPreset].n);
  147. lcd.setCursor(12, 0);
  148. lcd.print("| <-");
  149. lcd.setCursor(0, 1);
  150. lcd.print("F: | C: | ->");
  151. lcd.setCursor(3, 1);
  152. lcd.print(presets[cPreset].F);
  153. lcd.setCursor(10, 1);
  154. lcd.print(presets[cPreset].C);
  155.  
  156. lcd.blink();
  157. lcd.cursor();
  158. if (selection == 0) {
  159. lcd.setCursor(13, 0);
  160. } else if (selection == 1) {
  161. lcd .setCursor(13, 1);
  162. }
  163. } else if (stage == 2 || stage == 3) {
  164. lcd.clear();
  165. lcd.setCursor(0, 0);
  166. lcd.print(presets[cPreset].n);
  167. lcd.setCursor(12, 0);
  168. lcd.print("| <-");
  169. lcd.setCursor(0, 1);
  170. lcd.print("F: | C: | ->");
  171. lcd.setCursor(3, 1);
  172. lcd.print(presets[cPreset].F);
  173. lcd.setCursor(10, 1);
  174. lcd.print(presets[cPreset].C);
  175. if (selection == 0) {
  176. lcd.setCursor(3, 1);
  177. } else if (selection == 1) {
  178. lcd.setCursor(10, 1);
  179. } else if (selection == 2) {
  180. lcd.setCursor(13, 0);
  181. } else if (selection == 3) {
  182. lcd .setCursor(13, 1);
  183. }
  184. }
  185. }
  186.  
  187. //Based on global variables X Y BTN, returns a control option from 1 to 5, -1 being no command. acc is used to set the threshold for a joystick action
  188. float acc = 400;
  189. int buttonPressed(int x, int y, bool btn) {
  190. if (x - 500 > acc)
  191. return 4;
  192. else if ((-1) * (x - 500) > acc)
  193. return 1;
  194. else if (y - 500 > acc)
  195. return 3;
  196. else if ((-1) * (y - 500) > acc)
  197. return 2;
  198. else if (btn == false)
  199. return 5;
  200. return -1;
  201. }
  202.  
  203. /**
  204. Turn the stepper for steps, in CW direction if dir is 1 and CCW if dir is 0
  205. */
  206. void steps(int steps, int dir) {
  207. dir == 1 ? digitalWrite(DIR, HIGH) : digitalWrite(DIR, LOW);
  208. for (int i = 0; i < steps; i++) {
  209. digitalWrite(STEP, HIGH);
  210. delayMicroseconds(speeed);
  211. digitalWrite(STEP, LOW);
  212. delayMicroseconds(speeed);
  213. }
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement