Pages: [1]   Go Down
Author Topic: Serial communication between two arduinos  (Read 934 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I need som help with my code (is not a programmer...
I want to communicate serial between two Arduino. A potentiometer on one board to control an output on the other. I'm building a ROV and want to control the steering in this way.
Can I have the serial communication through 60 feet of cable?)

------------Transmit code on board no 1---------------
//Send potentiometer value from one Arudino board to another
const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin with LED for control of pot function
int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM

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

void loop()
{
  // read the analog in value:
  sensorValue = analogRead(analogInPin);           
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 255); 
  // change the analog out value:
  analogWrite(analogOutPin, outputValue);           

  // print the results to the serial monitor:
   Serial.println(outputValue);   
  delay(10);                     
}

------------Receiving code on board no 2---------------
//Recive potentiometer value and dim a LED
int outputValue = 127;         // start value
const int analogOutPin = 6; // Analog output pin that the LED is attached to

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

void loop()
{
  // if we get a valid byte, read analog ins:
   if (Serial.available() > 0) {
    // get incoming byte:
    outputValue = Serial.read();
     delay(10);         
  }
analogWrite(analogOutPin, outputValue);
}


Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49372
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I need som help with my code (is not a programmer...
OK.

Code:
Serial.println(outputValue);   
Sends the value as a string.

Code:
    outputValue = Serial.read();
Reads one character, and assumes that it is the value. It isn't.

You need to change the sender to add start and end of packet delimiters, and change the receiver to read a string, and convert the value back to an int.

Sender:
Code:
Serial.print("<");
Serial.print(outputValue);
Serial.print(">");

Receiver code:
 
Code:
#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(57600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}
Where it says "Process the packet", inData will contain the string sent by the sender, minus the start and end of packet markers, so
Code:
outputValue = atoi(inData);
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you. It seems to work :-) and I think I understand your code ...
Does this only with one sensor or can I define multiple "outputs"?

/Örjan
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49372
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Does this only with one sensor or can I define multiple "outputs"?
You can have any number of outputs. Separate them with commas or other delimiters. On the receiver, then, use strtok() to extract the tokens (the string representing each value) and convert the token to a numeric value using atoi().
Logged

Toronto, Canada
Offline Offline
Edison Member
*
Karma: 2
Posts: 1234
"Keep it R.E.I.L. - "Research, Experiment, Investigate and Learn"
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@PaulS

That is a useful tread. I am learning here... About atoi() and strtok() function... I can not find those functions ( atoi() and strtok() ) in the Arduino reference list of functions IDE --> Help--> reference  Where can I find that ? and do I need a #include <whatever.h> to use those functions ?
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

atoi() is part of the C standard library: stdlib.h (http://www.cplusplus.com/reference/clibrary/cstdlib/)
strtok() is part of the C string library: string.h (http://www.cplusplus.com/reference/clibrary/cstring/)

The IDE includes them automatically, I believe.
Logged

Toronto, Canada
Offline Offline
Edison Member
*
Karma: 2
Posts: 1234
"Keep it R.E.I.L. - "Research, Experiment, Investigate and Learn"
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@dxw00d

Thank for the link. 

Quote
The IDE includes them automatically, I believe.

I hope you right. If not, well I have to type : #include<stdlib.h>
Logged

Pages: [1]   Go Up
Jump to: