Just new to Arduino Coding.
My code involves sending a string to another Arduino from a serial input.
I'm using the SoftwareSerial library to connect the two Arduino's together.
I'm very new to this and have no idea what I'm doing wrong.
When I type hello into the serial monitor, my code returns hÿÿÿÿ elloÿ.
if (Serial.available() > 0){
for (int i=0; i < 5; i++){
input[i] = Serial.read();
You're reading five bytes when you're only guaranteed to have "more than zero" available. When you read a byte that isn't there, you get -1 which prints as the y with umlaut.
I edited my code for both your replies but both seem to not work as well.
int retrieveData()
{
if (Serial.available() > 0)
{
for (int i=0; i < 5; i++)
{
input[i] = Serial.read();
}
for (int i=0; i < 5; i++)
{
input[i]++; // some simple encryption;)
}
for (int i=0; i < 5; i++)
{
ArduinoLittleBro.print(input[i]);
Serial.print(input[i]); // added that to see what was being sent to the other Arduino
}
ArduinoLittleBro.println();
}
}
Even better would be check for greater than OR EQUAL to 5.
Get rid of the String class. It is not needed.
Don't forget that the definition of a string is a NULL terminated array of chars. You have the array of chars part, but not the NULL terminator. Instead of dealing with the array of chars as an array of chars, learn to deal with it as a string, after learning to add the NULL terminator.
Sinse you are probably writing the code on both arduino, then in the sending code use a delimiter to signal the end of the data packet, and use code like below on the receiving arduino. You can test the below using the serial monitor.
//zoomkat 3-5-12 simple delimited ',' string parce
//from serial port input (via serial monitor)
//and print result out serial port
// CR/LF could also be a delimiter
String readString;
void setup() {
Serial.begin(9600);
Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}
void loop() {
//expect a string like wer,qwe rty,123 456,hyre kjhg,
//or like hello world,who are you?,bye!,
if (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
if (c == ',') {
//do stuff
Serial.println(readString); //prints string to serial port out
readString=""; //clears variable for new input
}
else {
readString += c; //makes the string readString
}
}
}