Start a new topic

no changing of pages...

My Problem:


I have a sensor tray driven by a stepper.

After turning it on, there is a time to pass to show an intro on the display after this there is a second page, which shows information, that the tray is doing a reference drive. Then another page is shown with two empty text-fields: one for the input and one for the calculated and driven distance.

The input from the keypad is calculated/ processed from single digits being entered one after the other to a 4-digit number, it shows the travelling distance in 1/10ths of a mm. This number is displayed in the first the two text fields. After calculating the steps for the stepper and waiting the time for the stepper to move, the number is displayed in the second text field and the first field is emptied. The program waits for the next input.

As you can see in the code below, I deactivated the code itself (it is the code working with the Nokia LCD), but left my first tries to show the different pages just after short time. I tried all three serials of my Mega, but nothing happens on the screen. Compiling runs through with no errors and the uploading is the same.


Someone out there to help me?



 

 

#include <Nextion.h>
#include <NexPage.h>

/*
#include <Keypad.h> //Keypad Library
#include <AccelStepper.h> // AccelStepper Library
#include <U8glib.h> // Nokia 5110 LCD Display Library//
*/

#define at_serial Serial3

NexPage page0 = NexPage(0, 0, "page0");
NexPage page1 = NexPage(0, 0, "page1");
NexPage page2 = NexPage(0, 0, "page2");



/*
volatile int ersteziffer=99;
volatile int zweiteziffer=99;
volatile int dritteziffer=99;
volatile int vierteziffer=99;

int eingabe=0; //speichert die berechnete Eingabe
String baer = ""; //Position Baer


const byte ROWS = 4; //4 Reihen
const byte COLS = 4; // 4 Spalten
char keys[ROWS][COLS] = {
  {'1','2','3','F'},
  {'4','5','6','E'},
  {'7','8','9','D'},
  {'A','0','B','C'}
};
byte rowPins[ROWS] = {22, 24, 26, 28};
byte colPins[COLS] = {31, 33, 35, 37};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );



//U8glib Setup für  Nokia 5110 LCD Display
#define backlight_pin 11
U8GLIB_PCD8544 u8g(3, 4, 6, 5, 7);  // Arduino Pins verbunden mit Nokia Pins, CLK=3, DIN=4, CE=6, DC=5, RST=7
                                   
                                    
// AccelStepper Treiber Setup
AccelStepper stepper(1, A0, A1); // 1 = Easy Driver Interface, Arduino A0 verbunden mit STEP Pin von Easy Driver, Arduino A1 verbunden mit DIR Pin von Easy Driver
*/
void setup() {
  // put your setup code here, to run once:
  
NexPage page0 = NexPage (0, 0, "page0");

delay(500);

}


/*  
  Serial.begin(9600);
  
//analogWrite(backlight_pin, 0);  // Dimmen der Hintergrundbeleuchtung  (0=Hell, 255=Dunkel)
  
  u8g.firstPage();
  do {
  u8g.drawHLine(0, 0, 84);
  u8g.drawHLine(0, 47, 84); 
  u8g.setFont(u8g_font_profont10);
  u8g.drawStr(5, 10, "Sensorsteuerung");
  u8g.setFont(u8g_font_profont11);
  u8g.drawStr(10, 21, "HSP Mod. IV");
  u8g.setFont(u8g_font_profont10);
  u8g.drawStr(21, 33, "(C) 2016");  
  u8g.drawStr(0, 44, "Frithjof Guettler");
  }
  while( u8g.nextPage() );

  delay(5000);
  
u8g.firstPage();
  do {
  u8g.drawHLine(0, 15, 84);
  u8g.drawVLine(50, 16, 38);
  u8g.drawHLine(0, 32, 84); 
  u8g.setFont(u8g_font_profont10);
  u8g.drawStr(2, 10, "Eingabe Distanz");
  u8g.drawStr(62, 27, "MM");
  u8g.drawStr(0, 44, "Baer-Pos.");
  }
  while( u8g.nextPage() );
  
}

*/
void loop() {
  // put your main code here, to run repeatedly:



NexPage page1 = NexPage (1, 0, "page1");

delay(500);

NexPage page2 = NexPage (1, 0, "page2");

delay(500);

}

/*
  char keypressed = keypad.getKey();  // Get value of keypad button if pressed
  if (keypressed != NO_KEY){  // If keypad button pressed check which key it was
    switch (keypressed) {
      
      case '1':
        checknumber(1);
      break;
        
      case '2':
        checknumber(2);
      break;
 
      case '3':
        checknumber(3);
      break;
 
      case 'F':
        checknumber(780);
      break;
      
      case '4':
        checknumber(4);
      break;
      
      case '5':
        checknumber(5);
      break;
 
      case '6':
        checknumber(6);
      break;
 
      case 'E':
        checknumber(850);
      break;

      case '7':
        checknumber(7);
      break;
       
      case '8':
        checknumber(8);
      break;
 
      case '9':
        checknumber(9);
      break;
 
      case 'D':
        checknumber(1000);
      break;
 
      case 'A':
        deletenumber();
      break;

      case '0':
        checknumber(0);
      break;      

      case 'B':
        calculatedistance();
      break;      
 
      case 'C':
        checknumber(1250);
      break;
    }
  }
}

void checknumber(int x){
  if (ersteziffer == 99) { // Prüfen ob erste Ziffer eingegeben wurde
    ersteziffer=x;
    String displayvalue = String(ersteziffer);  //  Wandelt die Eingabe in einen String um
    drawnokiascreen(displayvalue); // Wiederaufbau Nokia 5110 LCD Display
    
  } else {
  if (zweiteziffer == 99) { // Prüfen ob zweite Ziffer eingegeben wurde
    zweiteziffer=x;    String displayvalue = String (ersteziffer) + String(zweiteziffer);  //  Wandelt die Eingabe in einen String um
    drawnokiascreen(displayvalue); // Wiederaufbau Nokia 5110 LCD Display 

  } else {
  if (dritteziffer == 99) { // Prüfen ob dritte Ziffer eingegeben wurde
    dritteziffer=x;
    String displayvalue = String (ersteziffer) + String(zweiteziffer) + String(dritteziffer);  //  Wandelt die Eingabe in einen String um
    drawnokiascreen(displayvalue); // Wiederaufbau Nokia 5110 LCD Display 

    
  } else {  // Prüfen ob vierte Ziffer eingegeben wurde
      vierteziffer=x;
      String displayvalue = (String(ersteziffer) + String(zweiteziffer) + String(dritteziffer) + String(vierteziffer));
      drawnokiascreen(displayvalue);

      }
    }
  }
}

void deletenumber() {  // löscht die letzte eingegebene Ziffer
  if (vierteziffer !=99) {
      String displayvalue = String(ersteziffer) + String(zweiteziffer) + String(dritteziffer);
      drawnokiascreen(displayvalue);
 
      vierteziffer=99;
  } 
  else {
    if (dritteziffer !=99) {
      String displayvalue = String(ersteziffer) + String(zweiteziffer);
      drawnokiascreen(displayvalue);
 
      dritteziffer=99;
   } 
  else {
    if (zweiteziffer !=99) {
      String displayvalue = String(ersteziffer);
      drawnokiascreen(displayvalue);
 
      zweiteziffer=99;
   }  
  else {
     if (ersteziffer !=99) {
       String displayvalue = "";
       drawnokiascreen(displayvalue);
 
       ersteziffer=99;
        }
      }
    }
  }
}

void calculatedistance() {  // Berechnet aus den eingegebenen Ziffern eine bis zu 4-stellige Zahl
 
    if (vierteziffer == 99 && dritteziffer == 99 && zweiteziffer == 99 && ersteziffer != 99) {
      eingabe=ersteziffer;
      movestepper(eingabe);
    }

     if (zweiteziffer != 99 && dritteziffer == 99) {
      eingabe=(ersteziffer*10)+zweiteziffer;
      movestepper(eingabe);
    }

     if (dritteziffer != 99 && vierteziffer == 99) {
      eingabe=(ersteziffer*100)+(zweiteziffer*10)+dritteziffer;
      movestepper(eingabe);
    }

     if (vierteziffer != 99) {
      eingabe=(ersteziffer*1000)+(zweiteziffer*100)+(dritteziffer*10)+vierteziffer;
      movestepper(eingabe);
    }
    
    resetnumbers(); // Löscht die letzte Eingabe
  }

void movestepper(int z) {  //  Startet Schrittmotor
 
  int calculatedmove=((z*1600)/80);  //  Berechnet die Schrittzahl für die neue Eingabe
  stepper.runToNewPosition(calculatedmove);
  baer = String(z);
  u8g.firstPage();
    do {
      u8g.drawHLine(0, 15, 84);
      u8g.drawVLine(50, 16, 38);
      u8g.drawHLine(0, 32, 84); 
      u8g.setFont(u8g_font_profont10);
      u8g.drawStr(2, 10, "Eingabe Distanz");
      u8g.drawStr(62, 27, "MM");
      u8g.drawStr(0, 44, "Baer-Pos.");
      u8g.setPrintPos(54,46);
      u8g.print(baer); // Zeigt die aktuelle Position des Bärs an      
  }
  while( u8g.nextPage() ); 
}

void resetnumbers() {  // Löschen der letzten Zahl
  ersteziffer=99;
  zweiteziffer=99;
  dritteziffer=99;
  vierteziffer=99;
}


 void drawnokiascreen(String y) {
 
    u8g.firstPage();
    do {
      u8g.drawHLine(0, 15, 84);
      u8g.drawVLine(50, 16, 38);
      u8g.drawHLine(0, 32, 84); 
      u8g.setFont(u8g_font_profont10);
      u8g.drawStr(2, 10, "Eingabe Distanz");
      u8g.setPrintPos(0,27);
      u8g.print(y); //Zeigt eingegebene Distanz an     
      u8g.drawStr(62, 27, "MM");
      u8g.drawStr(0, 44, "Baer-Pos.");
      u8g.setPrintPos(54,46);
      u8g.print(baer); // Zeigt die aktuelle Position des Bärs an
    }
      while( u8g.nextPage() );
  
}

*/

 

 

 

 


tft
(1.03 MB)
HMI
(991 KB)
ino
(7.37 KB)

Lastly, ask questions and not a dump-and-post.


As you see, the users do not provide many answers when first they have to decode your code to see what you are doing.  Refer back to very rarely will two programmers ever have same code to solve the same problem.  Everyone thinks differently - so the code is not as the other thinks.


Page long compiler errors are not needed here, but more useful at compiler forum.  Only the first error is of any interest.  By fixing the first error, many errors below it go away because the lines they depend upon have already been fixed.


Use debugging methods.  When you don't know what is going on with your code other than it don't work, you need to add in debugging messages to yourself.  Such method in Arduino IDE is using the Serial.println command to send messages to your serial monitor.  Since code is run one line at a time from top to bottom in your loop you could if need be add Serial.println commands around everything you are not certain about.  In this manner you can print what you like (variable values at that line, mile markers - code goes here, or if you don't see a line you put - code skipped that block as programmed).


Learning to code is generally quick to write a few line. 

void setup(void) {

  Serial.begin(9600);

}

void loop(void) {

  Serial.println('Hello world');

}


Learning to code well takes years.  Unfortunately Arduino started a mess.

Cut copy and paste only works well if what you copy is exactly what you needed for a specific MCU, and your specific connected hardware, to do exactly the specific tasks needed to be done.  They sell their goods so people can explore.  But MCUs are less forgiving of errors.  One must code for a purpose.


The MCU will only do as it has been told to do.  But it trusts that the user hasn't told it to do something that will be harmful to it - and there are many things it can be programmed with that will kill the electronics.

Fastest Nextion Tutorial ever


Take ANY component in Editor

image


Here we see I have component page0.t1 on pageno 0 component .id of 13.

any attribute is "get-able" (cept .vscope is error and .objname can not do)


get page0.t1.id

get page0.t1.type

get page0.t1.bco

get page0.t1.font

get page0.t1.txt


only those attributes in green are "set-able"

page0.t1.bco=0

page0.t1.pco=31

page0.t1.font=1

page0.t1.txt="Hello"


All commands over serial need three 0xFF bytes to terminate.

Only setting .txt attribute needs quotes around


Then Nextion Instruction Set

- Most commands have parameters

- there is only one space between commands and parameters

  no parameters have spaces (unless it is text)


So the rest, really involves what the user as an artist like to make.


Nextion Editor Debug


image

Instruction Input Area:

- type it in to see if it works

- see what Nextion device would send back

When it works in debug, it will work for Nextion <-> MCU


I will argue that again it is not the Nextion that is difficult.

It is how to get your MCU in what language to do your will.

- What MCU used

- What Language Chosen

- Which Compiler Chosen

- Detailed project : step by step for MCU

To backup my point on keywords


Arduino Reference has few keywords

 - mostly operators and then links to other library functions.

https://www.arduino.cc/en/Reference/HomePage

Just for shits and giggles, I checked google.

800+ pages on Arduino.cc with "Nextion"


Nextion site:arduino.cc

Have you tried the examples in

 * Nextion user manual (updating) ?


From Editor

  Help Menu -> Help MenuItem -> Launches Nextion HMI Solutions page in browser

  (Many good links from here)


Go down to useful links and Follow

   Nextion User Manual.



But I think you misunderstand the nature of embedded, and me in the process.

An MCU by its very nature is an integer device.

  unsigned 8 bits 0 to 256   fits in 1 byte

  signed 8 bits  -128 to 127   fits in 1 byte

  unsigned 16 bits  0 to 65536   fits in 2 bytes

  signed 16 bits -32768 to 32767   fits in 2 bytes

  unsigned 32 bits 0 to 4294967296   fits in 4 bytes

  signed 32 bits  -2147483648 to 2147483647   fits in 4 bytes


A char is a 7-bit or 8-bit

  as 7 bits, you have lower ASCII

  as 8 bits, you have either ASCII, iso-8559, or other.


Strings are a different beast and a challenge for most

as they are first and foremost variable in how many bytes used

and as such don't have a nice and neatly "defined" structure.


So, how are they implemented?  Depends on the library, language and compiler.

Most Basic language compilers handle strings well

Most Pascal language compilers have length at byte 0

  - but like I said depends on compiler, some don't - some are null terminated.

Most C usually uses a null termination

  - but most must be in a byte sized array first


How to pass variables through as function parameters ... again

   depends on the library, language and compiler.

   Some user pointers, some use references.


Why? because before you program gets to be machine code that the

  specific MCU can read and use, it usually looks like some form of assembler


 

ADD        R2, SP, #4
MOVS        R3, #46
STRB        R3, [R2, #0]
MOVS        R3, #108
STRB        R3, [R2, #1]
MOVS        R3, #101
STRB        R3, [R2, #2]
MOVS        R3, #105
STRB        R3, [R2, #3]
MOVS        R3, #0
STRB        R3, [R2, #4]
ADD        R2, SP, #4
MOV        R0, R2
BL        _UART1_Write_Text+0
IT        AL
BAL        L__printattr21

 

Some compilers do a great job, others very much less so.

But there are a few things beyond the scope of a device manufacture.

 - Basics of Electronics

 - Basics of Programming


Now, what happens when someone doesn't know something ...

Usually we as the users need to go look it up and use tools at hand.


When I have a mikroPascal question on an STM32F103 (for the Nextion)

- I don't post here and say why doesn't anyone else use mikroPascal?

- Why doesn't everyone else use the STM32F103 chips instead of Arduino?


No, if it is a hardware question, I turn to 1284 pages of Reference Manual.

 or if really stumped I will have to ask at the ST forum for the STM32

If it is a "how to say it with mikroPascal", I must go to mikroe's forum.

In those forums, the likelihood that someone knows what I might ask doubles.


Maybe Arduino C++ is not a good fit for your liking

 - Bascom compilers offer Basic Language and can program UNO and Megas.

Trust me, pascal languages are the one that suits me the best.

  But there are big differences between compilers (Kylix, Delphi, Turbo, mikro)

  and this means looking up either he references, or posting in their forums.


But before languages, we have to understand communications, serial or otherwise.

And we have to understand the specifics of the MCU that we are working with.

 - HUGE Manuals for the UNO and the Mega at the Arduino.cc page.


You need to be able to tell your MCU to send over serial the following bytes.

    page1.t0.txt="1. Ziffer"ÿÿÿ  -- it is that simple really.

So unfortunately, it isn't really the Nextion that is difficult here

  the instruction set was made to be human readable and understandable.

It is how do you get your language and compiler to make your MCU do it.


Now, I am not sure how to make a thread long tutorial about how to send

   page1.t0.txt="Hello World"ÿÿÿ


The larger problem is: we are talking about

  Multiple code segments (countless) in

  Mulitple languages (seriously hundreds) for

  Multiple compilers of each language for

  more than 10,000 different MCU types used with Nextion every day.


Coding is an art as well as some science, but very artistic in nature

rarely ever will two programmers have the same code to solve the same.

The basics is top-down design (100 would jump on this to say otherwise)

 - Break your problem down into manageable parts.

   - if you can do this part - do it

     but if not, break it down into manageable parts.


There are places around the Net that teach fundamental coding

- but after learning your languages 30 to 50 keywords

... it is all up to the user how they would like to string them together.

The difficulty is the MCU will only do exactly as it has been told to do

 - so it will always be wrong ... until it is right.


Hi Patrick,

thank you for your reply. I surely understand that it is most likely not possible to now all the codes and commands of all languages and/ or compilers. I only tried to find the answer in here...
...sorry for asking in the wrong forum!
It would be great, if there would be some step by step tutorial stuff from the very first beginning, just for stupid beginners like me.
I just want to use the Nextion as a display and it took me quite a while to figure out how to set it up to get this far I am right now. I didn´t find anything in the web about simple uses of the Nextion like this. Everything starts in a higher level. In the arduino forums there is very little about the Nextion, and here is very little about the arduino stuff. It would be awesome if there would be a solution...

My best regards and sorry again...

Freddie


 

t0.setText("1. Ziffer");


You are trying to pass a constant and the underlying compiler is interpreting it as a String.

Look at NexText.cpp and NexText.h code and it wants char *buffer - so no conversion

- the library is not overloaded with routines to accept all or many types.


But compiler issues are Arduino forum  string constant to char *buffer

- I simply can not know all changes in all languages/compilers



Hi folks,

nobody with any idea?

Freddie

 

Hi there,

just for understanding, if I have a code like the one in the following example:
C++
// Aufbereiten der eingegebenen Ziffern zu einer 1-4 - stelligen Zahl
void checknumber(int x){
  if (ersteziffer == 99) { // Prüfen ob erste Ziffer eingegeben wurde
    ersteziffer=x;
    String displayvalue = String(ersteziffer);  //  Wandelt die Eingabe in einen String um

//    drawnokiascreen(displayvalue); // Wiederaufbau Nokia 5110 LCD Display

    t0.setText("1. Ziffer"); // Darstellen der Eingabe der ersten Ziffer
    
  } else {
  if (zweiteziffer == 99) { // Prüfen ob zweite Ziffer eingegeben wurde
    zweiteziffer=x;    String displayvalue = String (ersteziffer) + String(zweiteziffer);  //  Wandelt die Eingabe in einen String um
    
//    drawnokiascreen(displayvalue); // Wiederaufbau Nokia 5110 LCD Display 

    t0.setText("2. Ziffer"); // Darstellen der Eingabe der zweiten Ziffer    

  } else

 and I want to show the string 'displayvalue' at the position 't0' do I use the code "t0.setText(displayvalue) or is there another code for it? The electronics for the the stepper positioning works fine with the code and the displaying of the steps on the Nextion is in the order it should as far as I can tell by using the "1. Ziffer" etc. as content to show as a wildcard.

If I use the code as shown above, just with the content of the brackets ("1. Ziffer" & "2. Ziffer" for the string changed to 'displayvalue' I end up in receiving the following error code:


HTML
E:\Arbeitsordner\Freddie\Arduino\Sensorsteuerung\Sensor_1_Nextion_roh_1\Sensor_1_Nextion_roh_1.ino: In function 'void checknumber(int)':

Sensor_1_Nextion_roh_1:182: error: no matching function for call to 'NexText::setText(String&)'

     t0.setText(displayvalue); // Darstellen der Eingabe der ersten Ziffer

                            ^

E:\Arbeitsordner\Freddie\Arduino\Sensorsteuerung\Sensor_1_Nextion_roh_1\Sensor_1_Nextion_roh_1.ino:182:28: note: candidate is:

In file included from C:\Program Files (x86)\Arduino\libraries\Nextion/Nextion.h:33:0,

                 from E:\Arbeitsordner\Freddie\Arduino\Sensorsteuerung\Sensor_1_Nextion_roh_1\Sensor_1_Nextion_roh_1.ino:10:

C:\Program Files (x86)\Arduino\libraries\Nextion/NexText.h:53:10: note: bool NexText::setText(const char*)

     bool setText(const char *buffer);    

          ^

C:\Program Files (x86)\Arduino\libraries\Nextion/NexText.h:53:10: note:   no known conversion for argument 1 from 'String' to 'const char*'

Sensor_1_Nextion_roh_1:190: error: no matching function for call to 'NexText::setText(String&)'

     t0.setText(displayvalue); // Darstellen der Eingabe der zweiten Ziffer    

                            ^

E:\Arbeitsordner\Freddie\Arduino\Sensorsteuerung\Sensor_1_Nextion_roh_1\Sensor_1_Nextion_roh_1.ino:190:28: note: candidate is:

In file included from C:\Program Files (x86)\Arduino\libraries\Nextion/Nextion.h:33:0,

                 from E:\Arbeitsordner\Freddie\Arduino\Sensorsteuerung\Sensor_1_Nextion_roh_1\Sensor_1_Nextion_roh_1.ino:10:

C:\Program Files (x86)\Arduino\libraries\Nextion/NexText.h:53:10: note: bool NexText::setText(const char*)

     bool setText(const char *buffer);    

          ^

C:\Program Files (x86)\Arduino\libraries\Nextion/NexText.h:53:10: note:   no known conversion for argument 1 from 'String' to 'const char*'

 


I´m searching now quiet a while to find my way, but I may be to blind for finding it...


Someone out there with an answer?


Freddie

 

 

OK, got the Nextion as far that it will show text sas formatted in the brackets, if I change it to the variables name I receive errors...
the code looks right now:

 

// Laden der Baugruppenbibliotheken
#include <Nextion.h>
#include <NexText.h>
#include <Keypad.h> //Keypad Library
#include "U8glib.h" // Nokia 5110 LCD Display Library
#include <AccelStepper.h> // AccelStepper Library


// Definieren der Seiten für Nextion 3.5"

NexPage page0    = NexPage(0, 0, "page0");  // Startseite mit Hinweis auf Verwendung
NexPage page1    = NexPage(1, 0, "page1");  // Hinweis auf die Verzögerung des Starts wegen Referenzfahrt und Nullpunktabgleich
NexPage page2    = NexPage(2, 0, "page2");  // Eingabeseite der Bärposition

// Hinweisseite für Test, in der endgültigen Fassung löschen!
//NexPage page3    = NexPage(3, 0, "page3");


NexText t0 = NexText(2, 1, "t0");
NexText t1 = NexText(2, 2, "t1");


NexTouch *nex_listen_list[] = 
{
    NULL
};


// Festlegen der Variablen
// Tastatur
volatile int ersteziffer=99;
volatile int zweiteziffer=99;
volatile int dritteziffer=99;
volatile int vierteziffer=99;

// Berechnung
long eingabe=0; //speichert die berechnete Eingabe
String baer = ""; //Position Baer

// Tastatur
const byte ROWS = 4; //4 Reihen
const byte COLS = 4; // 4 Spalten
char keys[ROWS][COLS] = {
  {'1','2','3','F'},
  {'4','5','6','E'},
  {'7','8','9','D'},
  {'A','0','B','C'}
};
byte rowPins[ROWS] = {22, 24, 26, 28};
byte colPins[COLS] = {31, 33, 35, 37};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );



// U8glib Setup für  Nokia 5110 LCD Display
#define backlight_pin 11
U8GLIB_PCD8544 u8g(3, 4, 6, 5, 7);  // Arduino Pins verbunden mit Nokia Pins, CLK=3, DIN=4, CE=6, DC=5, RST=7
                                    
                                    
// AccelStepper Treiber Setup
AccelStepper stepper(1, A0, A1); // 1 = Easy Driver Interface, Arduino A0 verbunden mit STEP Pin von Easy Driver, Arduino A1 verbunden mit DIR Pin von Easy Driver


// Programmvorbereitung
void setup() {

Serial.begin(9600);
   
    nexInit();

// Die Zeiten bei den zwei folgenden Delays sind für die Anzeigeverzögerung auf einen passenden Wert zu ändern!   
 
    page0.show();  // Startseite nach dem Einschalten
    delay(2500);
    
    page1.show();  // Hinweisseite für Referenzfahrt und Nullpunktabgleich
    delay(2500);

    page2.show();


//    t0.setText("enter distance");
//    t1.setText("currentposition");

  
//analogWrite(backlight_pin, 0);  // Dimmen der Hintergrundbeleuchtung  (0=Hell, 255=Dunkel)

  stepper.setMaxSpeed(4800);  // Setzt die Geschwindigkeit des Schrittmotors
  stepper.setAcceleration(2400);  //  Setzt die Beschleunigung des Schrittmotors  
}

// Programmabschnitt
void loop() {
  char keypressed = keypad.getKey();  // Welche Taste wurde gedrückt
  if (keypressed != NO_KEY){  // Übernahme des der gedrückten Taste hinterlegten Wertes
    switch (keypressed) {
      
      case '1':
        checknumber(1);
      break;
        
      case '2':
        checknumber(2);
      break;
 
      case '3':
        checknumber(3);
      break;
 
      case 'F':
        checknumber(780);
      break;
      
      case '4':
        checknumber(4);
      break;
      
      case '5':
        checknumber(5);
      break;
 
      case '6':
        checknumber(6);
      break;
 
      case 'E':
        checknumber(850);
      break;

      case '7':
        checknumber(7);
      break;
       
      case '8':
        checknumber(8);
      break;
 
      case '9':
        checknumber(9);
      break;
 
      case 'D':
        checknumber(1000);
      break;
 
      case 'A':
        deletenumber();
      break;

      case '0':
        checknumber(0);
      break;      

      case 'B':
        calculatedistance();
      break;      
 
      case 'C':
        checknumber(1250);
      break;
      }
    }
  }


// Aufbereiten der eingegebenen Ziffern zu einer 1-4 - stelligen Zahl
void checknumber(int x){
  if (ersteziffer == 99) { // Prüfen ob erste Ziffer eingegeben wurde
    ersteziffer=x;
    String displayvalue = String(ersteziffer);  //  Wandelt die Eingabe in einen String um

//    drawnokiascreen(displayvalue); // Wiederaufbau Nokia 5110 LCD Display

    t0.setText("1. Ziffer"); // Darstellen der Eingabe der ersten Ziffer
    
  } else {
  if (zweiteziffer == 99) { // Prüfen ob zweite Ziffer eingegeben wurde
    zweiteziffer=x;    String displayvalue = String (ersteziffer) + String(zweiteziffer);  //  Wandelt die Eingabe in einen String um
    
//    drawnokiascreen(displayvalue); // Wiederaufbau Nokia 5110 LCD Display 

    t0.setText("2. Ziffer"); // Darstellen der Eingabe der zweiten Ziffer    

  } else {
  if (dritteziffer == 99) { // Prüfen ob dritte Ziffer eingegeben wurde
    dritteziffer=x;
    String displayvalue = String (ersteziffer) + String(zweiteziffer) + String(dritteziffer);  //  Wandelt die Eingabe in einen String um
    
//    drawnokiascreen(displayvalue); // Wiederaufbau Nokia 5110 LCD Display 

    t0.setText("3. Ziffer"); // Darstellen der Eingabe der dritten Ziffer    

    
  } else {  // Prüfen ob vierte Ziffer eingegeben wurde
      vierteziffer=x;
      String displayvalue = (String(ersteziffer) + String(zweiteziffer) + String(dritteziffer) + String(vierteziffer));  //  Wandelt die Eingabe in einen String um
      
//      drawnokiascreen(displayvalue);

    t0.setText("4. Ziffer"); // Darstellen der Eingabe der vierten Ziffer      

      }
    }
  }
}

void deletenumber() {  // löscht die letzte eingegebene Ziffer
  if (vierteziffer !=99) {
      String displayvalue = String(ersteziffer) + String(zweiteziffer) + String(dritteziffer);
      
//      drawnokiascreen(displayvalue);

      t0.setText("del 4"); // Löschen der Eingabe der vierten Ziffer       
 
      vierteziffer=99;
  } 
  else {
    if (dritteziffer !=99) {
      String displayvalue = String(ersteziffer) + String(zweiteziffer);
      
//      drawnokiascreen(displayvalue);

      t0.setText("del 3"); // Löschen der Eingabe der dritten Ziffer      
 
      dritteziffer=99;
   } 
  else {
    if (zweiteziffer !=99) {
      String displayvalue = String(ersteziffer);
      
//      drawnokiascreen(displayvalue);

      t0.setText("del 2"); // Löschen der Eingabe der zweiten Ziffer      
 
      zweiteziffer=99;
   }  
  else {
     if (ersteziffer !=99) {
       String displayvalue = "";

//       drawnokiascreen(displayvalue);

       t0.setText("t0"); // Löschen der Eingabe der ersten Ziffer       
 
       ersteziffer=99;
        }
      }
    }
  }
}

void calculatedistance() {  // Berechnet aus den eingegebenen Ziffern eine bis zu 4-stellige Zahl
 
    if (vierteziffer == 99 && dritteziffer == 99 && zweiteziffer == 99 && ersteziffer != 99) {
      eingabe=ersteziffer;
      movestepper(eingabe);
    }

     if (zweiteziffer != 99 && dritteziffer == 99) {
      eingabe=(ersteziffer*10)+zweiteziffer;
      movestepper(eingabe);
    }

     if (dritteziffer != 99 && vierteziffer == 99) {
      eingabe=(ersteziffer*100)+(zweiteziffer*10)+dritteziffer;
      movestepper(eingabe);
    }

     if (vierteziffer != 99) {
      eingabe=(ersteziffer*1000)+(zweiteziffer*100)+(dritteziffer*10)+vierteziffer;
      movestepper(eingabe);
    }
    
//    resetnumbers(); // Löscht die letzte Eingabe

       t0.setText("t0");    
  }

void movestepper(long z) {  //  Startet Schrittmotor
 
  long calculatedmove=((z*1600)/40);  //  Berechnet die Schrittzahl anhand der neuen Eingabe
  stepper.runToNewPosition(calculatedmove);
  baer = String(z);
      
      t1.setText("POSITION"); // Zeigt die aktuelle Position des Bärs auf dem Nextion an  
  }


void resetnumbers() {  // Löschen der letzten Zahl
  ersteziffer=99;
  zweiteziffer=99;
  dritteziffer=99;
  vierteziffer=99;
  
}

// Anzeige der aktuellen Position des Baers
 void drawnokiascreen(String y) {
 
      t0.setText("t0");
      t1.setText("POSITION");

   
}

I´m lost again...

It´s working but I dont get the display to show the calculated numbers...


Freddie

 

Well, yesterday during trying I set a bug in my script which bothered me quiet a while. I took care of it this morning and checked the script working togehter with the electronic. Now I have it the way that it moves the stepper to the desired position, the only thing missing is showing the input at the t0 text field and the calculated distance in t1.
...I´m working on it...

Freddie

 

Just one thing before I turn the machine off:

When I type in a number eg. "150" every single keytroke shows up at the position "enter distance" what will be for the Nextion (page2, id1, t0). After the key "B" being pressed, the script will calculate the steps, send it to the stepper and as the stepper moved to the new position, the value from "enter distance" will be sent to the "currentposition" what will be for the Nextion (page2, id2, t1) and the field "enter distance" will be cleared.
A stroke on the key a will erase the last number entered, a stroke again will erase the next number ...


That´s it for tonight


Freddie

 

Well,
dinner is over, I started another try...
...and tried to put both codes together, step by step, using the "try and error method".
I
  • inserted the definition of the libraries, compiled, no errors => good
  • defined the 'NexText' as
    NexText t0 = NexText(2, 1, "t0"); // page2, id1, t0
    NexText t1 = NexText(2, 2, "t1"); // page2, id2, t1; compiled, no errors => good
  • left all other code in the script for not loosing the path; compiled, no errors => good
  • inserted all the way at the end of the script the 'setText' as
    t0.setText("Eingabe");
    t1.setText("Distanz"); compiled, no errors => good
  • already thought that the part witth the setText is at the wrong place, but I wnted to give it a try..

After this, I uploaded the code and tried...

...the screens appeared as they should, but on the display it showed right after the first keystroke


in the text-field t0: "Eingabe"; just the word...

in the text-field t1: "Distanz"; just the word...

and no other keystroke was displayed: bad...

...sad... because it started pretty good.


Well for sitting at this problem for at least 6 hours now I quit for today and try again tomorrow...

...I might miss the wood for the trees...


By the way Patrick is english your native tongue? I changed the german parts of the script into english, it´s probably a better understanding of what´s going on in my script...


 

// Definieren der Libraries

#include "Nextion.h"
#include <Keypad.h> //Keypad Library
#include <AccelStepper.h> // AccelStepper Library
#include <U8glib.h> // Nokia 5110 LCD Display Library//


// Definieren der Seiten für Nextion 3.5"

NexPage page0    = NexPage(0, 0, "page0");  // Startseite mit Hinweis auf Verwendung
NexPage page1    = NexPage(1, 0, "page1");  // Hinweis auf die Verzögerung des Starts wegen Referenzfahrt und Nullpunktabgleich
NexPage page2    = NexPage(2, 0, "page2");  // Eingabeseite der Bärposition

// Hinweisseite für Test, in der endgültigen Fassung löschen!
NexPage page3    = NexPage(3, 0, "page3");


NexText t0 = NexText(2, 1, "t0");
NexText t1 = NexText(2, 2, "t1");


NexTouch *nex_listen_list[] = 
{
    NULL
};


// Definieren der Variablen für die Berechnung

int keyfullnumber=0;  //speichert die berechnete Eingabe
String currentposition = "";  //Position Baer


// Definieren der Eingabeverarbeitung

volatile int firstnumber=99;
volatile int secondnumber=99;
volatile int thirdnumber=99;
volatile int fourthnumber=99;


// Setup der Keypadbelegung und definieren der Pin-Beschaltung

const byte ROWS = 4;  //4 Reihen
const byte COLS = 4;  // 4 Spalten
char keys[ROWS][COLS] = {
  {'1','2','3','F'},
  {'4','5','6','E'},
  {'7','8','9','D'},
  {'A','0','B','C'}
};
byte rowPins[ROWS] = {22, 24, 26, 28};
byte colPins[COLS] = {31, 33, 35, 37};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );


// U8glib Setup für  Nokia 5110 LCD Display

#define backlight_pin 11
U8GLIB_PCD8544 u8g(3, 4, 6, 5, 7);  // Arduino Pins verbunden mit Nokia Pins, CLK=3, DIN=4, CE=6, DC=5, RST=7
                                   
                                    
// Setup für den AccelStepper Treiber

AccelStepper stepper(1, A0, A1);  // 1 = Easy Driver Interface, Arduino A0 verbunden mit STEP Pin von Easy Driver, Arduino A1 verbunden mit DIR Pin von Easy Driver


void setup(void)
{   
    Serial.begin(9600);
    nexInit();


// Die Zeiten bei den zwei folgenden Delays sind für die Anzeigeverzögerung auf einen passenden Wert zu ändern!   
 
    page0.show();  // Startseite nach dem Einschalten
    delay(2500);
    
    page1.show();  // Hinweisseite für Referenzfahrt und Nullpunktabgleich
    delay(2500);

    page2.show();
}

void loop(void)
{
  char keypressed = keypad.getKey();  // Get value of keypad button if pressed
  if (keypressed != NO_KEY){  // If keypad button pressed check which key it was
    switch (keypressed) {
      
      case '1':
        checknumber(1);
      break;
        
      case '2':
        checknumber(2);
      break;
 
      case '3':
        checknumber(3);
      break;
 
      case 'F':
        checknumber(780);
      break;
      
      case '4':
        checknumber(4);
      break;
      
      case '5':
        checknumber(5);
      break;
 
      case '6':
        checknumber(6);
      break;
 
      case 'E':
        checknumber(850);
      break;

      case '7':
        checknumber(7);
      break;
       
      case '8':
        checknumber(8);
      break;
 
      case '9':
        checknumber(9);
      break;
 
      case 'D':
        checknumber(1000);
      break;
 
      case 'A':
        deletenumber();
      break;

      case '0':
        checknumber(0);
      break;      

      case 'B':
        calculatedistance();
      break;      
 
      case 'C':
        checknumber(1250);
      break;
    }
  }
}

void checknumber(int x){
  if (firstnumber == 99) { // Prüfen ob erste Ziffer eingegeben wurde
    firstnumber=x;
    String displayvalue = String(firstnumber);  //  Wandelt die Eingabe in einen String um
    drawnokiascreen(displayvalue); // Wiederaufbau Nokia 5110 LCD Display
    
  } else {
  if (secondnumber == 99) { // Prüfen ob zweite Ziffer eingegeben wurde
    secondnumber=x;    String displayvalue = String (firstnumber) + String(secondnumber);  //  Wandelt die Eingabe in einen String um
    drawnokiascreen(displayvalue); // Wiederaufbau Nokia 5110 LCD Display 

  } else {
  if (thirdnumber == 99) { // Prüfen ob dritte Ziffer eingegeben wurde
    thirdnumber=x;
    String displayvalue = String (firstnumber) + String(secondnumber) + String(thirdnumber);  //  Wandelt die Eingabe in einen String um
    drawnokiascreen(displayvalue); // Wiederaufbau Nokia 5110 LCD Display 

    
  } else {  // Prüfen ob vierte Ziffer eingegeben wurde
      fourthnumber=x;
      String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber) + String(fourthnumber));
      drawnokiascreen(displayvalue);

      }
    }
  }
}

void deletenumber() {  // löscht die letzte eingegebene Ziffer
  if (fourthnumber !=99) {
      String displayvalue = String(firstnumber) + String(secondnumber) + String(thirdnumber);
      drawnokiascreen(displayvalue);
 
      fourthnumber=99;
  } 
  else {
    if (thirdnumber !=99) {
      String displayvalue = String(firstnumber) + String(secondnumber);
      drawnokiascreen(displayvalue);
 
      thirdnumber=99;
   } 
  else {
    if (secondnumber !=99) {
      String displayvalue = String(firstnumber);
      drawnokiascreen(displayvalue);
 
      secondnumber=99;
   }  
  else {
     if (firstnumber !=99) {
       String displayvalue = "";
       drawnokiascreen(displayvalue);
 
       firstnumber=99;
        }
      }
    }
  }
}

void calculatedistance() {  // Berechnet aus den eingegebenen Ziffern eine bis zu 4-stellige Zahl
 
    if (fourthnumber == 99 && thirdnumber == 99 && secondnumber == 99 && fourthnumber != 99) {
      keyfullnumber=firstnumber;
      movestepper(keyfullnumber);
    }

     if (secondnumber != 99 && thirdnumber == 99) {
      keyfullnumber=(firstnumber*10)+secondnumber;
      movestepper(keyfullnumber);
    }

     if (thirdnumber != 99 && fourthnumber == 99) {
      keyfullnumber=(firstnumber*100)+(secondnumber*10)+thirdnumber;
      movestepper(keyfullnumber);
    }

     if (fourthnumber != 99) {
      keyfullnumber=(firstnumber*1000)+(secondnumber*100)+(thirdnumber*10)+fourthnumber;
      movestepper(keyfullnumber);
    }
    
    resetnumbers(); // Löscht die letzte Eingabe
  }

void movestepper(int z) {  //  Startet Schrittmotor
 
  int calculatedmove=((z*1600)/80);  //  Berechnet die Schrittzahl für die neue Eingabe
  stepper.runToNewPosition(calculatedmove);
  currentposition = String(z);

  u8g.firstPage();
    do {
      u8g.drawHLine(0, 15, 84);
      u8g.drawVLine(50, 16, 38);
      u8g.drawHLine(0, 32, 84); 
      u8g.setFont(u8g_font_profont10);
      u8g.drawStr(2, 10, "enter distance");
      u8g.drawStr(62, 27, "mm");
      u8g.drawStr(0, 44, "cur-pos");
      u8g.setPrintPos(54,46);
      u8g.print(currentposition); // Zeigt die aktuelle Position des Bärs an      
  }
  while( u8g.nextPage() ); 


  t1.setText("baer");
}

void resetnumbers() {  // Löschen der letzten Zahl
  firstnumber=99;
  secondnumber=99;
  thirdnumber=99;
  fourthnumber=99;
}


 void drawnokiascreen(String y) {
 
    u8g.firstPage();
    do {
      u8g.drawHLine(0, 15, 84);
      u8g.drawVLine(50, 16, 38);
      u8g.drawHLine(0, 32, 84); 
      u8g.setFont(u8g_font_profont10);
      u8g.drawStr(2, 10, "enter distance");
      u8g.setPrintPos(0,27);
      
      u8g.print(y); //Zeigt eingegebene Distanz an  
         
      u8g.drawStr(62, 27, "mm");
      u8g.drawStr(0, 44, "cur-pos");
      u8g.setPrintPos(54,46);
      
      u8g.print(currentposition); // Zeigt die aktuelle Position des Bärs an
      
    }
      while( u8g.nextPage() );


    t0.setText("enter distance");
    t1.setText("currentposition");    

 }

 I didn´t change the comments though, as they are just comments.


Good night from Germany...


Freddie

It sounds like it should work well

Hmm, jjust before I will forget...
the code:

NexText y = NexText(1, 2, "t0");

NexText baer = NexText(1, 4, "t1");

will be put right under the NexPage stuff


and the code:

  y.setText("eingabe");

  baer.setText("baer");

will be put there, where the step for redrawing the Screen and changing the variables was?


Freddie

 

Login or Signup to post a comment