(Im new to the arduino side of stackexchange). Im working on a worksheet for to begin with arduino. I have prior experiences working with arduinos, but problem is new for my field of expertise. Any help will be well appreciated (need help pls uwu)
Problem statement:
I tried different methods to obtain those values, but all are to vain.
Methods that I tried so far:
- using limits.h, floats.h and inbuild variables of those library -- Problem: Im getting wrong minimum and maximum values for float and double: Code:
#include <stdio.h>
#include <limits.h>
#include <float.h>
#include <math.h>
char fltmax[250];
char fltmin[250];
char dblmax[250];
char dblmin[250];
void sint() {
Serial.print("Range of signed int: ");
Serial.print(INT_MIN);
Serial.print(" to ");
Serial.print(INT_MAX);
Serial.print(", which has a storage size of ");
Serial.print(sizeof(int));
Serial.println(" bytes");
}
void usint() {
Serial.print("Range of unsigned int: 0 to ");
Serial.print(UINT_MAX);
Serial.print(", which has a storage size of ");
Serial.print(sizeof(unsigned int));
Serial.println(" bytes");
}
void schar() {
Serial.print("Range of signed char: ");
Serial.print(SCHAR_MIN);
Serial.print(" to ");
Serial.print(SCHAR_MAX);
Serial.print(", which has a storage size of ");
Serial.print(sizeof(signed char));
Serial.println(" bytes");
}
void uschar() {
Serial.print("Range of unsigned char: 0 to ");
Serial.print(UCHAR_MAX);
Serial.print(", which has a storage size of ");
Serial.print(sizeof(unsigned char));
Serial.println(" bytes");
}
void slong() {
Serial.print("Range of signed long: ");
Serial.print(LONG_MIN);
Serial.print(" to ");
Serial.print(LONG_MAX);
Serial.print(", which has a storage size of ");
Serial.print(sizeof(signed long));
Serial.println(" bytes");
}
void uslong() {
Serial.print("Range of unsigned long: 0 to ");
Serial.print(ULONG_MAX);
Serial.print(", which has a storage size of ");
Serial.print(sizeof(unsigned long));
Serial.println(" bytes");
}
void sshort() {
Serial.print("Range of signed short: ");
Serial.print(SHRT_MIN);
Serial.print(" to ");
Serial.print(SHRT_MAX);
Serial.print(", which has a storage size of ");
Serial.print(sizeof(signed short));
Serial.println(" bytes");
}
void usshort() {
Serial.print("Range of unsigned long: 0 to ");
Serial.print(USHRT_MAX);
Serial.print(", which has a storage size of ");
Serial.print(sizeof(unsigned short));
Serial.println(" bytes");
}
void flt() {
Serial.print("Range of float: ");
dtostrf(FLT_MIN, -1, 50, fltmin);
Serial.print(fltmin);
Serial.print(" to ");
sprintf(fltmax,"%e", FLT_MAX);
puts(fltmax);
Serial.print(", which has a storage size of ");
Serial.print(sizeof(float));
Serial.println(" bytes");
}
void dbl() {
Serial.print("Range of double: ");
dtostrf(DBL_MIN, -1, 50, dblmin);
Serial.print(dblmin);
Serial.print(" to ");
dtostrf(DBL_MAX, -1, 0, dblmax);
Serial.print(dblmax);
Serial.print(", which has a storage size of ");
Serial.print(sizeof(double));
Serial.println(" bytes");
}
void setup() {
Serial.begin(115200);
sint();
usint();
schar();
uschar();
slong();
uslong();
sshort();
usshort();
flt();
dbl();
}
void loop() {
}
- Other than using dtostrf(), I found better alternatives at exchange here too such as: Print unsigned long long in ESP8266 and https://www.tutorialspoint.com/c_standard_library/c_function_sprintf.htm
I tried combining both where the test code that I used before actual implementation:
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <float.h>
#include <limits.h>
void setup () {
char str[30];
Serial.begin(115200);
snprintf(str, 30, "Value of float max = %e", FLT_MAX);
/*puts(str);*/
Serial.print(str);
return(0);
}
void loop(){
}
But puts(str)
is not getting printed in the serial monitor. Then It clicked, that I have to use Serial.(code) for it to get printed in serial monitor, but it ended up in Serial.puts(str)
code not found. Then I tried just Serial.print(str)
, but it just outputs Value of float max = ?
(Initially, before I wrote this problem here, it output Va
only, which i have no idea how it changed, but its still wrong). What do I do now? also, Why cant I use puts to print in serial monitor?
The board im using for this is Arduino UNO (ATmega328P)