Hi guys,
I'm trying to make a bike dashboard with a lap timer and a pilot selection function through the touchscreen.
The screen is a Nextion HMI.
In my code, the rpm and pilot selection functions work perfectly, I can read the message from the touchscreen and send back the name of the pilot to the screen.
But when I activate the battery voltage part of my loop, the button reading from the screen doesn't work anymore.
Is it a buffer issue or something like that ? How can I get rid of this problem ?
Thanks
#include <SoftwareSerial.h>
#include <Nextion.h>
#define PIN 4 //chrono
/*************************VARIABLES rev counter*********************************/
volatile byte rpmcount;
volatile unsigned int rpm;
unsigned long timeold;
/******************************battery voltage***********************************/
const int analogInPin = A0;
const float Vcc = 5;
const float R1 = 22000;
const float R2 = 10000;
float tension;
float v_pin;
int sensorValue;
/*********pilot selection***********************************************/
String pilote;
String ben = "Ben";
String thom = "Thom";
/****************************nextion******************************************/
SoftwareSerial nextion(11, 12);// Nextion TX to pin 11 and RX to pin 12 of Arduino
Nextion myNextion(nextion, 9600);
void setup()
{
Serial.begin(9600);
myNextion.init();
attachInterrupt(1, rpm_pulse, FALLING);//rpm
}
void loop()
{
/*********************************************pilot selection*************************/
String message = myNextion.listen();
Serial.println(message);
if (message == "65 1 1 1 ffff ffff ffff") {
pilote = ben;
}
if (message == "65 1 2 1 ffff ffff ffff") {
pilote = thom;
}
myNextion.setComponentText("t6", String(pilote));
/*********************************************rev counter*******************************/
noInterrupts();
uint32_t t_now = micros();
uint32_t rpmc = rpmcount;
rpmcount = 0;
interrupts();
uint32_t delta_t = t_now - timeold;
timeold = t_now;
rpm = rpmc * 6.0e7 / delta_t;
myNextion.setComponentText("t4", String(rpm));
delay(150);
/*********************************************Battery voltage****************************/
sensorValue = analogRead(analogInPin);
v_pin = Vcc * sensorValue / 1023;
tension = v_pin / (R2 / (R1 + R2));
myNextion.setComponentText("t0", String(tension));
}
void rpm_pulse()
{
rpmcount++;
}
But when I activate the battery voltage part of my loop
Can you point out the "battery voltage part" of your code? I don't see anything that looks like it is reading battery voltage.
myNextion.setComponentText("t6", String(pilote));
Why is it necessary to wrap a String instance in a String instance?
PaulS:
Can you point out the "battery voltage part" of your code? I don't see anything that looks like it is reading battery voltage.
myNextion.setComponentText("t6", String(pilote));
Why is it necessary to wrap a String instance in a String instance?
it is "tension"~voltage that reads the analog pin A0 (I made a voltage divider), that function works perfectly
"t6" is not supposed to be displayed, it's necessary for the screen to know which text area to update
What PaulS is saying is: pilote is a String, so why use String(pilote) and not directly pilote?
But when I activate the battery voltage part of my loop, the button reading from the screen doesn't work anymore.
What do your Serial.print() statements tell you is happening?
sterretje:
What PaulS is saying is: pilote is a String, so why use String(pilote) and not directly pilote?
Because I tried several ways to do it and I forgot to change that...
PaulS:
What do your Serial.print() statements tell you is happening?
The right message without the battery voltage part.
Nothing with it activated but as I insist on the touch button (let's say every 50 attempts) it gives me just "ÿ"
v_pin = Vcc * sensorValue / 1023;
Since sensorValue is an int, in the range 0 to 1023, sensorValue / 1023 will be either 0 or 1. v_pin will then be either 0 or 5.0. Since the reading will be 0 for anything less then a fully charged battery, the doesn't seem too useful.
From your cryptic responses, I can't really understand what your problem is. I can't see that reading the battery voltage will stop the Nextion instance from working, unless the Nextion is using the analog pin for some other purpose.
Adding some Serial.print() statements, to show what the code is doing, and showing us the code with the print statements and the result of the print statements is going to be necessary.
A link to the hardware would not be amiss, either.
I modified the code:
#include <SoftwareSerial.h>
#include <Nextion.h>
#define PIN 4 //chrono
/*************************VARIABLES rev counter*********************************/
volatile byte rpmcount;
volatile unsigned int rpm;
unsigned long timeold;
/*********pilot selection***********************************************/
String pilote;
String ben = "Ben";
String thom = "Thom";
/****************************nextion******************************************/
SoftwareSerial nextion(11, 12);// Nextion TX to pin 11 and RX to pin 12 of Arduino
Nextion myNextion(nextion, 9600);
void setup()
{
Serial.begin(9600);
myNextion.init();
attachInterrupt(1, rpm_pulse, FALLING);//rpm
}
void loop()
{
/*********************************************pilot selection*************************/
String message = myNextion.listen();
Serial.print("message:");
Serial.println(message);
if (message == "65 1 1 1 ffff ffff ffff") {
pilote = ben;
}
if (message == "65 1 2 1 ffff ffff ffff") {
pilote = thom;
}
myNextion.setComponentText("t6",(pilote));
/*********************************************rev counter*******************************/
noInterrupts();
uint32_t t_now = micros();
uint32_t rpmc = rpmcount;
rpmcount = 0;
interrupts();
uint32_t delta_t = t_now - timeold;
timeold = t_now;
rpm = rpmc * 6.0e7 / delta_t;
myNextion.setComponentText("t4", String(rpm));
Serial.print("RPM:");
Serial.println(rpm);
delay(150);
/*********************************************Battery voltage****************************/
int sensorValue = analogRead(A0);
float voltage = sensorValue * (5.0 / 1023.0);
Serial.print("Voltage:");
Serial.println(voltage);
myNextion.setComponentText("t0", String(voltage));
}
void rpm_pulse()
{
rpmcount++;
}
The voltage part is fully based on this so I'm assuming it's not the problem.
The nextion screen is only using 2 serial pins (and GND/+5V).
Here is a link of my hardware.
The serial prints I added give me this:
message:
RPM:0
Voltage:1.69
Only the voltage changes but the analog pin is not connected yet so it's some kind of "free reading".
I can't figure out why the voltage reading can influence my program.
Thank you guys for your help.
The serial prints I added give me this:
Only once?
Only the voltage changes but the analog pin is not connected yet so it's some kind of "free reading".
How can the voltage change if it happens only once?
It is important to be as clear as possible when describing something only you can see.
How are the names ("t0", "t4", "t6", etc.) defined/linked to an action?
inryjo
May 2, 2016, 10:10pm
10
PaulS:
Only once?
How can the voltage change if it happens only once?
It is important to be as clear as possible when describing something only you can see.
How are the names ("t0", "t4", "t6", etc.) defined/linked to an action?
I just pasted a complete serial loop. It is looping as it is supposed to do.
Here is another sample:
message:
RPM:0
Voltage:1.69
message:
RPM:0
Voltage:1.56
message:
RPM:0
Voltage:1.48
...
The names are text objects I create in the screen UI editor, you can find an example here.
system
May 2, 2016, 10:46pm
11
It is looping as it is supposed to do.
That's good. So, what isn't it doing? Do each of the names referred to actually exist?
The message being empty it confirms the program doesn't handle the data properly.
Yes I checked every single name exists.
What about modifying the SoftwareSerial Library ?
(I'm a noob so I don't know where can I start)
The message being empty it confirms the program doesn't handle the data properly.
What program? The one on the Nextion?
What about modifying the SoftwareSerial Library ?
To do, or not do, what?
PaulS:
What program? The one on the Nextion?
Maybe the word I used is not appropriate. I meant everything done in my loop.
PaulS:
To do, or not do, what?
Disabling useless functions ran by the library. Lots of libraries run things that are not necessary.
Disabling useless functions ran by the library. Lots of libraries run things that are not necessary.
You have the source code. If you can find anything in the library that is useless, feel free to delete it.
Maybe the word I used is not appropriate. I meant everything done in my loop.
I don't see how you cab say that. Reading the analog pin happens in koop(), and that is done over and over.
The same is true for the interrupts firing and calculating RPM.
It is the interaction with the Nextion that begins to fail, but I can't imagine why.
I might try changing which analog pin I was using.
inryjo
May 3, 2016, 10:01pm
16
I tried other analog pins and still the same problem.
That's illogical, why this specific part affects the nextion reading while the rpm reading doesn't ?
Any other idea ?
Thanks
system
May 3, 2016, 10:07pm
17
Any other idea ?
Is there a Nextion forum? I really have no other ideas.
I made some research in the nextion research. It turns out people encountered the same issues.