Start a new topic

keypad with nextion and arduino

 Hi trying to use nextion as a keypad 


configured keypad using nextion editor 

touch 1 -serial output -65 0 11 ffff ffff ffff 

touch 2 -serial output -65 0 21 ffff ffff ffff 

touch 3 -serial output -65 0 31 ffff ffff ffff 

touch 4 -serial output -65 0 41 ffff ffff ffff 

touch 5 -serial output -65 0 51 ffff ffff ffff 

touch 6 -serial output -65 0 61 ffff ffff ffff 

touch 7 -serial output -65 0 71 ffff ffff ffff 

touch 8-serial output -65 0 81 ffff ffff ffff 

touch 9 -serial output -65 0 91 ffff ffff ffff 



  

#include <SoftwareSerial.h>
#include <Nextion.h>

SoftwareSerial nextion(2, 3);// Nextion TX to pin 2 and RX to pin 3 of Arduino

Nextion myNextion(nextion, 9600); //create a Nextion object named myNextion using the nextion serial port @ 9600bps

int led = 13;
void setup() {
  Serial.begin(9600);
  myNextion.init(); // send the initialization commands for Page 0
pinMode(led, OUTPUT);
}

void loop() {
  String message = myNextion.listen(); //check for message
  if (message == "65 0 1 1 ffff ffff ffff") {
    digitalWrite(led, HIGH);
  }
  if (message == "65 0 2 1 ffff ffff ffff") {
    digitalWrite(led, LOW); 
  }

}

  when i touch no1 on nextion led lights up

touch no2 led is off


would like to know which is the best way to use this as a keypad ,example to turn on led when a code is touched example if 2456 is touched the led should come on 

Appreciate you help 


2 people like this idea

Use a state machine If (keypressed) { Switch(state) { Case Waitfor2: If (2pressed) State= WaitFor4; Else State=WaitFor2; Break; Case Waitfor4: If (4pressed) State=WaitFor5; Else State=WaitFor2; And so on...
So much for the formatting... Oh well I think you get the idea.

Another technique is to manage the key presses in the Nextion and then send the entire text to the microcontroller using an Enter/Send key.


For example, in the user code of the key "1" add the following:

t0.txt=t0.txt+"1"

In key "2": t0.txt=t0.txt+"2"  ... etc for all the keys

A Delete/Backspace key is: t0.txt=t0.txt-1

In the Enter/Send key put the following code: get t0.txt

The sent message will be 0x70 + your data + END

Example project using keypad:
https://github.com/bborncr/SousVideController

hi, i dont really understand "switch state in arduino"(not much tutorials) ,will try to figure out how to manage key presses in nextion,maybe it will be simpler option,thank you for your help 

Hi,@Kevin Govender.

There is a quick start of Arduino_Nextion_library tutorial in Announcement.

Maybe it can help you.

hi ,

For example, in the user code of the key "1" add the following:

t0.txt=t0.txt+"1"

In key "2": t0.txt=t0.txt+"2"  ... etc for all the keys

A Delete/Backspace key is: t0.txt=t0.txt-1

In the Enter/Send key put the following code: get t0.txt

The sent message will be 0x70 + your data + END

i configured all keys with the above code, the only problem is when i configure the enter key with get t0.txt i get an invalid variable error, any suggestions ,thanks 

It sounds like you don't have a t0 text component.


1 person likes this

hi, any suggestions ,thanks

Kevin, if you look at the weather example that Nextion has made, they have a touch keyboard implemented using t0.txt. You can see from the example that you need an actual string component named t0. While you press the buttons it adds each button press to the string component.

All done,thanks for your help

I also just posted a keyboard example with working shift and cap lock keys. It is in the gallery.

1 person likes this
http://support.iteadstudio.com/support/discussions/topics/1000065449

I am getting this error 'class Nextion' has no member named 'listen'. Can anyone help please?


1 person likes this
Login or Signup to post a comment