I am using the ITEADLIB Arduino Nextion and I have pretty much got everything working on my Nextion. I just have one problem, I want to have one button that works like a momentary switch. I need it to turn on a relay while I am pushing it and turn it off when I release it. Can anyone help me with this?
You need to show the code You have made so far.
Providing schematics would be good. Pen and paper usually works well.
The Nextion-Editor provides a button pushed down-event and a button-release-event
Start action on button pushed down-event
stop action / or do a different action on button-release-event
best regards Stefan
OK here is the problem. I can get the button to work but I have no idea how to put in the sketch that when it is pushed, it does one thing and when it is released it does another. Right now I just have the nextion sending the id code on press and release, but as I said, I don't know how to write the sketch in the
void b13PopCallback(void *ptr)
{
if (statusb13 == false){
digitalWrite(R13, HIGH);
dbSerial.println("Horn ON");
statusb13 = true;
}
else {
digitalWrite(R13, LOW);
statusb13 = false;
dbSerial.println("Horn Off");
}
}`
This work, but it acts like a bt button.
Instead of sending the command ID you can send your own character-sequence
which you then have to evaluate.
I have worked only with this method. I have never worked with the library
But I guess there is a possability to define a touch press event

and a touch release event

best regards Stefan
So what does this mean it works like a "bt button??"
what is a "bt button" ?
is there anything in the library that is mutually exclusive related to button pressed down?
is there anything in the library that is mutually exclusive related to button released?
bt meaning Dualstate button. How would I call the "prints "" to make the MCU to look for it?
each and every button has two states
contact closed / contact opened except maybe a "trump"-button. Those buttons change to multiple states at any time depending on what fits best (for the button) ![]()
Do you mean a toggle-switch?:
press down a first time contact closes and contact stays closed if you take your finger off the button
press down a second time contact opens and contact stays opened if you take your finger off the button
.
.repeat
The prints are a command that you type into the nextion-editor exactly as shown on the screenshot
you press compile
then you do the TFT-file-output
copy the TFT-file onto SD-card
insert SD-card into nextion-display
powerup nextion display to load the new TFT-file into the display
and then whenever you touch the button
the nextion-display will send the charactersequence to your microcontroller
Your microcontroller has to look for the received characters and react on them.
I assume
if you use the nextion-library
you have to use a nextion-library function to check for these character-sequences
In case you want a toggle-switch the existing sending ID-number will be sufficient
as a toggle-switch does invert its state with each press
myToggleState = !myToggleState // the attention-mark "!" is the not-operator
simply inverting the boolean state
!true = false
!false = true
If you want more detailed help you to describe in much more detail what you want to have and how your special button behaves
best regards Stefan
What I need is for it to act like a momentary switch, while the button is being pushed it is on and it is off when it is released. I know the prints will send that command to the MCU, but I don't know how to have the MCU capture the command so I can have it compare to what state the button is in.
I really don't know it how you can capture the "prints" with the nextion-library because me myself I do it always without the nextion-library.
The commands that the nextion-editor provides may have something where you could send repeatedly "prints"
But this is not nescessary. It will be sufficient to toggle a boolean variable each time the button-ID is sended.
you press the nextion-button ==> ID gets sended ==> arduino toggles a boolean variable
you relase the nextion-button ==> ID gets sended ==> arduino toggles the boolean variable again
.
.
you press the nextion-button ==> ID gets sended ==> arduino toggles a boolean variable
your code keeps the variable on this value which is telling your arduino-code
button is pressed down
button is pressed down
button is pressed down
button is pressed down
button is pressed down
if you release the nextion-button
you press the nextion-button ==> ID gets sended ==> arduino toggles a boolean variable to false
your code keeps the variable on this value which is telling your arduino-code
button is released
it is about time that you do a simple testcode to examine how this works
best regards Stefan
Can you give me an idea what to put into the void callback to have it switch the relay "We'll call it R10"?
static boolean buttonForR10Pressed = false;
buttonForR10Pressed = !buttonForR10Pressed;
if (buttonForR10Pressed) {
// continue with action
}
it seems that you would have it much easier to write code if you
Take a look into this tutorial:
It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.
best regards Stefan
after a digitalWrite(IOpin,HIGH);
the IO pin stays HIGH until you execute
a digitalWrite(IOpin,LOW);
So the callback for button pressed has a simple
digitalWrite(IOpin,HIGH);
and the callback for button released has a simple
digitalWrite(IOpin,LOW);
if there is only one callback for both
you code
digitalWrite(IOpin, !digitalRead(IOPin) );
where "IOpin" is the identifier that you use in your code.
Again: if this is not enough information for you I highly recommend
Take a look into this tutorial:
It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.