Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.46 KB | None | 0 0
  1.  
  2.  
  3. //Todo
  4. //Remove use of analog pins for easier swapping between Uno and Teensy due to 3.3 limit on Teensy
  5. //For this reason it is a good idea to connect OUTPUT pins to other devices with 470Ω or 1k resistors, unless maximum current draw from the pins is required for a particular application.
  6.  
  7. //https://programmingelectronics.com/make-one-button-functionality-two-arduino/
  8. //https://github.com/mathertel/OneButton
  9. //http://www.mathertel.de/Arduino/OneButtonLibrary.aspx
  10.  
  11. //Libraries
  12. #include <FreqCount.h>
  13. #include <LiquidCrystal.h>
  14. #include <OneButton.h>
  15. #include <EEPROM.h>
  16.  
  17. //Floating Map Function
  18. float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
  19. {
  20. return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  21. }
  22.  
  23. //Pin Setup, 0 for Uno, 1 for Teensy 3.1
  24. //Only works on Teensy 3.1 due to need for 5v
  25. //THC A-D Input must be on pin 5 on Uno or pin 13 on Teensy 3.1
  26.  
  27. //LCD Pins
  28. #define LCD_RS 12
  29. #define LCD_EN 11
  30. #define LCD_D4 6
  31. #define LCD_D5 4
  32. #define LCD_D6 8
  33. #define LCD_D7 7
  34. #define LCD_CHARS 20
  35. #define LCD_LINES 4
  36.  
  37. //Encoder Pins
  38. int encoderPinA = 3;
  39. int encoderPinB = 2;
  40.  
  41. //New OneButton
  42. OneButton button(13,true);
  43.  
  44.  
  45.  
  46. //LCD Pin Setup and Custom Character
  47. LiquidCrystal lcd(LCD_RS, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
  48. uint8_t testChar[8] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff}; // Custom char
  49.  
  50.  
  51. //Variable Setup
  52. int16_t last, value, testval, tipval, TargetVal, EncoderValue;
  53. bool flash; //Bool value for flashing text on screen
  54. unsigned long startMillis, torchMillis, currentMillis, count; //some global variables available anywhere in the program
  55. long int frq;
  56. float TorchVal;
  57.  
  58.  
  59. int lastEncoded;
  60. int TorchHysteresis; //Voltage offset from target before outputs activate
  61.  
  62.  
  63.  
  64.  
  65. void setup() {
  66.  
  67. FreqCount.begin(10);
  68.  
  69. //Variable setup
  70. flash = false;
  71. lastEncoded = 0;
  72. EncoderValue = EEPROM.read(0); //Read starting value from EEPROM 0
  73. TargetVal = EncoderValue; //Set this equal to EncoderVal during setup
  74. TorchHysteresis = 4; //Voltage offset from target before outputs activate
  75.  
  76.  
  77. //Timer setup
  78. startMillis = millis();
  79. torchMillis = millis();
  80.  
  81.  
  82. //Button setup
  83. button.attachClick(ClickFunction); //Create Click function for button
  84. button.attachDoubleClick(DoubleClickFunction); //Create Double Click function for button
  85. button.attachLongPressStart(HoldFunction); //Create Hold function for button
  86.  
  87.  
  88. //Output setup
  89. pinMode(9, OUTPUT); //Torch Up output
  90. digitalWrite(9, LOW); //Turn output off
  91. pinMode(10, OUTPUT); //Torch Down output
  92. digitalWrite(10, LOW); //Turn output off
  93.  
  94.  
  95. //LCD Setup
  96. lcd.begin(LCD_CHARS, LCD_LINES);
  97. lcd.clear();
  98. lcd.createChar(0, testChar); //Create custom full block char and send to LCD
  99.  
  100.  
  101. //LCD startup message
  102. lcd.setCursor(0,0);
  103. lcd.print("CNC Plasma Torch");
  104. lcd.setCursor(0,1);
  105. lcd.print("Height Control");
  106. delay(500);
  107. lcd.clear();
  108.  
  109.  
  110. //LCD text setup
  111. lcd.setCursor(0, 0);
  112. lcd.print("Tip Voltage:");
  113. lcd.setCursor(0, 1);
  114. lcd.print("Tip Target:");
  115. lcd.setCursor(12, 1);
  116. lcd.print(TargetVal); //Write initial TargetVal, updated by button press
  117. lcd.setCursor(0,3);
  118. lcd.print("UP:");
  119. lcd.setCursor(6,3);
  120. lcd.print("Down:");
  121. lcd.setCursor(14,3);
  122. lcd.print("Off:");
  123.  
  124.  
  125. //Encoder Setup
  126. pinMode(encoderPinA, INPUT_PULLUP);
  127. pinMode(encoderPinB, INPUT_PULLUP);
  128.  
  129. //get starting position
  130. int lastMSB = digitalRead(encoderPinA);
  131. int lastLSB = digitalRead(encoderPinB);
  132.  
  133. //let start be lastEncoded so will index on first click
  134. lastEncoded = (lastMSB << 1) |lastLSB;
  135.  
  136. }//Setup
  137.  
  138. void loop() {
  139.  
  140. currentMillis = millis(); //Grab current loop time, millis used to not delay code execution
  141.  
  142. button.tick(); //Check button for input
  143.  
  144.  
  145. int MSB = digitalRead(encoderPinA); //MSB = most significant bit
  146. int LSB = digitalRead(encoderPinB); //LSB = least significant bit
  147. int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number
  148. int sum = (lastEncoded << 2) | encoded; //adding it to the previous encoded value
  149. if(sum == 0b1101 || sum == 0b0010)EncoderValue ++;
  150. if(sum == 0b1110 || sum == 0b0001)EncoderValue --;
  151.  
  152.  
  153. lastEncoded = encoded; //store this value for next time
  154.  
  155. if (FreqCount.available()) {
  156. TorchVal = mapfloat(FreqCount.read(), 1197, 9265, 0.0, 10.0);
  157. TorchVal = TorchVal*50;
  158. }
  159.  
  160. if (currentMillis - torchMillis >= 250) { //Delay printing of TorchVal to make it easier to read
  161. torchMillis = millis();
  162. lcd.setCursor(13,0);
  163. lcd.print(" ");
  164.  
  165. lcd.setCursor(13,0);
  166. if (TorchVal <= 0){
  167. lcd.print("-----");
  168. }
  169. else{
  170. lcd.print(TorchVal,1);
  171. }
  172. }
  173.  
  174. if(EncoderValue != TargetVal){
  175. if(EncoderValue <= 100)EncoderValue = 100; //Don't let EncoderValue below 100
  176. if(EncoderValue >= 150)EncoderValue = 150; //Don't let EncoderValue above 150
  177. lcd.setCursor(12, 1);
  178. lcd.print(EncoderValue);
  179.  
  180. //Begin code for flashing Tip Target text
  181. if (currentMillis - startMillis >= 500 && flash) { //Hide Tip Target text
  182. startMillis = millis();
  183. lcd.setCursor(0, 1);
  184. lcd.print(" ");
  185. flash = false;
  186. }
  187. else if(currentMillis - startMillis >= 300 && !flash) { //Show Tip Target text
  188. startMillis = millis();
  189. lcd.setCursor(0, 1);
  190. lcd.print("Tip Target:");
  191. flash = true;
  192. }
  193. }
  194. else{ //Make sure flashing text is returned to on in case we exit while off
  195. lcd.setCursor(0, 1);
  196. lcd.print("Tip Target:");
  197. lcd.setCursor(12, 1);
  198. lcd.print(EncoderValue);
  199. }
  200.  
  201.  
  202. //Code for moving torch based on tip value compared to requested value, add outputs here.
  203. if(TorchVal < (TargetVal - TorchHysteresis)){ //Voltage too low, raise torch
  204. //Turn Down off, Up on
  205. digitalWrite(10, LOW);
  206. digitalWrite(9, HIGH);
  207. //Print Square
  208. lcd.setCursor(4,3);
  209. lcd.print((char)0);
  210. //Clear Other Squares
  211. lcd.setCursor(12,3);
  212. lcd.print(" ");
  213. lcd.setCursor(19,3);
  214. lcd.print(" ");
  215. } //End If
  216. else if(TorchVal > (TargetVal + TorchHysteresis)){ //Voltage too high, lower torch
  217. //Turn Up off, Down on
  218. digitalWrite(9, LOW);
  219. digitalWrite(10, HIGH);
  220. //Print Square
  221. lcd.setCursor(12,3);
  222. lcd.print((char)0);
  223. //Clear Other Squares
  224. lcd.setCursor(19,3);
  225. lcd.print(" ");
  226. lcd.setCursor(4,3);
  227. lcd.print(" ");
  228. } //End Else If
  229. else { //Voltage Stable - Outputs Off
  230. //Turn both outputs off
  231. digitalWrite(10, LOW);
  232. digitalWrite(9, LOW);
  233. //Print Square
  234. lcd.setCursor(19,3);
  235. lcd.print((char)0);
  236. //Clear Other Squares
  237. lcd.setCursor(4,3);
  238. lcd.print(" ");
  239. lcd.setCursor(12,3);
  240. lcd.print(" ");
  241. } //End Else
  242.  
  243.  
  244. }// Loop
  245.  
  246.  
  247. void ClickFunction() { //Runs on single click of encoder button
  248. TargetVal = EncoderValue; //Save current EncoderValue to TargetVal
  249. }
  250.  
  251. void DoubleClickFunction() { //Runs on double click of encoder button
  252. EncoderValue = EEPROM.read(0); //Recall saved value from EEPROM 0
  253. }
  254.  
  255. void HoldFunction() { //Runs on hold of encoder button
  256. EEPROM.write(0,0); //Clear EEPROM before write, maybe unnecessary
  257. EEPROM.write(0, TargetVal); //Write current TargetVal to EEPROM 0
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement