Hi!
I just made a code that is supposed to calculate a circumference of a circle.
void setup()
{
Serial.begin(9600);
}
int incomingByte = 0;
void loop()
char buffer[100];{
if (Serial.available() > 0) {
int circle(int incomingByte);
float Pi = 3.141592;
int incomingByte = Serial.readBytes(buffer, 9);
atof(incomingByte);
Serial.println("");
Serial.println(Pi*2*incomingByte);
}
}
When i send number 1 to COM, the result is 6.28.
When i send number 2 to COM, the result is also 6.28.
The same happens with numbers 1.1 and 2.2.
Why are the calculations always the same???
your code doesn't compile... and there are too many things wrong to comment exhaustively until you get that right.
but a few pointers:
Serial.readBytes() returns the number of characters placed in the buffer. A 0 means no valid data was found.
so as long as you enter one byte, you will get the same result here:
atof(incomingByte);
it seems like you are expecting more, trying get nine bytes...
Serial.readBytes(buffer, 9);
Read this: Serial.readBytes() - Arduino Reference
I think you want Serial.parseFloat();
Your sketch won't compile for me:
sketch_oct18b:8: error: expected initializer before 'char'
char buffer[100]; {
^
sketch_oct18b:8: error: expected unqualified-id before '{' token
char buffer[100]; {
^
exit status 1
expected initializer before 'char'
That bracket is in the wrong place. Fixing that mistake gets me this warning:
/Users/john/Documents/Arduino/sketch_oct18b/sketch_oct18b.ino: In function 'void loop()':
/Users/john/Documents/Arduino/sketch_oct18b/sketch_oct18b.ino:14:22: warning: invalid conversion from 'int' to 'const char*' [-fpermissive]
atof(incomingByte);
^
In file included from /Users/john/Library/Arduino15/packages/arduino/hardware/avr/1.6.19/cores/arduino/Arduino.h:23:0,
from sketch/sketch_oct18b.ino.cpp:1:
/Users/john/Library/Arduino15/packages/arduino/tools/avr-gcc/4.9.2-atmel3.5.4-arduino2/avr/include/stdlib.h:361:15: note: initializing argument 1 of 'double atof(const char*)'
extern double atof(const char *__nptr);
^
Sketch uses 4202 bytes (13%) of program storage space. Maximum is 32256 bytes.
Global variables use 202 bytes (9%) of dynamic memory, leaving 1846 bytes for local variables. Maximum is 2048 bytes.
I'm kind of surprised that " int circle(int incomingByte);" parses at all. It looks like a function prototype inside loop()!
I think what you meant to do is:
const float Pi = 3.141592;
void setup() {
Serial.begin(9600);
}
void loop() {
float inputValue = Serial.parseFloat();
Serial.println();
Serial.println(Pi * 2 * inputValue);
}
johnwasser:
I'm kind of surprised that " int circle(int incomingByte);" parses at all. It looks like a function prototype inside loop()!
that is a valid int constructor, even though it is being initialized with an un-initialized value...
void setup()
{
Serial.begin(9600);
int myValue(5);
Serial.println(myValue);
}
void loop()
{
}
Thank you, johnwasser and BulldogLowell!
My code now works and everyhing is fine.
Both will get +1 karma.
Thank you!
And the sketch compiled normally for me without any warnings.
That's weird