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.
char
out of them? – user529758unsigned char c = 0; for (i = 0; i < 8; i++) { c <<= 1; c += bit; }
– user529758