Communications Project Serial Data

Hi, I'm working on developing communications between two arduinos. I have been able to implement the audio portion of my project. Where I communicate via a microphone through the serial connection between the two boards via serial. However, I'm also trying to send a text message via serial. I have attempted to combine the two forms of data in my TX code. In my receive code I'm trying to parse the two bytes of information. One is the message in text format and the other is the value from 0 to 255 to send to a DAC which out puts to a speaker. I have attached my code for both TX and RCV. I want to use some sort of header to identify each type of data so I can parse the audio data to PORTA on my Mega and then the other byte of data to the serial terminal.

My Transmitter code :

#include <stdlib.h>


int inByte;
int incomingAudio;
//int sensorValue = 0;
int commValue = 0;
//int i = 0;
//int z = 0;
//int oValue[3] = {0, 0, 0};

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

void loop(){
    

incomingAudio = analogRead(A0); // read voltage at pin A0
incomingAudio = (incomingAudio+1)/4 - 1;  //scale from 10 bits to 8 bit sound

if (incomingAudio<0){ //loop to fix negitive numbers
incomingAudio = 0;}

Serial1.write(incomingAudio);
delay(1);

if (Serial.available() > 0){
  
  inByte = Serial.read(); // this reads a letter h being sent from a third Arduino to my Transmitter board
  
  Serial1.write('~');        // this is my header
  Serial1.write(inByte);} // this is the H being sent to the receiver board.
  
  }

This is my Receiver Code:

int incomingByte = 0;//for incoming serial data

int analogByte = 0;
int commByte = 0;
int i = 0;
int iValue[3];

void setup(){
    Serial.begin(9600);
    Serial1.begin(9600);  //opens erial port and sets to 9600baud
    
     for (byte i=22;i<30;i++){
    pinMode(i,OUTPUT);//set digital pins 0-7 as outputs (DAC)
     }
    
}
void loop(){
  while (Serial1.available()){
   
  incomingByte = Serial1.read();
     
   
   if(incomingByte == 126){          //if it sees the header byte do what follows
     
     incomingByte = Serial1.read();// use byte after the header byte
     
     Serial.print(incomingByte);// print
     
     
     
     
     //delay(1);
   }                                  // if the incommingByte isn't the header just send the following bytes to the PORTA for DAC
   else {
     PORTA = incomingByte;  
   }
     
}
  
}

I have success with audio passing through. Just can't get my message to come thru. Any help would be greatly appreciated. I believe my issue lies in the fact that I'm not really sure if I'm sending strings or ASCII DEC values. Its a bit confusing and most of what I've read for parsing serial data is done for strings. I don't think my data is strings. I could be wrong. Thanks!

Why are you using such a slow baud rate. If both devices are arduino, you could send at a much higher speed and therefore reduce disruption to your audio. A baud rate of 576000 would be more appropriate but you may even go to 1,000,000

