Blinking LEDs controlled by Arduino

arduino

The picture above is the Arduino part of functions.

On the right end of breadboard, I use a wire simply represent a protection pushbutton to control the whole loop. Only if the wire is connected the LEDs can blink later.

Then leave pin 8 as input from Raspberry Pi’s GPIO. Once the pin get a HIGH impulse, LED blinking loop will be executed twice.

The code for Arduino is as follows:

const int buttonPin = 7; 
const int ledPin = 2; 
const int rpiPin = 8; //pin8 is connected to Raspberry Pi GPIO 25

int buttonState = 0; //set initial variables to 0
int rpiState = 0;
int i = 4; //set i to any contant that larger than 2, just not to execute the led blinking part.

void setup() {
   Serial.begin(9600); //open serial port, set data rate to 9600 bps
   pinMode(ledPin, OUTPUT); 
   pinMode(buttonPin, INPUT); 
   pinMode(rpiPin,INPUT) ;
}

void loop(){
   buttonState = digitalRead(buttonPin);
   rpiState = digitalRead(rpiPin);
   Serial.print(buttonState);
   Serial.print(rpiState);
   if (rpiState == HIGH ){i=1;} 
   if (buttonState == HIGH) { //just loop twice
   while (i<=2) { 
  digitalWrite(ledPin, HIGH);
  delay(100);
  digitalWrite(ledPin, LOW);
  delay(200); 
  digitalWrite(ledPin, HIGH);
  delay(400);
  digitalWrite(ledPin, LOW);
  delay(300); 
  digitalWrite(ledPin, HIGH);
  delay(200);
  digitalWrite(ledPin, LOW);
  delay(200); 
  i++;} 

else {
  digitalWrite(ledPin, LOW); 
  i=1; 
}
}