I wrote this simplified code while trying to understand the use of decimals with analog input in my main program, as I've done little with the UNO analog pins until now.
I've plainly misunderstood how to specify the number of decimals when printing debug info. I thought a comma followed by the number of decimal places required? Where am I going wrong please?
const byte readingPin = A5;
void setup()
{
Serial.begin(115200);
delay(300);
int reading = analogRead(readingPin); // read the value from a 10K pot
Serial.println("====================");
// Serial.print("reading = ");
// Serial.println(reading); // Prints 282 (correct)
// Serial.println(reading, 0); // Prints (unprintable, small square)
// Serial.println(reading, 1); // Prints 282 (correct)
// Serial.println(reading, 2); // prints 100011010
// Serial.println(reading, 3); // prints 101110
// So I tried more examples:
for (byte i = 0; i <= 20; i++)
{
Serial.print("Serial.println(reading, ");
Serial.print(i);
Serial.print("); prints ");
Serial.println(reading, i);
}
while (1 == 1); // Stops program from proceeding
}
void loop()
{
}
/*
RESULTS:
Serial.println(reading, 0); prints
Serial.println(reading, 1); prints 282
Serial.println(reading, 2); prints 100011010
Serial.println(reading, 3); prints 101110
Serial.println(reading, 4); prints 10122
Serial.println(reading, 5); prints 2112
Serial.println(reading, 6); prints 1150
Serial.println(reading, 7); prints 552
Serial.println(reading, 8); prints 432
Serial.println(reading, 9); prints 343
Serial.println(reading, 10); prints 282
Serial.println(reading, 11); prints 237
Serial.println(reading, 12); prints 1B6
Serial.println(reading, 13); prints 189
Serial.println(reading, 14); prints 162
Serial.println(reading, 15); prints 13C
Serial.println(reading, 16); prints 11A
Serial.println(reading, 17); prints GA
Serial.println(reading, 18); prints FC
Serial.println(reading, 19); prints EG
Serial.println(reading, 20); prints E2
*/
int reading = analogRead(readingPin); // read the value from a 10K pot
reading is declared as an int but you are trying to print it as if it were a float
The second parameter when you are printing a int is usually DEC, HEX etc each of which each have numeric values which is how the compiler is using your for loop value, hence the output that you are getting
To see what I mean, try printing the value of the HEX constant, for example
Agreed, but it let me move confidently onto the next stage. I'm actually using several pots to determine values such as duration and volume. My problem was that I couldn't see how to get the rounded result I wanted. Printing examples with decimals was a requirement in fathoming that.
Here's a minimal version of code to get duration. Is there a neater way of doing the rounding?
const byte readingPin = A5;
//unsigned long duration;
float duration;
void setup()
{
Serial.begin(115200);
delay(300);
// Get duration (0 to 60 mins)
float reading = analogRead(readingPin); // read the value from a 10K pot
duration = 3600 * reading / 1023; // 0 to 1 hour, in s
Serial.println(F("===================="));
Serial.print(F("reading = "));
Serial.println(reading);
Serial.print(F("duration = "));
Serial.print(duration);
Serial.println(F( " s"));
duration = duration / 60; // To get duration in minutes
Serial.print(F("duration = "));
Serial.print(duration);
Serial.println(F( " mins")); // Prints 59, but it's almost 60
// which is the rounded result I want printed.
// By also changing duration to a float type, instead of unsigned
// long, I get 59.94 mins printed.
// I see no 'rounding' command?
// So what is the recommended way to do so,
// rather than using floor() or ceil() to round down or up?
// Best I can manage is:
if (ceil(duration) - duration < 0.5)
{
duration = ceil(duration);
}
else
{
duration = floor(duration);
}
Serial.print("duration rounded = ");
Serial.println(duration);
}
void loop()
{
}