Communication between Raspberry pi and Arduino via GPIO

Hi, I want to create a communication between Raspberry pi and arduino by using GPIO pin, but I have problem with my code. So RPi act as a sender and arduino will be the receivier, for example : if RPi pin number 4 is HIGH/LOW, consequently so does the Arduino’s pin 4, also arduino will print ‘1’ if it is high or ‘0’ if it is low.
const int piPin1 = 4;
const int piPin2 = 6;
int PiState = 0;

void setup() {
pinMode(piPin1, INPUT);
pinMode(piPin2, INPUT);
}

void loop() {
PiState = digitalRead(PiState);

if (PiState == 4 - ‘0’) {
digitalWrite(piPin1 , !digitalRead(piPin1));
}

if(PiState == 6 - ‘0’) {
digitalWrite(piPin2, !digitalRead(piPin2));
}
return 0;
}
Here’s the code that I’m curently working on.
Also I’ve 2 channel relay shield attatched on my arduino , how do I connect them to RPi by using cable jumper.
Any help or advice would be greatly appreciated.
Thankyou

The result of a digitalRead() call is either HIGH or LOW and not a numeric character as your code

if (PiState == 4 - '0') {

assumes .

stowite:
The result of a digitalRead() call is either HIGH or LOW and not a numeric character as your code

if (PiState == 4 - '0') {

assumes .

So it should be
if (PiState == HIGH) {
digitalWrite(piPin1 , !digitalRead(piPin1));
}
else if{
digitalWrite(piPin2, !digitalRead(piPin2));
}

It is not obvious to me what you are trying to do. If your intent is to toggle either piPin1 or piPin2, depending on the state of the piState pin, as rapidly as possible then that code should do the job.

yes I need to toogle either piPin1 and piPin2 or toggle both of them
i change the code into

const int piPin1 = 4;
const int piPin2 = 5;
int PiState = 0;

void setup() {
 pinMode(piPin1, INPUT);
 pinMode(piPin2, INPUT);
}

void loop() {
 PiState = digitalRead(PiState);
 
 if (PiState == HIGH) {
  digitalRead(piPin1);
  digitalRead(piPin2);
 }

 if (PiState == LOW){
  digitalRead(piPin1);
  digitalRead(piPin2);
 }
 return 0;
}

Im totally new into microcontroller , some advice or help would be appriciated

I don't understand your latest code. This

PiState = digitalRead(PiState);

is nonsense since will read into PiState from pin 0 or pin 1 since PiState is always 0 or 1! It needs to read from the pin that is connected to the PI.

This

 if (PiState == HIGH) {
  digitalRead(piPin1);
  digitalRead(piPin2);
 }

is nonsense since it checks for PiState being HIGH, reads piPin1 and discards the value read then reads piPin2 and discards the value read. Similarly the other block of code.

Thankyou for your advice

PiState = digitalRead(piPin1);
 if (PiState == HIGH) {
  digitalWrite(piPin1, HIGH);
  }
 else {
  digitalWrite(piPin1 , LOW);
 }

 PiState = digitalRead(piPin2);
 if (PiState == HIGH) {
  digitalWrite(piPin2, HIGH);
  }
 else {
  digitalWrite(piPin2 , LOW);
 }

I've update my code, tell me if i still missing something

You're reading and writing to the same pin. Is that pin an input or an output? It's hard for me to see how it is going to be both in this code.

Also posted at pins - Arduino & RPi communication via GPIO - Arduino Stack Exchange

OP - if you're going to sprinkle the same nonsense question all over the Internet, then please at least be considerate enough to provide links to the others so nobody wastes their time giving answers here that you already have there.

Delta_G:
You're reading and writing to the same pin. Is that pin an input or an output? It's hard for me to see how it is going to be both in this code.

the pin is an input, I'm terribly sorry for my code also I'm sorry that I've made another post at pins - Arduino & RPi communication via GPIO - Arduino Stack Exchange and talking nonsense all the time

credist:
the pin is an input

So why are you calling digitalWrite to it? Do you know what you are doing with the pull-up resistors or are you confused about what an input does?

so It's a digitalRead, impretty much confuse , my programming skill is below average

It's not about your programming skills. Just think about it. What is an input? Do you read inputs or write to them? Even with zero coding skills you gotta be able to see that one.