Hutkikz:
It's not the comment section it is the global space where global variables can be declared/initialized.
There are always several ways to do things. The halltach code uses the predefined( in the arduino library) pin name A0 so it doesn't need to be declared. While the nextion code assignes it a name that more clearly indicates what it is doing. Also the nextion code takes advantage of the fact that all pins by default are set as inputs so they do not bother to set them in setup.If you comment out these two lines:
int sensorPin = A0; // Potentiometer pin to simulate an rpm sensor, for testing
int sensorValue; // Variable to store the value of potentiometer
and add in the variables from the halltach code:// int sensorPin = A0; // Potentiometer pin to simulate an rpm sensor, for testing
// int sensorValue; // Variable to store the value of potentiometer
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
then comment out these 2 lines:sensorValue = analogRead(sensorPin); // Read analog pin where the potentiometer is connected
RealRPM = map (sensorValue, 0, 1023, 0, 8000); // Remap pot to simulate an RPM valueand add the halltach code right there:// 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
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();
RealRPM = (1000000*60/(cur_t-t));//print the rpm
t=micros();
}
prev_val=val;
RealRPM = constrain(RealRPM, 0, 8000); // Constrain the value so it doesn't go below or above the limits
That should do it. It isn't the way I would do it but it follows the style they used. Like I said always lots of different ways to do stuff.
That did not work unfortunately. I tried to add the A0 code in the Global area, tried to remove that and add the sensor declaration in the Setup and tried a few different curley bracket combinations to see if it would work and none did.
I am thinking that maybe sensorValue = analogRead(sensorPin); shouldnt be commented out but should be where the HALLTACH code should be, but then we are back to the next line where RealRPM is mapping ADC bits.
Stepping back a bit, can you maybe explain how the modifications to the code you recommended should work? I know that the tutorial code works by mapping the 0-8000 to the 209 pictures that make up a sweeping needle on the screen and prior to that it maps the ADC from 0-1023 into 0-8000. How does the modification do the same thing? Im almost thinking that I use the HALLTACH code and add the serial writes to the nextion within that, but either way, I need to figure out how to call out particular pictures based on a mapped RPM range.
I suppose what Im asking is not so much how to do this, but how would you do things like this in general? You said you wouldn't do it the way you posted, but there are multiple ways to do it, so how would you do it? The only reason Im picking out the two parts of code that I posted is that both function perfectly on their own, but maybe there is a better way to approach this and I should start with my own code (although that troubles me as I still dont understand why this doesnt work).