Caspert
Caspert

Reputation: 4363

How to set up Serial Communication?

I have a project that I run with Arduino. Now I want to send data from one Arduino to another via serial communication.

I have connected 2 Arduino's to my computer. Uploaded the following code to Arduino (2):

int incomingByte = 0;   // for incoming serial data

void setup() {
        Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
}

void loop() {

        // send data only when you receive data:
        if (Serial.available() > 0) {
                // read the incoming byte:
                incomingByte = Serial.read();

                // say what you got:
                Serial.print("I received: ");
                Serial.println(incomingByte, DEC);
        }
}

I would like to send ints to the Arduino. Now I doesn't know how to send data to Arduino 2 to 1 with Serial communication.

Upvotes: 1

Views: 81

Answers (1)

Fortran
Fortran

Reputation: 593

First you have to write two programs. One for

Sender

and one for

Receiver

. In other words you must create

Master Writer/Slave Receiver

A simple code, but it doesn't tested:

Sender Code

void setup() {
  Serial.begin(9600);
}

void loop() { 
  Serial.write("test message");
}

Receiver Code

void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
}
    void loop() {
      int i=0;

      if (Serial1.available()) {
        delay(100); 
        while(Serial1.available()) {
         .....
        }

      }
    }

Upvotes: 1

Related Questions