You are also loosing some of the audio by using a single write. (analog read will be a number between 0 and 1023, whereas Serial1.write will only write one BYTE of data. You'll be loosing the upper two bits.

Just here you see the header, then IMMEDIATELY attempt to read the following byte. In reality it may not have been recieved yet.

Suggest you put in a while (!Serial1.available()); Just before the read statement.

  if(incomingByte == 126){          //if it sees the header byte do what follows

  while(!Serial1.available()); //wait for following byte to become available.
    
     incomingByte = Serial1.read();// use byte after the header byte

Although, personally I'd rather restructure the routine so that it doesn't end up in a potentialy frozen state.

Thanks for the quick reply! I'll see what I can do with the baud rate see how that goes. Also what does the ! in front of the Serial.available do? I've seen it else where but I don't understand what thats doing. I'm not the most experienced coder. I'll try out your suggestion see how it works out. Thanks again!

So we increased the baud rate for the serial connection. It works fine via serial connection but we are using a fiber optic kit connect the two boards and that is limited via the transmission speed. For some reason the kit doesn't tolerate high speeds.

cmeff1:
Also what does the ! in front of the Serial.available do?

It's standard C for NOT

So while(!Serial1.available()); (note also the semicolon on the end) means, just wait here WHILE there is NOT serial available. Without the ! it would be wait while there IS serial available (not what we want)

It works fine via serial connection but we are using a fiber optic kit connect the two boards and that is limited via the transmission speed.

This surprises me. Perhaps the hardware would need to be reconfigured for the higher baud rate. The fibre itself is certainly not a limiting factor.

Very simple serial code for sending text from one arduino to another,

//zoomkat 3-5-12 simple delimited ',' string tx/rx 
//from serial port input (via serial monitor)
//and print result out serial port
//Connect the sending arduino rx pin to the receiving arduino rx pin. 
//Connect the arduino grounds together. 
//What is sent to the tx arduino is received on the rx arduino.
//Open serial monitor on both arduinos to test

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 == ',') {
      if (readString.length() >0) {
        Serial.print(readString); //prints string to serial port out
        Serial.println(','); //prints delimiting ","
        //do stuff with the captured readString 
        readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

KenF: Thanks again, I believe the fiber kit has an IC that has some gates in it that my be the limiting factor, switching speeds and such. I'll have to read more about it. I'll have to try to implement the code you suggested.

zoomkat: Thanks for the suggestion. I have been able to send the text message and the audio separately its when I send them both and on the receiver I want to parse them into there respective outputs. The text message to the serial command window and the audio to PORTA on my Mega for the DAC that we are using to handle converting the digtal data to analog for speakers.

Thanks guys any more suggestions I'm open, in the mean time I'll have a go with your suggestion KenF

KenF this is the code I dropped in. I get just get garbage out when I look at my serial terminal on my receiving arduino. bunch of the y's with dots over them if i use serial.write and if i use serial.print just a bunch of garbage.

void loop(){
  while (Serial1.available()){
   delay(1);
  incomingByte = Serial1.read();
  
  if(incomingByte == 126){          //if it sees the header byte do what follows

  while(!Serial1.available()); //wait for following byte to become available.
    
     incomingByte = Serial1.read();// use byte after the header byte
    
      Serial.print(incomingByte);
  }  
  else{
  //Serial.println(incomingByte);
 PORTA = incomingByte;
  
  }
}
}

Figured out that my serial terminal didn't have the right speed set. Once the speed was set I was able to see numerical values coming through. It appears my transmit code isn't working correctly. Its not sending the ASCII dec value for the ~ followed by the byte right after it.

Perahps you could change
Serial1.write('~'); // this is my header
To
Serial1.write(126); // this is my header

KenF: I've played with that a bit, at one point we where able to get the '~' to come through but it wouldnt send the following serial.write(inByte) as seen in the TX code. I think I'm going to have verify I'm actually receiving the inByte. If inByte had nothing in it I would get nothing to serial.write.....Any was thanks for your help, I'm going to have a go at it again in a couple of days. See what I can make happen. Have some finals to attend to in the mean time.

KenF, above you mention you would rewrite the routine to prevent freezing, why would it freeze and how would you look to prevent that from occurring?

Might be worth trying something like this to help debug the situation.

int incomingByte = 0;//for incoming serial data
int analogByte = 0;
int commByte = 0;
int i = 0;
int iValue[3];

void setup(){
    Serial.begin(9600);
    Serial1.begin(9600);  //opens erial port and sets to 9600baud
    
     for (byte i=22;i<30;i++){
    pinMode(i,OUTPUT);//set digital pins 0-7 as outputs (DAC)
     }
    
}

byte currentMode=0;

void loop(){
  if (Serial1.available())
    {
    incomingByte = Serial1.read();
    switch(currentMode)
      {case 0://
         if(incomingByte == 126)
            currentMode = 1;
         else
          {
          PORTA = incomingByte;
          Serial.print("Just sent ");
          Serial.print(incomingByte,DEC);
          Serial.println(" to PORT A ");
          }
      break;
  
      case 1://digital mode
           Serial.print("Recieved ");
           Serial.print(incomingByte,DEC);// print
           Serial.print(" (char code of '");
           Serial.print((char) incomingByte);
           Serial.println("' )");
           currentMode=0;
           break;      
      }  
   }
}