Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Nextion.h>
- #include <NexNumber.h>
- #include <NexDualStateButton.h>
- //Page0 objects
- NexButton b001 = NexButton(0, 1, "b0");
- NexButton b202 = NexButton(0, 2, "b2");
- NexButton b303 = NexButton(0, 3, "b3");
- NexButton b404 = NexButton(0, 4, "b4");
- NexDSButton bt007 = NexDSButton(0, 8, "bt0"); // Dual state button added
- NexNumber n005 = NexNumber(0,5,"n0");
- //Page1 objects
- NexSlider h011 = NexSlider(1, 1, "h0"); // Slider added
- NexSlider h112 = NexSlider(1, 2, "h1"); // Slider added
- NexNumber n014 = NexNumber(1,4,"n0");
- NexNumber n115 = NexNumber(1,5,"n1");
- // Declare touch event objects to the touch event list:
- NexTouch *nex_listen_list[] =
- {
- &b001, &b202, &b303, &b404, &bt007, &n005, &h011, &h112, &n014, &n115,
- NULL // String terminated
- }; // End of Nextion touch event list
- void b001PopCallback(void *ptr) // Release event for button b1
- {
- } // End of release event
- void b202PopCallback(void *ptr) // Release event for button b2
- {
- } // End of release event
- void b303PopCallback(void *ptr) // Release event for button b3
- {
- } // End of release event
- void b404PopCallback(void *ptr) // Release event for button b4
- {
- } // End of release event
- void bt007PopCallback(void *ptr) // Release event for button b1
- {
- }
- void h011PopCallback(void *ptr) // Release event for slider
- {
- uint32_t number2 = 0; // Create variable to store value of slider
- h011.getValue(&number2); // Read the value of the slider
- // I got a problem where sometimes I received a 0 instead of the correct slider value.
- // 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.
- // I am going to do this a few times because sometimes it takes a few tries to get the correct value.
- // The problem looks like it have something to do with touch events. Everytime the display sends a touch event,
- // we need to wait that data to finish transmiting before we can get another data from the display (in this case
- // we want the slider position). For this reason it's important to use a high serial baud (ideally 115200)
- // so it doesn't have to wait too long for the touch event to finish sending the data.
- // The "Are you sure is 0?" begins:
- if(number2==0){ // If I got a 0, then recheck:
- h011.getValue(&number2); // Read the value of the slider
- }
- if(number2==0){ // If I got a 0, then recheck:
- h011.getValue(&number2); // Read the value of the slider
- }
- if(number2==0){ // If I got a 0, then recheck:
- h011.getValue(&number2); // Read the value of the slider
- }
- if(number2==0){ // If I got a 0, then recheck:
- h011.getValue(&number2); // Read the value of the slider
- }
- if(number2==0){ // If I got a 0, then recheck:
- h011.getValue(&number2); // Read the value of the slider
- }
- if(number2==0){ // If I got a 0, then recheck:
- h011.getValue(&number2); // Read the value of the slider
- }
- if(number2==0){ // If I got a 0, then recheck:
- h011.getValue(&number2); // Read the value of the slider
- }
- if(number2==0){ // If I got a 0, then recheck:
- h011.getValue(&number2); // Read the value of the slider
- }
- if(number2==0){ // If I got a 0, then recheck:
- h011.getValue(&number2); // Read the value of the slider
- }
- // The "Are you sure is 0?" ended. At this point, if the slider keep showing as 0, then it really is at 0.
- // Now is going to send the value it received by the slider:
- 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.
- Serial.print(number2); // This is the value you want to send to that object and atribute mentioned before.
- Serial.write(0xff); // We always have to send this three lines after each command sent to the nextion display.
- Serial.write(0xff);
- Serial.write(0xff);
- } // End of release event
- void h112PopCallback(void *ptr) // Release event for slider
- {
- uint32_t number3 = 0; // Create variable to store value of slider
- h112.getValue(&number3); // Read the value of the slider
- // I got a problem where sometimes I received a 0 instead of the correct slider value.
- // 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.
- // I am going to do this a few times because sometimes it takes a few tries to get the correct value.
- // The problem looks like it have something to do with touch events. Everytime the display sends a touch event,
- // we need to wait that data to finish transmiting before we can get another data from the display (in this case
- // we want the slider position). For this reason it's important to use a high serial baud (ideally 115200)
- // so it doesn't have to wait too long for the touch event to finish sending the data.
- // The "Are you sure is 0?" begins:
- if(number3==0){ // If I got a 0, then recheck:
- h112.getValue(&number3); // Read the value of the slider
- }
- if(number3==0){ // If I got a 0, then recheck:
- h112.getValue(&number3); // Read the value of the slider
- }
- if(number3==0){ // If I got a 0, then recheck:
- h112.getValue(&number3); // Read the value of the slider
- }
- if(number3==0){ // If I got a 0, then recheck:
- h112.getValue(&number3); // Read the value of the slider
- }
- if(number3==0){ // If I got a 0, then recheck:
- h112.getValue(&number3); // Read the value of the slider
- }
- if(number3==0){ // If I got a 0, then recheck:
- h112.getValue(&number3); // Read the value of the slider
- }
- if(number3==0){ // If I got a 0, then recheck:
- h112.getValue(&number3); // Read the value of the slider
- }
- if(number3==0){ // If I got a 0, then recheck:
- h112.getValue(&number3); // Read the value of the slider
- }
- if(number3==0){ // If I got a 0, then recheck:
- h112.getValue(&number3); // Read the value of the slider
- }
- // The "Are you sure is 0?" ended. At this point, if the slider keep showing as 0, then it really is at 0.
- // Now is going to send the value it received by the slider:
- 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.
- Serial.print(number3); // This is the value you want to send to that object and atribute mentioned before.
- Serial.write(0xff); // We always have to send this three lines after each command sent to the nextion display.
- Serial.write(0xff);
- Serial.write(0xff);
- } // End of release event
- // predefined variables - dont touch or override !
- uint16_t DC1, DC2, DC3;
- int ph = 0; boolean dir = true;
- int i,N;
- int stageAngle;
- //******************************************
- // pins for periferials :
- // digital 18 TX for touch screen
- // digital 19 RX for touch screen
- // digital 15 TX for BT
- // digital 14 RX for BT
- //
- //
- // ADD GLOBAL VARIABLES FOR COMMUNICATION procedures
- //******************************************
- void setup() {
- // put your setup code here, to run once:
- nexInit();
- Serial2.begin(9600); // Start serial comunication at baud=9600
- // I am going to change the Serial baud to a faster rate.
- // The reason is that the slider have a glitch when we try to read it's value.
- // One way to solve it was to increase the speed of the serial port.
- delay(500); // This dalay is just in case the nextion display didn't start yet, to be sure it will receive the following command.
- Serial2.print("baud=115200"); // Set new baud rate of nextion to 115200, but it's temporal. Next time nextion is power on,
- // it will retore to default baud of 9600.
- // To take effect, make sure to reboot the arduino (reseting arduino is not enough).
- // If you want to change the default baud, send the command as "bauds=115200", instead of "baud=115200".
- // If you change the default baud, everytime the nextion is power ON is going to have that baud rate, and
- // would not be necessery to set the baud on the setup anymore.
- Serial2.write(0xff); // We always have to send this three lines after each command sent to nextion.
- Serial2.write(0xff);
- Serial2.write(0xff);
- Serial2.end(); // End the serial comunication of baud=9600
- Serial2.begin(115200); // Start serial comunication at baud=115200
- // NextionCode - Register the event callback functions of each touch event:
- b001.attachPop(b001PopCallback); // Button release
- b202.attachPop(b202PopCallback); // Button release
- b303.attachPop(b303PopCallback); // Button release
- b404.attachPop(b404PopCallback); // Button release
- bt007.attachPop(b404PopCallback); // Button release
- h011.attachPop(h011PopCallback); // Slider release
- // End of registering the event callback functions
- //1. INITIALIZATION OF GPIOs
- MCUCR |= 0x80; MCUCR |= 0x80; // disable j tag (must be done twice) page 301 atmel datasheet
- PORTA = 0xFF; PORTB = 0xFF; PORTC = 0xFF; PORTD = 0xFF; PORTE = 0xFF;
- PORTF = 0xFF; PORTG = 0xFF; PORTH = 0xFF; PORTJ = 0xFF; PORTK = 0xFF; PORTL = 0xFF;
- //Configure GPIO direction and set outputs' initial values
- DDRB |= 0x80; //PB7 is an output (LED3)
- DDRD = 0x08; //PD3 is an output (TxD1)
- DDRE = 0xFF; //all PE pins are outputs (MOTOR 1)
- PORTE = 0x61; //to match hardware jumpers
- DDRF = 0xEF; //all PF pins are outputs (MOTOR 2)
- PORTF = 0x61; //to match hardware jumpers
- DDRG = 0x20; //PG5 is an output (STEP, MOTOR 2)
- PORTG = 0x00; //to match hardware jumper
- DDRK = 0x0F; //PK3 - PK0 are outputs (MOTOR 3)
- DDRH = 0x38; //PH5, PH4, PH3 are outputs (counter 4, PWM)
- DDRJ = 0x05; //PJ2 is HM-10 enable, PH0 is TxD3
- //2. INITIALIZATION OF PERIPHERALS (alternate functions of the GPIO lines)
- // data direction (input or output) is already defined!
- //Configure PWM timer 4
- TCCR4A = 0xA9; //non-inverting mode for all channels, 8 bit resolution
- TCCR4B = 0x0B; //fast PWM, clk/64 (976 Hz -- tell me if you need other frequency)
- OCR4AH = 0x00; OCR4AL = 256*DC1/100; //set duty cycle (DC1 = 1 to 99) Bottom LED
- OCR4BH = 0x00; OCR4BL = 256*DC2/100; //set duty cycle (DC2 = 1 to 99) Top LED
- //3. Work functions related to system
- PORTK = 0x00; // low current for topMotor (overheat protect)
- //homeStage(); // run on start to init stage angle
- }
- void loop() {
- // NextionCode
- nexLoop(nex_listen_list); // Check for any touch event
- stageMotor(800,0);
- tiltMotor(800,0);
- delay(10000);
- stageMotor(800,1);
- tiltMotor(800,1);
- delay(10000);
- //homeStage();
- topMotor(4096,1);
- delay(10000);
- topMotor(4096,0);
- delay(10000);
- for(i=0;i<100;i++){
- laserOn();
- delay (200);
- laserOff();
- delay(200);
- }
- for(i=0;i<100;i++){
- topLight(i);
- delay(200);
- }
- }
- //********************************************************
- //********** bottom light intensity (0-100) *************
- //****** 0 = low light intensity (not turned off) *******
- //********************************************************
- //***** !!!!! electric schematic does not work !!!! *****
- //********************************************************
- void bottomLight(int power){
- //digitalWrite(6,HIGH);
- OCR4AL = 256*power/100;
- }
- //********************************************************
- //************ top light intensity (0-100) **************
- //****** 0 = low light intensity (not turned off) *******
- //********************************************************
- void topLight(int power){
- //digitalWrite(7,HIGH);
- OCR4BL = 256*power/100;
- }
- //************************************************
- //************ center light on/off **************
- //************************************************
- void laserOn(){
- digitalWrite(8,HIGH);
- }
- void laserOff(){
- digitalWrite(8,LOW);
- }
- //************************************************
- //******* Rotate top motor hinge X deg **********
- //************************************************
- void topMotor(int N,int dir){
- // full run = 4096 steps
- uint8_t phase[8] = {0x08, 0x0C, 0x04, 0x06, 0x02, 0x03, 0x01, 0x09};
- for(int i = 0; i < N; i++){
- PORTK = phase[ph]; //switch to current phase pattern
- if(dir){ph++; if(ph > 7){ph = 0;}} //modify phase index for the next step
- else{ph--; if(ph < 0){ph = 7;}} //depending on direction
- delay(1); //minimal feasible delay is 1 ms
- }//for
- PORTK = 0x00;
- }
- //************************************************
- //********* Rotate bottom stage X deg **********
- //************************************************
- void stageMotor (int steps,int dirx){
- //fullrun = 200 steps
- PORTE = 0x61;
- PORTE &= B11111110; //ENABLE
- PORTE &= B11111101; //MS1 0
- PORTE &= B11111011;//MS2 0
- PORTE &= B11110111; //MS3 = 0
- PORTE |= B00100000; //RESET 1
- if (dirx==0) { PORTE |= B10000000;}
- if (dirx==1) { PORTE &= B01111111;}
- for (i=0 ;i<=steps;i++){
- PORTE |= B00010000; //STEP = 1
- delay(1); //pulse width in ms
- PORTE &= B11101111; //STEP = 0
- delay(1); //delay between pulses in ms
- }//for
- PORTE |= B00000001;
- }
- //***********************************************
- //******** Move stage to 0/5/10/15 deg *********
- //******** procedure will be completed later ***
- //***********************************************
- void tiltStage(int deg){
- // missign calclulation for variable angles currently set on 500 for testings
- if (stageAngle>deg){
- tiltMotor(500,0);
- }
- if (stageAngle<deg){
- tiltMotor(500,1);
- }
- }
- //***********************************************
- //*********** Move stage to X deg **************
- //***********************************************
- void tiltMotor(int steps,int dirx){
- //fullrun = 200 steps
- PORTF = 0x61;
- PORTF &= B11111110; //ENABLE
- PORTF &= B11111101; //MS1 0
- PORTF &= B11111011;//MS2 0
- PORTF &= B11110111; //MS3 0
- PORTF |= B00100000; //RESET 1
- if (dirx==0) { PORTF |= B10000000;}
- if (dirx==1) { PORTF &= B01111111;}
- for (i=0 ;i<=steps;i++){
- PORTG |= B00100000; //STEP = 1
- delay(1); //pulse width in ms
- PORTG &= B11011111; //STEP = 0
- delay(1); //delay between pulses in ms
- }//for
- PORTF |= B00000001;
- }
- //***********************************************
- //*********** Move stage to 0 deg **************
- //***********************************************
- void homeStage(){
- PORTF = 0x61;
- PORTF &= B11111110; //ENABLE
- PORTF &= B11111101; //MS1 0
- PORTF &= B11111011;//MS2 0
- PORTF &= B11110111; //MS3 0
- PORTF |= B00100000; //RESET 1
- PORTF |= B10000000; // DIR
- while( digitalRead(21) ){
- PORTG |= B00100000; //STEP = 1
- delay(1); //pulse width in ms
- PORTG &= B11011111; //STEP = 0
- delay(1); //delay between pulses in ms
- }//for
- PORTF |= B00000001;
- stageAngle=0;
- }
Add Comment
Please, Sign In to add comment