Hello,
I want an Arduino to read INTEGERS from the Serial, ie, if one Arduino sends the number 1745, I want the other Arduino to read 1745 and not some arbitrary ASCII nonsense.
Not knowing how to do it after several days of researching, I tried approaching the problem with a smexy smile and simple words.
This is what I've done:
void setup() {
Serial.begin(9600);
}
void loop () {
int i = 0;
while (Serial.available()) { // while there's something in the serial buffer...
int serialIn = Serial.read();
int string[i] = serialIn; // store the whole string in an array byte-by-byte
i++;
}
Serial.print(string, BYTE); // prints the whole string
Serial.println(); // to make the output neater
delay(1000);
}
Now, there's an error I can't seem to get rid of:
In function 'void loop()':
error: variable-sized object 'string' may not be initialized
What the -blam!-.
Will someone help me out here? I've never used arrays, so I don't know what I'm doing wrong...
Also, is there a simpler way to do what I want?
I just want one Arduino to send data gathered from a sensor to another Arduino via RS485 [but RS485 is a story for another day]...
Thanks!
PS: In case you haven't figured out, I'm kinda still a noob, so please don't post very complex code.
PPS:
Might as well just add the code that I based myself on - which DOES work. The problem is that the data read can't be used, because it's just makeshift, ie, if it reads the number 150 I can't use it to (for example) delay 150ms or PWM an LED with a value of 150.
Here it is:
/* ------------------------------------------------
* SERIAL COM - HANDELING MULTIPLE BYTES inside ARDUINO - 01_simple version
* by beltran berrocal
*
* this prog establishes a connection with the pc and waits for it to send him
* a long string of characters like "hello Arduino!".
* Then Arduino informs the pc that it heard the whole sentence
*
* this is the first step for establishing sentence long conversations between arduino and the pc.
* serialRead() reads one byte at a time from the serial buffer.
* so in order to print out the whole sentence at once
* (it is actually still printing one byte at a time but the pc will receive it
* not interupted by newLines or other printString inside you loop)
* You must loop untill there are bytes in the serial buffer and
* and print right away that byte you just read.
* after that the loop can continue it's tasks.
*
* created 15 Decembre 2005;
* copyleft 2005 Progetto25zero1 <http://www.progetto25zero1.com>
*
* --------------------------------------------------- */
int serIn; //var that will hold the bytes in read from the serialBuffer
void setup() {
Serial.begin(9600);
}
//auto go_to_the_line function
//void printNewLine() {
// Serial.print(13, BYTE);
// Serial.print(10, BYTE);
//}
void loop () {
//simple feedback from Arduino Serial.println("Hello World");
// only if there are bytes in the serial buffer execute the following code
if(Serial.available()) {
//inform that Arduino heard you saying something
Serial.print("Arduino heard you say: ");
//keep reading and printing from serial untill there are bytes in the serial buffer
while (Serial.available()>0){
serIn = Serial.read(); //read Serial
Serial.print(serIn, BYTE); //prints the character just read
}
//the serial buffer is over just go to the line (or pass your favorite stop char)
Serial.println();
}
//slows down the visualization in the terminal
delay(1000);
}