Go Down

Topic: Moving analog data over serial (Read 1 time) previous topic - next topic

jlefevre1

Jan 01, 2013, 09:46 pm Last Edit: Jan 01, 2013, 10:41 pm by Coding Badly Reason: 1
I need to move the analog data from the input pins on one arduino to another arduino over the serial port. I am able to get the analog data going out the serial port of the "transmitter" arduino and I have connected the tx output of the transmitter to the rx of the reciever. But I cannot get the data to the reciever.

Here is the transmitter program

Code: [Select]
int  bcd1 = 2;
int  bcd2 = 3;
int  bcd4 = 4;
int  bcd8 = 5;
int  analogpinred = 0;
int  analogpingreen = 1;
int  analogpinblue = 2;
int  analogpinamber = 3;
int  analogpinpink = 4;
int  red = 0;
int  green = 0;
int  blue = 0;
int  amber = 0;
int  pink = 0;
int  valuecmd = 0;

void setup ()
{
Serial.begin(9600);  
pinMode  (bcd1, INPUT);
digitalWrite (bcd1, HIGH);
pinMode  (bcd2, INPUT);
digitalWrite  (bcd2, HIGH);
pinMode  (bcd4, INPUT);
digitalWrite (bcd4, HIGH);
pinMode  (bcd8, INPUT);
digitalWrite (bcd8, HIGH);
}

void loop ()
{
readanalog ();
buildbcd ();
serialoutput ();
}

void  readanalog ()
{
 red = analogRead(analogpinred);
 green = analogRead(analogpingreen);
 blue = analogRead(analogpinblue);
 amber = analogRead(analogpinamber);
 pink = analogRead(analogpinpink);
}


void  buildbcd ()
{
 valuecmd = 0;
 if (digitalRead(bcd1) == LOW)
 {
   valuecmd = valuecmd + 1;
 }
 if (digitalRead(bcd2) == LOW)
 {
   valuecmd = valuecmd + 2;
 }
 if (digitalRead(bcd4) == LOW)
 {
   valuecmd = valuecmd + 4;
 }
 if (digitalRead(bcd8) == LOW)
 {
   valuecmd = valuecmd + 8;
 }
}

void serialoutput ()
{
 Serial.print(red);
 Serial.print(",");
 Serial.print(green);
 Serial.print(",");
 Serial.print(blue);
 Serial.print(",");
 Serial.print(amber);
 Serial.print(",");
 Serial.print(pink);
 Serial.print(",");
 Serial.print(valuecmd);
 Serial.println("");
}


and here is the reciever program

Code: [Select]
int  redpin = 2;
int  greenpin = 5;
int  bluepin = 6;
int  amberpin = 9;
int  pinkpin = 10;
int  outA = 2;
int  outB = 4;
int  outC = 7;
int  outD = 8;
int  outE = 11;
int  outF = 12;
int  outG = 13;
int  red = 0;
int  green = 0;
int  blue = 0;
int  amber = 0;
int  pink = 0;
int  valuecmd = 0;

void setup ()
{
Serial.begin(9600);  
pinMode (redpin, OUTPUT);
pinMode (greenpin, OUTPUT);
pinMode (bluepin, OUTPUT);
pinMode (amberpin, OUTPUT);
pinMode (pinkpin, OUTPUT);
pinMode (outA, OUTPUT);
pinMode (outB, OUTPUT);
pinMode (outC, OUTPUT);
pinMode (outD, OUTPUT);
pinMode (outE, OUTPUT);
pinMode (outF, OUTPUT);
}

void loop ()
{
readserial ();
printserial ();
}

void  readserial ()
{
 while (Serial.available() > 0)
 {
   red = Serial.parseInt();
   green = Serial.parseInt();
   blue = Serial.parseInt();
   amber = Serial.parseInt();
   pink = Serial.parseInt();
   valuecmd = Serial.parseInt();
   if (Serial.read() == '\n')
   {
     output128 ();
     output64 ();
     output32 ();
     output16 ();
     output8 ();
     output4 ();
     output2 ();
     output1 ();
   }
 }
}

void output128 ()
{
 if (valuecmd > 127)
 {
   valuecmd = valuecmd - 128;
   digitalWrite (outA, HIGH);
 }
 else
 {
   digitalWrite (outA, LOW);
 }
}

void output64 ()
{
 if (valuecmd > 637)
 {
   valuecmd = valuecmd - 64;
   digitalWrite (outB, HIGH);
 }
 else
 {
   digitalWrite (outB, LOW);
 }
}

void output32 ()
{
 if (valuecmd > 31)
 {
   valuecmd = valuecmd - 32;
   digitalWrite (outC, HIGH);
 }
 else
 {
   digitalWrite (outC, LOW);
 }  
}

void output16 ()
{
 if (valuecmd > 15)
 {
   valuecmd = valuecmd - 16;
   digitalWrite (outD, HIGH);
 }
 else
 {
   digitalWrite (outD, LOW);
 }
}

void output8 ()
{
 if (valuecmd > 7)
 {
   valuecmd = valuecmd - 8;
   digitalWrite (outE, HIGH);
 }
 else
 {
   digitalWrite (outE, LOW);
 }
}

void output4 ()
{
 if (valuecmd > 3)
 {
   valuecmd = valuecmd - 4;
   digitalWrite (outF, HIGH);
 }
 else
 {
   digitalWrite (outF, LOW);
 }
}

void output2 ()
{
 if (valuecmd > 1)
 {
   valuecmd = valuecmd - 2;
   digitalWrite (outF, HIGH);
 }
 else
 {
   digitalWrite (outF, LOW);
 }
}

void output1 ()
{
 if (valuecmd > 0)
 {
   analogWrite (redpin, red);
   analogWrite (greenpin, green);
   analogWrite (bluepin, blue);
   analogWrite (amberpin, amber);
   analogWrite (pinkpin, pink);
 }
 else
 {
   analogWrite (redpin, 0);
   analogWrite (greenpin, 0);
   analogWrite (bluepin, 0);
   analogWrite (amberpin, 0);
   analogWrite (pinkpin, 0);
 }
}

void printserial ()
{
 Serial.print (red);
 Serial.print (",");
 Serial.print (green);
 Serial.print (",");
 Serial.print (blue);
 Serial.print (",");
 Serial.print (amber);
 Serial.print (",");
 Serial.print (pink);
 Serial.print (",");
 Serial.print (valuecmd);
 Serial.print ("\n");
}

   

Any ideas what I am doing wrong here. Basically I am trying to move 6 values in a comma delimited format over the serial ports from one arduino to another.

Thanks for any input

Jim


Moderator edit: [code] [/code] tags added.

Grumpy_Mike

Quote
Any ideas what I am doing wrong here.

Yes you are not using the correct code tabs.
READ THIS:-
http://arduino.cc/forum/index.php/topic,97455.0.html

MikMo

Did you remember to connect the grounds of the two Arduino boards ?

billroy

If you are using two standard Arduinos with on-board USB hardware, it does not work to connect an additional TX input to the RX pin.  It fights with the existing connection to the USB hardware on that pin.

You can use another pin as a serial input on the receiving unit with the SoftSerial library.  Search the forum and playground for examples.

Good luck with your project,

-br

jlefevre1

Yes grounds are connected.  The boards I am using do not have USB on them.


billroy

#5
Jan 01, 2013, 10:37 pm Last Edit: Jan 01, 2013, 10:41 pm by billroy Reason: 1
Does the receiver work as expected when you type the expected input to it in the serial monitor while it's connected to a PC?

-br

Edit: an additional question: doesn't parseInt() leave the delimiter as the next available character in the serial buffer?  If so, wouldn't you have to eat the comma with Serial.read() after parseInt() to move on past the comma?

jlefevre1

Yes it does. Apparently the voltage levels on the pins 0 and 1 do not work for ttl. Rs232. When I jumper the tx pin on the header going to the Macintosh USB cable header on the arduino board to the Rex pin on the programming port on the reciever board the signal get there okay.

What kind of circuit do I have to use to get the pins 0 and 1 to have the correct voltage levels to communicate.


billroy

If the two arduinos are running at the same +VCC, a direct connection from TX on Arduino 1 to Rx on Arduino 2 should "just work" for 9600 baud serial, provided nothing else is connected to the either pin in question, and the grounds are good.

A puzzler...

-br

retrolefty


If you are using two standard Arduinos with on-board USB hardware, it does not work to connect an additional TX input to the RX pin.  It fights with the existing connection to the USB hardware on that pin.

You can use another pin as a serial input on the receiving unit with the SoftSerial library.  Search the forum and playground for examples.

Good luck with your project,

-br



No, the arduino boards uses two 1K ohm 'isolation' resistors on the send and rec lines from pins 0 and 1 to the USB serial converter chip. As long as you are not receiving data from the PC via USB (or better yet not plugged into USB) you can directly connect pins 0 and 1 to another arduino boards pin 1 and 0 and exchange serial data with each other.

Lefty

billroy

Thanks, Lefty.  Happy to know that. 

-br

Grumpy_Mike

Code: [Select]
while (Serial.available() > 0)
  {
    red = Serial.parseInt();
    green = Serial.parseInt();
    blue = Serial.parseInt();
    amber = Serial.parseInt();
    pink = Serial.parseInt();

So if there is only one byte in the serial buffer you happily go off and read lots of data from the serial port. Spot your error?

jlefevre1

No. I don't even understand this. I am using an exact copy of a way to read a serial buffer from an example on the auduino web site.

Arrch


No. I don't even understand this. I am using an exact copy of a way to read a serial buffer from an example on the auduino web site.


Which example?

Grumpy_Mike

Exactly because either you are missunderstanding it or it is wrong.
Serial data takes a long time to arrive, just because one byte has arrived does not mean that all the bytes have arrived. FACT.

PaulS

Change your sender:
void serialoutput ()
{
  Serial.print("<");
  Serial.print(red);
  Serial.print(",");
  Serial.print(green);
  Serial.print(",");
  Serial.print(blue);
  Serial.print(",");
  Serial.print(amber);
  Serial.print(",");
  Serial.print(pink);
  Serial.print(",");
  Serial.print(valuecmd);
  Serial.println(">");
}

Use code like this on the receiver:
                                                                                                                               
Code: [Select]

#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", use strtok() and atoi() to get the numbers from inData.

Go Up