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

Raspberry Pi GPIO connected with Arduino

OKAY now I’m back here using Arduino to control LEDs.

I connect GPIO.25 on RPI with Arduino Pin 8. Remember to connect GND to GND to get them synchronised。While RPi is a loop sensing the RF signal input, once it need to blink LED, it generates a Pulse to Arduino. Then Arduino will execute the Blinking LED loop, at the same time RPi is playing sounds stored in its memory initially.

RPIArd

How to use RPI GPIO(General Purpose Input Output) you can find information here.

RaspberryPi-GPIO-Layout

I write a python code named pumpkin.py. Then can use

sudo python pumpkin.py

Below is the code that can perform the functions of sensing RF signals, playing sounds and ask Arduino to blink LEDs at the same time.

I add one more function, use the down button on controller to stop the loop and exit the program.

from evdev import InputDevice
from select import select
import os
import time
import RPi.GPIO as GPIO
dev = InputDevice(‘/dev/input/event0’)
m=1
n=1
o=1
b=1
GPIO.setmode(GPIO.BCM)
GPIO.setup(25,GPIO.OUT)
GPIO.output(25,GPIO.LOW)

def blink():

GPIO.output(25,GPIO.HIGH)
time.sleep(0.1)
GPIO.output(25,GPIO.LOW)
return

while b==1 :
r,w,x=select([dev],[],[])

for event in dev.read():

if event.value == 458827L:

print ‘left’
m=m+1
if m%2 == 0 :
    blink()
    os.system(‘aplay thunder.wav’)
continue

elif event.value == 458830L:

print ‘right’
n=n+1
if n%2 == 0:
    blink()
    os.system(‘aplay laugh.wav’)
continue

elif event.value == 458757L:

print ‘up’
o=o+1
if o%2 == 0 :
    blink()
    os.system(‘aplay monster.wav’)
continue

elif event.value == 458977L or event.value == 458814L or event.value== 458793L:

print ‘down, off’
b=0
continue