0
votes

I'm doing a communication system with 2 Arduinos Leonardo. To transmit I had to convert a String in bits and send them through a PWM pin. The question is, how can I do the receiver?

My idea is to receive data in a digital pin, so I can convert LOW/HIGH to '0' and '1', put 8 bits in a string, convert to char and then I have my final String.

Some info: - I'm using PWM because I want to implement FSK modulation, sending bit by bit;- - I have some hardware to get communication system working; - My physical channel for communication is Serial (via Arduino pins); - In the receiver hardware will provide me TTL signals, so I will have 0V/5V. That's why I think digitalRead() should work; - Data is sent via PWM and received in two level signals; - I'll have to do some protocol to send/receive data, but still don't know how...

This is the code I wrote:

void setup() {
  Serial.begin(9600);
  while (!Serial) {;}
}
void loop() {
  char bitPosition = 0;
  String inMessage = 0;
  while (Serial.available() > 0) {
    char outChar;
    boolean digValue = digitalRead(inPin);
    for(bitPosition = 7; bitPosition >= 0;  ){
      if(digValue == LOW){
        bitWrite(outChar, bitPosition, 0);
      }
      if(digValue == HIGH){
        bitWrite(outChar, bitPosition, 1);
      }
    bitPosition--;
    }
  inMessage.concat(outChar);
  if(inMessage != 0){
    Serial.println("Received:  " + inMessage);
  }
 }
}

I want this code to transform HIGH/LOW states from digitalRead in a string of characters. 0/1 -> char (outChar) -> String (inMessage). I don't know if i'm not thinking right. Suggestions? Should I have to do something about baudrate to receive/transmit data? How?

Any help/suggestion would be appreciated.

2
So you want to group bits by 8 and make a char out of them?user529758
yes, something like 8bits -> char -> Stringmafap
then use bit shifting and bitwise logic operators.user529758
I tryed to understand that. Can you explain me please how it works in my situation?mafap
unsigned char c = 0; for (i = 0; i < 8; i++) { c <<= 1; c += bit; }user529758

2 Answers

0
votes

Look at how SoftwareSerial is implemented in Arduino for a clue as to how to get this done.

http://arduino.cc/en/Reference/SoftwareSerial

"The SoftwareSerial library has been developed to allow serial communication on other digital pins of the Arduino, using software to replicate the functionality (hence the name "SoftwareSerial")."

0
votes

Well, your problem if far more complicated that you believe. I have read your similar (identical?) question posted on EE.SE.

It seams that you haven't catch a fundamental thing about your issue. It is not a software problem. If I summarize your issue, globally.

You want to send data from arduino A to arduino B.

You plan to generate a FSK signal using a PWM pin of arduino A.

first comment: How ? Look-up table of a sine wave where the pointer moves according to the phase modulation? This seems easy. So let's believe this it the way you choose. (here you say it is a logic signal, but on EE.SE you say it's a wireless signal)

Now you have a signal at XX kHz (we don't know the XX value. But accordint to the Xtal speed of your Arduino, I can't be much greater than ~500Khz)

On arduino B, you have to solve the problem of clock recovery because on arduino B you don't have access to the clock of arduino A. This means that you can't compare the signal you are getting with a reference clock. When you analyse the received signal you get a certain phase relative to your local clock. But because you cannot know the reference phase of ardunio A, you cannot say if the receive bit is -90° or +90°. Thus you can't assign a 0 or 1 to the received signal.

This is not a trivial problem. According to your answer to the question and comments here or on EE.SE, you are trying to solve implementation problem of an algorithm that is probably not working. Your should first try to find a suited algorithm that you may simulate using MATLAB (or the free OCTAVE), make sure it is working, and next try to implement it on embedded hardware.

You receive a lot of answer on EE.SE about what do you want to do. It's because it makes no sens to solve an issue about implementing something that looks false to an expert.

Thus, if you want meaningful answers. Please spend more time explaining us what and how, conceptually, you plan to solve your problem.

Add information about how your think your FSK works. How do you plan to use PWM to do FSK, this in not trivial. And how you plan, conceptually to detect that a received symbol is a 1 or a 0.

As it is, my answer is : Event if we can solve you copy bit issue, it won't solve your problem.