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;
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
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.
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.
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.