Read and Write in same PIN as bidirection

Wondering, will works with write and read with same a pin such as bi-direction in the code? I am trying to make read and write the 4 bit data without use API or I2c.

example:

int D0 = 0;
int D1 = 0;
int D2 = 0;
int D3 = 0;

void setup() {

Serial.begin(9600);

pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);

pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
}

void loop() {
 write_data(1,0,1,1);
 delay(5000);
 Serial.print(read_data());
}

void read_data() {
  D0=digitalRead(2);
  D1=digitalRead(3);
  D2=digitalRead(4);
  D3=digitalRead(5);
  return D0, D1, D2, D3;
}

void write_data(int w0,int w1,int w2,int w3){
  digitalWrite(2,w0);
  digitalWrite(3,w1);
  digitalWrite(4,w2);
  digitalWrite(5,w3);
}

thanks

More information is required.

bool D0 = 0;
bool D1 = 0;
bool D2 = 0;
bool D3 = 0;

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

void loop() {
  write_data(1, 0, 1, 1);
  delay(5000);
  read_data();
  Serial.println(D0);
  Serial.println(D1);
  Serial.println(D2);
  Serial.println(D3);
  delay(5000);
}

void read_data() {
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  pinMode(4, INPUT);
  pinMode(5, INPUT);
  D0 = digitalRead(2);
  D1 = digitalRead(3);
  D2 = digitalRead(4);
  D3 = digitalRead(5);
}

void write_data(bool w0, bool w1, bool w2, bool w3) {
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  digitalWrite(2, w0);
  digitalWrite(3, w1);
  digitalWrite(4, w2);
  digitalWrite(5, w3);
}
1 Like

That's one!! thanks!!!

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