Guest User

Untitled

a guest
Jan 10th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.61 KB | None | 0 0
  1. #include <Nextion.h>
  2. #include <NexNumber.h>
  3. #include <NexDualStateButton.h>
  4. //Page0 objects
  5. NexButton b001 = NexButton(0, 1, "b0");
  6. NexButton b202 = NexButton(0, 2, "b2");
  7. NexButton b303 = NexButton(0, 3, "b3");
  8. NexButton b404 = NexButton(0, 4, "b4");
  9. NexDSButton bt007 = NexDSButton(0, 8, "bt0"); // Dual state button added
  10. NexNumber n005 = NexNumber(0,5,"n0");
  11.  
  12. //Page1 objects
  13. NexSlider h011 = NexSlider(1, 1, "h0"); // Slider added
  14. NexSlider h112 = NexSlider(1, 2, "h1"); // Slider added
  15. NexNumber n014 = NexNumber(1,4,"n0");
  16. NexNumber n115 = NexNumber(1,5,"n1");
  17.  
  18. // Declare touch event objects to the touch event list:
  19. NexTouch *nex_listen_list[] =
  20. {
  21. &b001, &b202, &b303, &b404, &bt007, &n005, &h011, &h112, &n014, &n115,
  22. NULL // String terminated
  23. }; // End of Nextion touch event list
  24.  
  25. void b001PopCallback(void *ptr) // Release event for button b1
  26. {
  27.  
  28. } // End of release event
  29.  
  30. void b202PopCallback(void *ptr) // Release event for button b2
  31. {
  32.  
  33. } // End of release event
  34.  
  35. void b303PopCallback(void *ptr) // Release event for button b3
  36. {
  37.  
  38. } // End of release event
  39.  
  40. void b404PopCallback(void *ptr) // Release event for button b4
  41. {
  42.  
  43. } // End of release event
  44.  
  45. void bt007PopCallback(void *ptr) // Release event for button b1
  46. {
  47.  
  48. }
  49.  
  50. void h011PopCallback(void *ptr) // Release event for slider
  51. {
  52. uint32_t number2 = 0; // Create variable to store value of slider
  53. h011.getValue(&number2); // Read the value of the slider
  54.  
  55. // I got a problem where sometimes I received a 0 instead of the correct slider value.
  56. // To fix this I will put a condition that if I get a 0, I am going to read again the slider to make sure I get the real value.
  57. // I am going to do this a few times because sometimes it takes a few tries to get the correct value.
  58. // The problem looks like it have something to do with touch events. Everytime the display sends a touch event,
  59. // we need to wait that data to finish transmiting before we can get another data from the display (in this case
  60. // we want the slider position). For this reason it's important to use a high serial baud (ideally 115200)
  61. // so it doesn't have to wait too long for the touch event to finish sending the data.
  62.  
  63. // The "Are you sure is 0?" begins:
  64. if(number2==0){ // If I got a 0, then recheck:
  65. h011.getValue(&number2); // Read the value of the slider
  66. }
  67. if(number2==0){ // If I got a 0, then recheck:
  68. h011.getValue(&number2); // Read the value of the slider
  69. }
  70. if(number2==0){ // If I got a 0, then recheck:
  71. h011.getValue(&number2); // Read the value of the slider
  72. }
  73. if(number2==0){ // If I got a 0, then recheck:
  74. h011.getValue(&number2); // Read the value of the slider
  75. }
  76. if(number2==0){ // If I got a 0, then recheck:
  77. h011.getValue(&number2); // Read the value of the slider
  78. }
  79. if(number2==0){ // If I got a 0, then recheck:
  80. h011.getValue(&number2); // Read the value of the slider
  81. }
  82. if(number2==0){ // If I got a 0, then recheck:
  83. h011.getValue(&number2); // Read the value of the slider
  84. }
  85. if(number2==0){ // If I got a 0, then recheck:
  86. h011.getValue(&number2); // Read the value of the slider
  87. }
  88. if(number2==0){ // If I got a 0, then recheck:
  89. h011.getValue(&number2); // Read the value of the slider
  90. }
  91. // The "Are you sure is 0?" ended. At this point, if the slider keep showing as 0, then it really is at 0.
  92.  
  93. // Now is going to send the value it received by the slider:
  94. Serial.print("n014.val="); // This is sent to the nextion display to set what object name (before the dot) and what atribute (after the dot) are you going to change.
  95. Serial.print(number2); // This is the value you want to send to that object and atribute mentioned before.
  96. Serial.write(0xff); // We always have to send this three lines after each command sent to the nextion display.
  97. Serial.write(0xff);
  98. Serial.write(0xff);
  99. } // End of release event
  100.  
  101. void h112PopCallback(void *ptr) // Release event for slider
  102. {
  103. uint32_t number3 = 0; // Create variable to store value of slider
  104. h112.getValue(&number3); // Read the value of the slider
  105.  
  106. // I got a problem where sometimes I received a 0 instead of the correct slider value.
  107. // To fix this I will put a condition that if I get a 0, I am going to read again the slider to make sure I get the real value.
  108. // I am going to do this a few times because sometimes it takes a few tries to get the correct value.
  109. // The problem looks like it have something to do with touch events. Everytime the display sends a touch event,
  110. // we need to wait that data to finish transmiting before we can get another data from the display (in this case
  111. // we want the slider position). For this reason it's important to use a high serial baud (ideally 115200)
  112. // so it doesn't have to wait too long for the touch event to finish sending the data.
  113.  
  114. // The "Are you sure is 0?" begins:
  115. if(number3==0){ // If I got a 0, then recheck:
  116. h112.getValue(&number3); // Read the value of the slider
  117. }
  118. if(number3==0){ // If I got a 0, then recheck:
  119. h112.getValue(&number3); // Read the value of the slider
  120. }
  121. if(number3==0){ // If I got a 0, then recheck:
  122. h112.getValue(&number3); // Read the value of the slider
  123. }
  124. if(number3==0){ // If I got a 0, then recheck:
  125. h112.getValue(&number3); // Read the value of the slider
  126. }
  127. if(number3==0){ // If I got a 0, then recheck:
  128. h112.getValue(&number3); // Read the value of the slider
  129. }
  130. if(number3==0){ // If I got a 0, then recheck:
  131. h112.getValue(&number3); // Read the value of the slider
  132. }
  133. if(number3==0){ // If I got a 0, then recheck:
  134. h112.getValue(&number3); // Read the value of the slider
  135. }
  136. if(number3==0){ // If I got a 0, then recheck:
  137. h112.getValue(&number3); // Read the value of the slider
  138. }
  139. if(number3==0){ // If I got a 0, then recheck:
  140. h112.getValue(&number3); // Read the value of the slider
  141. }
  142. // The "Are you sure is 0?" ended. At this point, if the slider keep showing as 0, then it really is at 0.
  143.  
  144. // Now is going to send the value it received by the slider:
  145. Serial.print("n115.val="); // This is sent to the nextion display to set what object name (before the dot) and what atribute (after the dot) are you going to change.
  146. Serial.print(number3); // This is the value you want to send to that object and atribute mentioned before.
  147. Serial.write(0xff); // We always have to send this three lines after each command sent to the nextion display.
  148. Serial.write(0xff);
  149. Serial.write(0xff);
  150. } // End of release event
  151.  
  152.  
  153. // predefined variables - dont touch or override !
  154. uint16_t DC1, DC2, DC3;
  155. int ph = 0; boolean dir = true;
  156. int i,N;
  157. int stageAngle;
  158.  
  159. //******************************************
  160. // pins for periferials :
  161. // digital 18 TX for touch screen
  162. // digital 19 RX for touch screen
  163. // digital 15 TX for BT
  164. // digital 14 RX for BT
  165. //
  166. //
  167. // ADD GLOBAL VARIABLES FOR COMMUNICATION procedures
  168. //******************************************
  169.  
  170. void setup() {
  171. // put your setup code here, to run once:
  172.  
  173. nexInit();
  174. Serial2.begin(9600); // Start serial comunication at baud=9600
  175. // I am going to change the Serial baud to a faster rate.
  176. // The reason is that the slider have a glitch when we try to read it's value.
  177. // One way to solve it was to increase the speed of the serial port.
  178. delay(500); // This dalay is just in case the nextion display didn't start yet, to be sure it will receive the following command.
  179. Serial2.print("baud=115200"); // Set new baud rate of nextion to 115200, but it's temporal. Next time nextion is power on,
  180. // it will retore to default baud of 9600.
  181. // To take effect, make sure to reboot the arduino (reseting arduino is not enough).
  182. // If you want to change the default baud, send the command as "bauds=115200", instead of "baud=115200".
  183. // If you change the default baud, everytime the nextion is power ON is going to have that baud rate, and
  184. // would not be necessery to set the baud on the setup anymore.
  185. Serial2.write(0xff); // We always have to send this three lines after each command sent to nextion.
  186. Serial2.write(0xff);
  187. Serial2.write(0xff);
  188. Serial2.end(); // End the serial comunication of baud=9600
  189. Serial2.begin(115200); // Start serial comunication at baud=115200
  190.  
  191. // NextionCode - Register the event callback functions of each touch event:
  192. b001.attachPop(b001PopCallback); // Button release
  193. b202.attachPop(b202PopCallback); // Button release
  194. b303.attachPop(b303PopCallback); // Button release
  195. b404.attachPop(b404PopCallback); // Button release
  196. bt007.attachPop(b404PopCallback); // Button release
  197. h011.attachPop(h011PopCallback); // Slider release
  198. // End of registering the event callback functions
  199.  
  200. //1. INITIALIZATION OF GPIOs
  201.  
  202. MCUCR |= 0x80; MCUCR |= 0x80; // disable j tag (must be done twice) page 301 atmel datasheet
  203. PORTA = 0xFF; PORTB = 0xFF; PORTC = 0xFF; PORTD = 0xFF; PORTE = 0xFF;
  204. PORTF = 0xFF; PORTG = 0xFF; PORTH = 0xFF; PORTJ = 0xFF; PORTK = 0xFF; PORTL = 0xFF;
  205.  
  206. //Configure GPIO direction and set outputs' initial values
  207. DDRB |= 0x80; //PB7 is an output (LED3)
  208. DDRD = 0x08; //PD3 is an output (TxD1)
  209. DDRE = 0xFF; //all PE pins are outputs (MOTOR 1)
  210. PORTE = 0x61; //to match hardware jumpers
  211. DDRF = 0xEF; //all PF pins are outputs (MOTOR 2)
  212. PORTF = 0x61; //to match hardware jumpers
  213. DDRG = 0x20; //PG5 is an output (STEP, MOTOR 2)
  214. PORTG = 0x00; //to match hardware jumper
  215. DDRK = 0x0F; //PK3 - PK0 are outputs (MOTOR 3)
  216.  
  217. DDRH = 0x38; //PH5, PH4, PH3 are outputs (counter 4, PWM)
  218. DDRJ = 0x05; //PJ2 is HM-10 enable, PH0 is TxD3
  219.  
  220. //2. INITIALIZATION OF PERIPHERALS (alternate functions of the GPIO lines)
  221. // data direction (input or output) is already defined!
  222.  
  223. //Configure PWM timer 4
  224. TCCR4A = 0xA9; //non-inverting mode for all channels, 8 bit resolution
  225. TCCR4B = 0x0B; //fast PWM, clk/64 (976 Hz -- tell me if you need other frequency)
  226.  
  227. OCR4AH = 0x00; OCR4AL = 256*DC1/100; //set duty cycle (DC1 = 1 to 99) Bottom LED
  228. OCR4BH = 0x00; OCR4BL = 256*DC2/100; //set duty cycle (DC2 = 1 to 99) Top LED
  229.  
  230. //3. Work functions related to system
  231. PORTK = 0x00; // low current for topMotor (overheat protect)
  232. //homeStage(); // run on start to init stage angle
  233.  
  234. }
  235.  
  236. void loop() {
  237. // NextionCode
  238. nexLoop(nex_listen_list); // Check for any touch event
  239.  
  240. stageMotor(800,0);
  241. tiltMotor(800,0);
  242. delay(10000);
  243. stageMotor(800,1);
  244. tiltMotor(800,1);
  245. delay(10000);
  246. //homeStage();
  247. topMotor(4096,1);
  248. delay(10000);
  249. topMotor(4096,0);
  250. delay(10000);
  251. for(i=0;i<100;i++){
  252. laserOn();
  253. delay (200);
  254. laserOff();
  255. delay(200);
  256. }
  257. for(i=0;i<100;i++){
  258. topLight(i);
  259. delay(200);
  260. }
  261.  
  262. }
  263.  
  264.  
  265. //********************************************************
  266. //********** bottom light intensity (0-100) *************
  267. //****** 0 = low light intensity (not turned off) *******
  268. //********************************************************
  269. //***** !!!!! electric schematic does not work !!!! *****
  270. //********************************************************
  271.  
  272. void bottomLight(int power){
  273. //digitalWrite(6,HIGH);
  274. OCR4AL = 256*power/100;
  275. }
  276.  
  277. //********************************************************
  278. //************ top light intensity (0-100) **************
  279. //****** 0 = low light intensity (not turned off) *******
  280. //********************************************************
  281.  
  282. void topLight(int power){
  283. //digitalWrite(7,HIGH);
  284. OCR4BL = 256*power/100;
  285. }
  286.  
  287.  
  288. //************************************************
  289. //************ center light on/off **************
  290. //************************************************
  291. void laserOn(){
  292. digitalWrite(8,HIGH);
  293. }
  294. void laserOff(){
  295. digitalWrite(8,LOW);
  296. }
  297.  
  298. //************************************************
  299. //******* Rotate top motor hinge X deg **********
  300. //************************************************
  301. void topMotor(int N,int dir){
  302. // full run = 4096 steps
  303. uint8_t phase[8] = {0x08, 0x0C, 0x04, 0x06, 0x02, 0x03, 0x01, 0x09};
  304. for(int i = 0; i < N; i++){
  305. PORTK = phase[ph]; //switch to current phase pattern
  306. if(dir){ph++; if(ph > 7){ph = 0;}} //modify phase index for the next step
  307. else{ph--; if(ph < 0){ph = 7;}} //depending on direction
  308. delay(1); //minimal feasible delay is 1 ms
  309. }//for
  310. PORTK = 0x00;
  311. }
  312.  
  313.  
  314. //************************************************
  315. //********* Rotate bottom stage X deg **********
  316. //************************************************
  317.  
  318.  
  319. void stageMotor (int steps,int dirx){
  320. //fullrun = 200 steps
  321. PORTE = 0x61;
  322. PORTE &= B11111110; //ENABLE
  323. PORTE &= B11111101; //MS1 0
  324. PORTE &= B11111011;//MS2 0
  325. PORTE &= B11110111; //MS3 = 0
  326. PORTE |= B00100000; //RESET 1
  327.  
  328. if (dirx==0) { PORTE |= B10000000;}
  329. if (dirx==1) { PORTE &= B01111111;}
  330. for (i=0 ;i<=steps;i++){
  331. PORTE |= B00010000; //STEP = 1
  332. delay(1); //pulse width in ms
  333. PORTE &= B11101111; //STEP = 0
  334. delay(1); //delay between pulses in ms
  335. }//for
  336. PORTE |= B00000001;
  337. }
  338.  
  339. //***********************************************
  340. //******** Move stage to 0/5/10/15 deg *********
  341. //******** procedure will be completed later ***
  342. //***********************************************
  343.  
  344. void tiltStage(int deg){
  345. // missign calclulation for variable angles currently set on 500 for testings
  346. if (stageAngle>deg){
  347. tiltMotor(500,0);
  348. }
  349. if (stageAngle<deg){
  350. tiltMotor(500,1);
  351. }
  352. }
  353.  
  354. //***********************************************
  355. //*********** Move stage to X deg **************
  356. //***********************************************
  357.  
  358. void tiltMotor(int steps,int dirx){
  359. //fullrun = 200 steps
  360. PORTF = 0x61;
  361. PORTF &= B11111110; //ENABLE
  362. PORTF &= B11111101; //MS1 0
  363. PORTF &= B11111011;//MS2 0
  364. PORTF &= B11110111; //MS3 0
  365. PORTF |= B00100000; //RESET 1
  366. if (dirx==0) { PORTF |= B10000000;}
  367. if (dirx==1) { PORTF &= B01111111;}
  368. for (i=0 ;i<=steps;i++){
  369. PORTG |= B00100000; //STEP = 1
  370. delay(1); //pulse width in ms
  371. PORTG &= B11011111; //STEP = 0
  372. delay(1); //delay between pulses in ms
  373. }//for
  374. PORTF |= B00000001;
  375. }
  376.  
  377.  
  378. //***********************************************
  379. //*********** Move stage to 0 deg **************
  380. //***********************************************
  381.  
  382. void homeStage(){
  383. PORTF = 0x61;
  384. PORTF &= B11111110; //ENABLE
  385. PORTF &= B11111101; //MS1 0
  386. PORTF &= B11111011;//MS2 0
  387. PORTF &= B11110111; //MS3 0
  388. PORTF |= B00100000; //RESET 1
  389. PORTF |= B10000000; // DIR
  390.  
  391. while( digitalRead(21) ){
  392. PORTG |= B00100000; //STEP = 1
  393. delay(1); //pulse width in ms
  394. PORTG &= B11011111; //STEP = 0
  395. delay(1); //delay between pulses in ms
  396. }//for
  397. PORTF |= B00000001;
  398. stageAngle=0;
  399.  
  400. }
Add Comment
Please, Sign In to add comment