Hi all!
I have a program uploaded in Arduino Mega 2560, which has a bunch of variables. Is it possible is some way to change these variables while the arduino board is still working, from a PC?
So lets say there is a variable defined as "float a=-0.0276; and I want to change it into a=-0.376...
How would I do that? For now I'm communicating with the arduino through a serial interface with a software HTerm 0.8.1 beta, later I will probably use Matlab(2009).
Please help as soon as possible!
Thanks in advance,
Answers can be found all over the Arduino site which you would do well to browse and bookmark the major pages of.
Here is Learning Examples, look at the section titled Communication.
And you already know the Reference page? Under Communication, see Serial for details.
Thanks but this doesn't help me much
In what way doesn't it help?
Please help us to help you - we're not psychic.
for now I have a code like this:
float a = 0.00;
void setup() {
Serial.begin(9600);
}
void loop() {
// send data only when you receive data:
if (Serial.available() )
{
a = Serial.read();
}
Serial.print(a);
Serial.print(";");
delay(1000);
}
and I do not now further steps to make what I need
in this code, as you can probably figure it out, i can only send dec numbers from 0-255 to get this same output at the serial print, and I need it for a all possible numbers(-inf to +inf)
and I'm a beginner in programing
Serial.read() reads a character and returns that ( or -1 while nothing is available)
This is just the starting point.
I recommend to study Nick's great tutorial on Serial http://gammon.com.au/serial to get started...
and I need it for a all possible numbers(-inf to +inf)
That is simply not possible. The Arduino doesn't support any datatype with the size, or precision, to store every possible number.
dxw00d:
and I need it for a all possible numbers(-inf to +inf)
That is simply not possible. The Arduino doesn't support any datatype with the size, or precision, to store every possible number.
you can implement it, so yes, it is possible (+ inf and -inf normally are flag setted when you oveflow some numeric class, like NaN (not a number) when you try to divide by 0. this help to create less black hole :D)
I guess, once OP has learned how to enter a float number via Serial into a float variable,
he could also set it to NaN, -INF, +INF , if he really wants to.
I'm not sure what the word "support" means in that scenario, but these bit patterns are possible, the ATMEGA 328 controller itself does not care.
What is a "less black hole" ? -- just to keep this thread off topic
well, i'm not good in english, i mean we will produce less blackhole:
please forget about +-inf, I just wanted to say that i need to upload a float number, sometimes it would be -0.547, sometimes 0.547 or 1.5 etc.. I will never need +- inf
A good place to start would maybe be atof
we are jocking and OT, sorry.
there are 2 way:
1: you send the byte rapresentation: that is, 4 byte and you are done! obviusly thase byte have to be reconstructed in the right order, and came from another float value of 4 byte(this is not a reitable standard on PC and other programming language), oterwise you have to convert it into the right format
2: you send the string rapresentation: that way you can send a variable number of byte.
first of all you chck if there is a +/- at the beginning and remove it. (but set a flag to remeber if the number is positive or negative).
now you find the "," and count how many number are on his right (remeber, every byte is a char and can be a number from 0 to 9).
remove the ",", and now take your flat sum the number on the left, multuply 10, and so on until you processed every number. now we have to set the decimal part. simpli divide by 10*number of "number on the right of ','"
that is, easy right?
edit: oh right, there are "premade" function for point 2. But if you never implemnt it by yourself, when you will have a conversation problem you will never understand where bug can come from
that is, easy right?
Not as easy as using "atof".
AWOL:
that is, easy right?
Not as easy as using "atof".
look at my edit. BTW we say UCAS, "Ufficio Complicazione Affari Semplici", it mean "Office for Complication of Easy Things"
and i work for them
Ok now its a little less black
OT:
UCAS, "Ufficio Complicazione Affari Semplici"
In England, UCAS is the university central admissions authority, but I think the Italian explanation works for them just as well!
Smradac:
Thanks but this doesn't help me much
Here is one Example:
http://arduino.cc/en/Tutorial/ReadASCIIString
Read ASCII String
This sketch uses the Serial.parseInt() function to locate values separated by a non-alphanumeric character. Often people use a comma to indicate different pieces of information (this format is commonly referred to as comma-separated-values), but other characters like a space or a period will work too. The values are parsed into ints and used to determine the color of a RGB LED. You'll use the serial monitor to send strings like "5,220,70" to the Arduino to change the lights.
From there you look on the Reference page and click on Serial. That page shows not only parseInt() but parseFloat().
If it doesn't help you much then you are not really trying.
I found this code somewhere online and it works for me.
Now if I could just figure out how to read a vector of floating numbers :S, because I'm having trouble when data is sent from matlab while wanting to send more then one variable!
void setup(){
Serial.begin(9600);
}
void loop()
{
Serial.println("Hypotenuse of a right triangle calculator");
Serial.println("What is the base?");
float x = getFloatFromSerialMonitor();
Serial.println(x);
Serial.println("What is the height?");
float y = getFloatFromSerialMonitor();
Serial.println(y);
Serial.println("The Hypotenuse is:");
Serial.println(sqrt(x*x+y*y));
Serial.println("~~~~~~~~~~~~~~~~~~");
}
float getFloatFromSerialMonitor(){
char inData[20];
float f=0;
int x=0;
while (x<1){
String str;
if (Serial.available()) {
delay(100);
int i=0;
while (Serial.available() > 0) {
char inByte = Serial.read();
str=str+inByte;
inData[i]=inByte;
i+=1;
x=2;
}
f = atof(inData);
memset(inData, 0, sizeof(inData));
}
}//END WHILE X<1
return f;
}
Your code nearly does it already:
what's the purpose of int x ( the one inside getFloatFromSerialMonitor : you might use that as index into your float array to return. )
Questions remaining :
How do you distinguish between 2 numbers ? ( A comma , semicolon, space ?)
Do you know in advance how many floats you get ?
modify float getFloatFromSerialMonitor() to
float* get3FloatsFromSerialMonitor()
Finding nice code is great, but latest to enhance it, you need to understand it.
Another hint
Your unused String str;
shows that you are not afraid of memory problems. Perhaps dynamically creating the float array might work for you, too.
It's better however, if the calling loop() provides the float array, and if you remove that String str;