Im struggling a bit to figure out the correct way to combine these two piece of code to do as I want.
The first part of code is an example of how to make a tachometer on a Nextion display, however, it is written to use a potentiometer to map the ADC bits to a series of pictures of the actual gauge. It also has some code to make extra indicators blink but Im only worried about the tach at this point.
This is a link to Youtube of the project Nextion+Arduino Tutorial #4 Custom Gauge And Play Video - YouTube
int sensorPin = A0; // Potentiometer pin to simulate an rpm sensor, for testing
int sensorValue; // Variable to store the value of potentiometer
// Calibration for smoothing RPM:
const int numReadings = 20; // number of samples for smoothing. The higher, the more smoothing, but slower to react. Default: 20
// Calibration for tachometer deadzone:
const int TachometerDeadzoneSamples = 2; // amount of samples for dead zone (1 would be no dead zone). Default: 2
// Calibration for tachometer limit:
const int TachLimitAmount = 20; // how many cycles we are going to wait when tach reach limit value, before showing that value. Default: 20
// Variables for smoothing tachometer:
int readings[numReadings]; // the input
int readIndex = 0; // the index of the current reading
long total = 0; // the running total
int average = 0; // the average
// Variables for deadzone for tachometer:
int TachometerWithDeadzone;
// Variables for tachometer limit:
int TachLimitCounter1 = 0; // counter to wait for how long we see a limit value for tach, before showing that value
int TachLimitCounter2 = 0; // counter to wait for how long we see a limit value for tach, before showing that value
// Variable to store the tachometer value after remaped:
int TachometerRemaped;
// Variable to store rpm:
int RealRPM = 0;
void setup() { // Put your setup code here, to run once:
Serial.begin(9600); // Start serial comunication at baud=9600
// I am going to change the Serial baud to a faster rate.
delay(500); // This dalay is just in case the nextion display didn't start yet, to be sure it will receive the following command.
Serial.print("baud=115200"); // Set new baud rate of nextion to 115200, but it's temporal. Next time nextion is power on,
// it will retore to default baud of 9600.
// To take effect, make sure to reboot the arduino (reseting arduino is not enough).
// If you want to change the default baud, send the command as "bauds=115200", instead of "baud=115200".
// If you change the default baud, everytime the nextion is power ON is going to have that baud rate, and
// would not be necessery to set the baud on the setup anymore.
Serial.write(0xff); // We always have to send this three lines after each command sent to nextion.
Serial.write(0xff);
Serial.write(0xff);
Serial.end(); // End the serial comunication of baud=9600
Serial.begin(115200); // Start serial comunication at baud=115200
} // End of setup
void loop() { // Put your main code here, to run repeatedly:
delay(20); // I put this delay because without it, the timer on the display would stop running.
// Aparently we shouldn't send data to the display too often.
sensorValue = analogRead(sensorPin); // Read analog pin where the potentiometer is connected
RealRPM = map (sensorValue, 0, 1023, 0, 8000); // Remap pot to simulate an RPM value
RealRPM = constrain(RealRPM, 0, 8000); // Constrain the value so it doesn't go below or above the limits
int TachometerRemapedWithoutSmoothing = map (RealRPM, 0, 8000, 0, 208); // Remap the raw RPM to match the tachometer value range
TachometerRemapedWithoutSmoothing = constrain(TachometerRemapedWithoutSmoothing, 0, 208); // Constrain the value so it doesn't go below or above the limits
// Smoothing RPM:
// subtract the last reading:
total = total - readings[readIndex];
// read speed:
readings[readIndex] = RealRPM; // takes the value we are going to smooth
// add the reading to the total:
total = total + readings[readIndex];
// advance to the next position in the array:
readIndex = readIndex + 1;
// if we're at the end of the array...
if (readIndex >= numReadings) {
// ...wrap around to the beginning:
readIndex = 0;
}
// calculate the average:
average = total / numReadings; // the average value it's the smoothed result
TachometerRemaped = map (average, 0, 8000, 0, 208); // Remap the smoothed RPM to match the tachometer value range
TachometerRemaped = constrain(TachometerRemaped, 0, 208); // Constrain the value so it doesn't go below or above the limits
// Deadzone for tachometer:
// This is another layer of smoothing the tachometer. It adds some dead zone so it doesn't go back and forward to the same
// adjacent values. This is because RPM sensors have some error range and jump between different values for the same speed.
TachometerWithDeadzone = TachometerWithDeadzone + ((TachometerRemaped - TachometerWithDeadzone)/TachometerDeadzoneSamples);
// By putting dead zone on tachometer, the limits can't be reached anymore. Because of this we need to check min and max limits:
// Min limit:
if (TachometerRemaped == 0) // if tachometer is 0
{
if(TachLimitCounter1 == TachLimitAmount) // if we wait long enough and still is 0
{
TachometerWithDeadzone = 0; // show real tach as 0
}
else // since we didn't wait long enough if tachometer it's still 0
{
TachLimitCounter1++; // count to wait if tachometer it's going to remain 0
}
}
else // since tachometer its not 0
{
TachLimitCounter1 = 0; // reset counter
}
// Max limit:
if (TachometerRemaped == 208) // if tachometer is 208 (the maximun limit)
{
if(TachLimitCounter2 == TachLimitAmount) // if we wait long enough and still is 208
{
TachometerWithDeadzone = 208; // show real tach as 208
}
else // since we didn't wait long enough if tachometer it's still 208
{
TachLimitCounter2++; // count to wait if tachometer it's going to remain 208
}
}
else // since tachometer its not 208
{
TachLimitCounter2 = 0; // reset counter
}
// Send tachometer value:
Serial.print("tach.pic="); // This is sent to the nextion display to set what object name (before the dot) and what atribute (after the dot) are you going to change.
// Choose one of the folling two, on what value to send to the tachometer:
//Serial.print(TachometerWithDeadzone); // Send RPM smoothed and with deadzone
Serial.print(TachometerRemapedWithoutSmoothing); // Send RPM without any smoothing at all
Serial.write(0xff); // We always have to send this three lines after each command sent to the nextion display.
Serial.write(0xff);
Serial.write(0xff);
} // End of loop
I have successfully copied the project and it worked as I want.
Since I would like this to actually read RPM, I found this code that reads a hall effect sensor and I have verified that it also works when I hook up the Hall signal and read data in the serial monitor.
int refsig=200;//for converting the analog signal coming from hall sensor to digital through arduino code
int val;//the digital value of the incoming analog signals
int prev_val=0;
unsigned long t,cur_t;//time variables
void setup()
{
Serial.begin(115200);
pinMode(A0,INPUT);
}
void loop()//Measure RPM
{
int sig=analogRead(A0);//read raw value of hall sensor
if(sig>refsig) val=HIGH;//convert it to digital 0,1 form
else val=LOW;
if(prev_val==0 && val==1) {//check for rising edge
cur_t=micros();
Serial.println(1000000*60/(cur_t-t));//print the rpm
t=micros();
}
prev_val=val;
}
What I am having a hard time with is mapping the second code to have it work within the first code. I have a feeling that I need to do a few extra steps to make it work, but Im lost on what that is. Ive tried searching for Mapping Frequency and similar things, but it doesnt appear to be what Im looking for. I believe that this line in the second code
Serial.println(1000000*60/(cur_t-t));//print the rpm
Needs to be changed to something else so that it will read in this portion of the first code
sensorValue = analogRead(sensorPin); // Read analog pin where the potentiometer is connected
RealRPM = map (sensorValue, 0, 1023, 0, 8000); // Remap pot to simulate an RPM value
RealRPM = constrain(RealRPM, 0, 8000); // Constrain the value so it doesn't go below or above the limits
int TachometerRemapedWithoutSmoothing = map (RealRPM, 0, 8000, 0, 208)
Nextion_Tutorial4-1.ino (21 KB)
halltach.ino (789 Bytes)