Newbie Frustration

Hey Guys,

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ÿ.

//Include Libaries
#include "SoftwareSerial.h"

SoftwareSerial ArduinoLittleBro(11, 10); //rx-tx yellow-orange

char input[16];
String command;

void setup(){
  Serial.begin(9600);
  ArduinoLittleBro.begin(9600);
  
  
}
void loop(){
  retrieveData();
 
}


int retrieveData(){
  if (Serial.available() > 0){
    for (int i=0; i < 5; i++){
       input[i] = Serial.read();
    }
       ArduinoLittleBro.print(input);
       Serial.println(input); 
        
    }
  
  }

If anyone has any ideas on how I can do this better, I'd be very thankful.

Thanks again,

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.

Perhaps a check for > 5 would work better?

-br

you check if there is at least 1 byte available and you read 5.
You should test for the same amount as you read, preferably per byte,

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]);
    }
    ArduinoLittleBro.println();
  }
}

Thanks for the quick reply guys.

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();
  }
}

I get i fmmp from hello

there is still --> if (Serial.available() > 0) -- to be fixed , see post billroy

Eeeek, thanks :slight_smile:

Perhaps a check for > 5 would work better?

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
    }
  }
}