Problem with transmitting binary data

Hi guys,

I am currently new to Arduino. I would like to transmit binary data from one digital pin to another and then reprint it with the help from interrupt. However when i run my code the output is "010".

#define TX_PIN   9 
#define RX_PIN   3 

uint8_t data = B01101001;

void isrhandler(){
  int pinState = digitalRead(RX_PIN);
  if(pinState == HIGH){
    Serial.print("1");
  }
  else{
    Serial.print("0");
  }
}

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

void setup() {
  Serial.begin(9600);
  pinMode(RX_PIN, INPUT_PULLUP);
  pinMode(TX_PIN, OUTPUT);
  attachInterrupt(digitalPinToInterrupt(RX_PIN), isrhandler, CHANGE);

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

void loop() {
  // put your main code here, to run repeatedly:
}

Because Serial itself uses interrupts for transmitting, you can't use it in interrupt handlers.

As mentioned above, don't do serial I/O in interrupt routines.

Interrupts simply won't work for this task anyway. What is supposed to happen if you transmit two "1"s or two "0"s in a row?

I just want to be able to get the same data on the receiveing pin.

That is not the way to do it.

To send 8 bits of data, use Serial.write and Serial.available + Serial.receive instead (either hardware or software serial, the latter works with any digital pins).

why do you not use digitalWrite() directly in the handler?

void isrhandler(){
  int pinState = digitalRead(RX_PIN);
  digitalWrite(TX_PIN,pinState);
}

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