Problem with receiving binary data

Hi guys,

I have a problem with receiving data. In the code I am trying to send binary data to digital pin thorugh which I want to receive and store them. This setup is on Arduino UNO where the two pins are connected together. I want to store them somehow through the pin change interrupt. I know that there have to be some kind of delay to know if there are two or more "1s" or "0s". I measured the high pulse width before and I got two different times when I tried it with ISR (16µs)and without ISR(8µs) .

Thank you in advance.

#define TxPin 9
#define RxPin 3

uint8_t data = B10110010;
String str, string;

void sendSignal(String string){
  for(int i=0;i<8;i++){
    if(string[i]==0){
      digitalWrite(TxPin, LOW);
    }
    else{
      digitalWrite(TxPin, HIGH);
    }
  }
}

void isrhandler(){
  int pinState, state;
  pinState = digitalRead(RxPin);
  if(pinState == HIGH || pinState == LOW){
    delayMicroseconds(16);
    state = digitalRead(RxPin);
    if(state == HIGH){
      str = str + 1;
    }else{
      str = str + 0;
    }
  }
}

void setup() {
  Serial.begin(9600);
  pinMode(TxPin, OUTPUT); 
  pinMode(RxPin, INPUT_PULLUP);

  attachInterrupt(digitalPinToInterrupt(RxPin), isrhandler, CHANGE);

  string = String(data, BIN);
  sendSignal(string);

  Serial.print(str);
}

void loop() {
  
}

Why would you convert a byte to String just to get the bits? if(string[i]==0) should probably be if(string[i]=='0'). Try to use bitRead instead:

for (int i = 0; i < sizeof(data); i++)
  digitalWrite(TxPin, bitRead(data, i));

What other values could pinState be?

1 Like

Thank you, I changed it.

so if the data is all 1s or all 0s, you might only get one interrupt

it might help to understand conventional serial transmission

a UART sends a ~byte of data, prefixed by a start bit and suffixed with one or 2 stops bits.

the start bit is the opposite of the idle line value so that there is a well defined change to indicate when a transmission begins.

bit times are well defined (e.g. 9600). bit timing is determined using a a timer

each bit is sampled one or more times (possibly voted to determine its state. if a single sample is used, the first delay based on the beginning of the start bit is 1.5 bit periods, and 1 bit period after that

stop bits are opposite the start bit state and guarantee that there is a change when start occurs and guarantees some amount of time between transissions

the # of bits transmitted is limited to tolerate ~2% overspeed and underspeed by both TX and RX.

this approach only relies on recognizing the change in state when the start bit occurs and uses timing to detect each bit.

Ok guys thank you all for your advice. I did it a little differently but it is done.Here is the code

#define TxPin 9
#define RxPin 3
#define ClockPin 2

uint8_t data = B10001010;
String str, string;

void sendSignal(uint8_t data){
  for(int i=0;i<8;i++){
    digitalWrite(TxPin, bitRead(data,i));
    digitalWrite(ClockPin, HIGH);
    digitalWrite(ClockPin, LOW);
  }
}

void isrhandler1(){
  int pinState;
  pinState = digitalRead(RxPin);
  if(pinState==HIGH){
    str = str + "1";
  }else{
    str = str + "0";
  }
}

void setup() {
  Serial.begin(9600);
  pinMode(TxPin, OUTPUT); 
  pinMode(RxPin, INPUT_PULLUP);
  pinMode(ClockPin, OUTPUT);

  attachInterrupt(digitalPinToInterrupt(ClockPin), isrhandler1, FALLING);

  noInterrupts();
  digitalWrite(ClockPin, LOW);
  interrupts();

  sendSignal(data);

  Serial.println(str);
}

void loop() {
  
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.