Start a new topic
Solved

Nextion and HC-SR04 on Arduino Leonardo

Hi,


I have a dual state button to start the sensor HC-SR04 on the display. If I push the button the sensor start the work and show the result on display, but I can not stop the sensor with the dual button. In the debug monitor I see the following...


[1415:0,3,bt0]

buttonStartPopCallback

recvRetCommandFinished ok

recvRetCommandFinished ok

recvRetCommandFinished ok

recvRetCommandFinished ok

recvRetCommandFinished ok

recvRetCommandFinished ok

recvRetCommandFinished ok




There is more details needed to understand the problem


Is this on an enhanced model nextion : where you have bound an io pin to bt0?

Is this on a basic model nextion : where code in bt1 press/release is to start/stop the sensor

Is starting and stopping the sensor performed within Nextion code, or Arduino code


Have you stopped your timer anywhere in your code

 -- so that it doesn't trigger your routine that fetches your data?

Hi Patrick,


I use the following hardware

Arduino Leonard 

Nextion NX4832T035 (basic model)


I try to start/stopp the sensor function with the dual state button bt0. The start is possible, but the stop is not possible. 

#include <Arduino.h>
#include <Nextion.h>

#define pingOut 10
#define pingIn 11

NexDSButton btn_start = NexDSButton(0, 3, "bt0");
NexText t1 = NexText(0,9, "t1");

int state = LOW;

uint32_t number = 0;

NexTouch *nex_listen_list[] =
{
    &btn_start,
    NULL
};

void buttonStartPopCallback(void *ptr)
{
    if (state == HIGH)
    {
        state = LOW;
    }
    else
    {
        state = HIGH;
    }
}

void Sonar(void)
{
    long duration, inches, cm;
    char buffer[33];
    char *strptr;
    digitalWrite(pingOut,LOW);
    delayMicroseconds(2);
    digitalWrite(pingOut,HIGH);
    delayMicroseconds(10);
    digitalWrite(pingOut,LOW);
    duration = pulseIn(pingIn,HIGH,30000);
    cm = duration / 29 / 2;
    strptr = ltoa(cm,buffer,10);
    t1.setText(strptr);
}

void setup(void)
{
    nexInit();
    btn_start.attachPush(buttonStartPopCallback); 
    pinMode(13, OUTPUT);
    pinMode(pingOut, OUTPUT);
    pinMode(pingIn, INPUT);
}

void loop(void)
{
    nexLoop(nex_listen_list);
    digitalWrite(13, state);
    if(state ==HIGH)
        Sonar();
}


I would recommend that your code perhaps use the dual state button getValue function to determine if your dual state button is on or off, and make your loop condition based on the actual button's value


Perhaps the DualStateButton example can assist:

https://github.com/itead/ITEADLIB_Arduino_Nextion/blob/master/examples/CompDualStateButton/CompDualStateButton.ino


Hi Patrick,


I have try to use the dual state, but the result is the same.

If I remove  t1.setText(strptr) from scratch then I can start/stop the sensor function without any problem.

I see in the serial monitor a output loop, if I use t1.setValue


recvRetCommandFinished ok

recvRetCommandFinished ok

recvRetCommandFinished ok

...



Perhaps you might try sending the already converted buffer and not trying pointers.

The function ltoa(val, str, 10) converts the first param into the second param, using the base stated in the third param.

 

void Sonar(void)
{
    long duration, inches, cm;
    char buffer[33];
    digitalWrite(pingOut,LOW);
    delayMicroseconds(2);
    digitalWrite(pingOut,HIGH);
    delayMicroseconds(10);
    digitalWrite(pingOut,LOW);
    duration = pulseIn(pingIn,HIGH,30000);
    cm = duration / 29 / 2;
    ltoa(cm,buffer,10);
    t1.setText(buffer);
}

 

The code above may help, but has not been tested, as I am not the Adruino guru

I also strongly recommending that if you need further coding assistance,
that you start your discussion in the Free Chat Section where Arduino users may provide better assistance.

As this is not a Bug Report, I will need to mark this as solved.