Pages: [1]   Go Down
Author Topic: Serial Communication | Arduino to Arduino - 4 Character Array  (Read 633 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am trying to read 4 analog inputs with one Arduino (transmitter),  create an array for the values, and send the values via serial connection to a second Arduino (receiver).  The receiver uses the values to operate buzzers and lights based on the values of each of the 4 channels.  I am satisfied that my transmitter (shown below) is sending the correct values for the array, but I'm stumped on how to write a code to read the four values into an array on the receiver side.  I mapped the analog values from 0-9 to hopefully make the code a little simpler on the receiver side.

Can anyone please help with a suggestion for my receiver?  Thanks.

void loop()
{
  Data[0] = map(analogRead(CH1), 0, 1023, 0, 9);
  Data[1] = map(analogRead(CH2), 0, 1023, 0, 9);
  Data[2] = map(analogRead(CH3), 0, 1023, 0, 9);
  Data[3] = map(analogRead(CH4), 0, 1023, 0, 9);
  for(i=0; i<4; i++){
   
  Serial.println(Data);
}
Logged

Offline Offline
Edison Member
*
Karma: 9
Posts: 1016
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The Serial.println() needs a string of text. That string must be zero-terminated.
Since you are sending data, I would suggest to use Serial.write()

http://arduino.cc/en/Reference/Serial
http://arduino.cc/en/Serial/Println
http://arduino.cc/en/Serial/Write

For reading serial data, see the example with the Serial.available() function:
http://arduino.cc/en/Serial/Available

But you are still far away from a working communication. Post your code of both the transmitter and receiver to let us know how far you got.
Logged

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

Thanks, Krodal.  Making just that change cleared things up quite a bit on the receiver end.  I'm only trying to estalish one way communication.  I'm able to receive the four channels and monitor them using the IDE serial monitor.  I'm printing labels with my receiver to make sure it's correctly reading the data.  Here's what I have for the 4 channel receiver:

void loop()

  if (Serial.available()>3) // Verify serial data are available to be read
{
  for(int i=0; i<4; i++)
  {
    Data = Serial.read();//Read the next available byte of data and store in out array

// These statements label the data so that we can understand which channel we are
// viewing.  I did not include a carriage return so that the value will follow
// on the same line as the label.

    if(i == 0){
      Serial.print("Channel 1 = ");
    }
    if(i == 1){
      Serial.print("Channel 2 = ");
    }
    if(i == 2){
      Serial.print("Channel 3 = ");
    }
    if(i ==3){
      Serial.print("Channel 4 = ");
    }
    Serial.println(Data); //Writes the individual channels of data
                             //to the IDE serial monitor

  }
  Serial.flush();
}
}

I'm going to use the data to operate four different servos on the receiver side.  I think I know how to do that now that I have the receiver correctly receiving the data.  Where I think I'm going to have problems is when I try to improve the resolution from 0-9 to something better.  I don't think my receiver code will work for that and I'm not sure how to adjus it.  I'll take a look at the references you pointed me to.
Logged

Offline Offline
Edison Member
*
Karma: 9
Posts: 1016
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The best way (for a programmer) is to send readable data.
You also need a byte to indicate a 'start', perhaps also a 'stop'.

So you could send this: [STX]120,130,140,150[ETX]
The [STX] and [ETX] are "Start of text" and "End of text" and are code 0x02 and 0x03.
You could use the scanf function to get integer values from that string of text. You might need some help with the use of scanf.

This requires more programming than what you have now. But it will make things easier in the end. It's not hard to add a 5th number or other data, and you can check the data with a normal terminal monitor.
Logged

Pages: [1]   Go Up
Jump to: