Category Archives: all

Handheld Infra Red Camera code

Motion detect code:

#!/usr/bin/env python
import RPi.GPIO as GPIO
import time

# Set pins to BOARD
GPIO.setmode(GPIO.BOARD)

#PIR setup
#set up PIR pin
PIR_PIN = 15
#set PIR pin as input
GPIO.setup(PIR_PIN, GPIO.IN)

#relay setup
# Set relay pin
GPIO_LIGHT_ON = 11
# Set relay pin as output
GPIO.setup(GPIO_LIGHT_ON,GPIO.OUT)

def MOTION(PIR_PIN):
				GPIO.output (GPIO_LIGHT_ON,1)
				print "light on"
				time.sleep(10)
				while GPIO.input(PIR_PIN)==1:
					time.sleep(2)
					GPIO.output (GPIO_LIGHT_ON,0) 

print "PIR Module test press Ctrl+C to exit"
time.sleep(2)
print "ready"

try:
	GPIO.add_event_detect(PIR_PIN, GPIO.RISING, callback=MOTION)
	while 1:
		time.sleep(100)
except KeyboardInterrupt:
				print "quit"
				GPIO.cleanup()

Main button control code:

#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
import os
import sys
from time import sleep
import subprocess as sp

# Set pins to BOARD
GPIO.setmode(GPIO.BOARD)

#set up BUTTON 1 - RED
BUTTON_RED_PIN = 37
#set PIR pin as input
GPIO.setup(BUTTON_RED_PIN, GPIO.IN)

#set up BUTTON 2 - GREEN
BUTTON_GREEN_PIN = 31
#set PIR pin as input
GPIO.setup(BUTTON_GREEN_PIN, GPIO.IN)

#set up BUTTON 3 - YELLOW
BUTTON_YELLOW_PIN = 33
#set PIR pin as input
GPIO.setup(BUTTON_YELLOW_PIN, GPIO.IN)

#set up BUTTON 4 - BLUE
BUTTON_BLUE_PIN = 32
#set PIR pin as input
GPIO.setup(BUTTON_BLUE_PIN, GPIO.IN)

#set up BUTTON 5 - BLACK
BUTTON_BLACK_PIN = 36
#set PIR pin as input
GPIO.setup(BUTTON_BLACK_PIN, GPIO.IN)

#set up pin 12 for PWM (screen control)
GPIO.setup(12, GPIO.OUT)
#GPIO.PWM(12, 1000)
pwm = GPIO.PWM(12, 1000)
pwm.start(50)

# set record states
rkd_state = 0
rkd_state_blue = 0
rkd_state_black = 0
rkd_state_green = 0

# set filter pins
Motor1A = 16
Motor1B = 13
Motor1E = 7
GPIO.setup(Motor1A,GPIO.OUT)
GPIO.setup(Motor1B,GPIO.OUT)
GPIO.setup(Motor1E,GPIO.OUT)	

# set up the variables for running motion detect
extProc = 1

def BUTTON_RED(BUTTON_RED_PIN):
				os.system('sudo poweroff')

def BUTTON_GREEN(BUTTON_GREEN_PIN):
				global rkd_state_green
				global extProc
				if rkd_state_green == 0:
					print "Green on"
					os.system('echo "motion_enable on" > /home/pi/pikrellcam/www/FIFO')
					extProc = sp.Popen(['python','motion.py']) # runs motion.py 
					status = sp.Popen.poll(extProc) # status should be 'None'
					rkd_state_green = 1
				elif rkd_state_green == 1:
					print "Green off"
					sp.Popen.terminate(extProc) # closes the process
					status = sp.Popen.poll(extProc) # status should now be something other than 'None' ('1' in my testing)
					os.system('echo "motion_enable off" > /home/pi/pikrellcam/www/FIFO')
					rkd_state_green = 0	
				
def BUTTON_YELLOW(BUTTON_YELLOW_PIN):
				global rkd_state
				if rkd_state == 0:
					os.system('echo "record on" > /home/pi/pikrellcam/www/FIFO')
					rkd_state = 1
				elif rkd_state == 1:
					os.system('echo "record off" > /home/pi/pikrellcam/www/FIFO')
					rkd_state = 0
					
def BUTTON_BLUE(BUTTON_BLUE_PIN):
				global rkd_state_blue
				if rkd_state_blue == 0:
					pwm.ChangeDutyCycle(0)
					rkd_state_blue = 1
				elif rkd_state_blue == 1:
					pwm.ChangeDutyCycle(100)
					rkd_state_blue = 0

def BUTTON_BLACK(BUTTON_BLACK_PIN):
				global rkd_state_black
				if rkd_state_black == 0:
					GPIO.output(Motor1A,GPIO.LOW)
					GPIO.output(Motor1B,GPIO.HIGH)
					GPIO.output(Motor1E,GPIO.HIGH)
					sleep(0.1)
					GPIO.output(Motor1E,GPIO.LOW)
					rkd_state_black = 1
				elif rkd_state_black == 1:
					GPIO.output(Motor1A,GPIO.HIGH)
					GPIO.output(Motor1B,GPIO.LOW)
					GPIO.output(Motor1E,GPIO.HIGH)
					sleep(0.1)
					GPIO.output(Motor1E,GPIO.LOW)
					rkd_state_black = 0		
try:
				GPIO.add_event_detect(BUTTON_RED_PIN, GPIO.RISING, callback=BUTTON_RED, bouncetime=300)
				GPIO.add_event_detect(BUTTON_GREEN_PIN, GPIO.RISING, callback=BUTTON_GREEN, bouncetime=300)
				GPIO.add_event_detect(BUTTON_YELLOW_PIN, GPIO.RISING, callback=BUTTON_YELLOW, bouncetime=300)
				GPIO.add_event_detect(BUTTON_BLUE_PIN, GPIO.RISING, callback=BUTTON_BLUE, bouncetime=300)
				GPIO.add_event_detect(BUTTON_BLACK_PIN, GPIO.RISING, callback=BUTTON_BLACK, bouncetime=300)
				while 1:
					time.sleep(100)
except KeyboardInterrupt:
				print "quit"
				GPIO.cleanup()

Elite dangerous controller

Whilst ordering some new arts for a couple of projects, I came across the zero delay arcade by Reyann.

board

It is intended for raspberry pi etc. controls for creating arcade consoles and appears to be USB plug and play so I thought I’d give it a go and make some additional controls for elite:dangerous.

dscn4619

This prototype has been lovingly crafted for the cardboard box that my delivery arrived in. No setup required and it just plugged in and worked.

The kit comes with the wiring loom and usb cable so it’s just a case of slipping the buttons on to the wires and off you go. It connects perfectly with the arcade buttons but not so well with the smaller tactile ones which required some structural blu-tak – a couple of mins soldering would fix it but these buttons are only temporary.

dscn4610

Windows sees it as a generic controller and E:D responds in a similar way, so there is no issue in assigning the buttons to the massive list of controls available (copy and save your key binds though. They’re very fickle).

The arcade buttons themselves are made by Sanwa and are spot on arcade quality – you can roll your finger on them like a real arcade machine and they feel like they could go on forever.

The joystick too is a Sanwa item and just oozes quality.

The larger buttons are (I think) from Adafruit. They are a bit more clicky than the arcade ones but very tactile. The 100mm HAL special in the middle is impossible not to touch and brings out the father dougal in everyone.

lit

They also light up, I’ve not tried this yet but it looks to be pretty straightforward and some of the online pictures make them look very effective (e.g. the HAL 9000 replica on adafruit )

dscn4616

For a prototype it works perfectly and the next step is to work on the case and assemble the required buttons. This was pretty much thrown together to see if the board worked however having played for a bit today the HAL9000 / pacman mashup has really grown on me so I think I’ll keep it!

dscn4617

In summary:

Controller board

By Reyann. mod my pi link: https://www.modmypi.com/raspberry-pi/arcade/arcade-controllers/zero-delay-arcade-usb-encoder-and-wire-set/?search=arcade

Arcade buttons

 By Sanwa – mod my pi link : https://www.modmypi.com/raspberry-pi/arcade/arcade-buttons/official-sanwa-arcade-button-black/?search=sanwa%20arcade%20buttons

Arcade stick

By Sanwa Mod my i link : https://www.modmypi.com/raspberry-pi/arcade/arcade-joysticks/official-sanwa-8-way-arcade-joystick-jlf-tp-8yt/?search=sanwa

Illuminated buttons.

By Adafruit. Mod my pi link : https://www.modmypi.com/electronics/buttons-and-switches/massive-arcade-button-100mm-red/?search=arcade%20buttons

Trailcam – 3. PIR sensor

Other sections

  1. Introduction
  2. Basic setup & Power
  3. PIR sensor
  4. IR lamp
  5. IR filter
  6. Buttons
  7. Completed pics, improvements and parts list
  8. Updates

 

Description

The PIR sensor is a pretty standard piece of kit – I got mine from Tandy however they’re really common and stocked by the usual suspects (adafruit, pimoroni, Modmypi,maplin)

In the first version I had the PIR sensor triggering the recording however for this version I’ve altered it so that it just turns the IR lamp on leaving the motion detection in the software to do its thing..

Install

Camera in window cill mode
PIR at bottom of trailcam

I had hoped that the PIR would “see” through the plastic on the box however it wasn’t happening so I ended up removing the defractor / lens, drilling a hole through the box to push the sensor through then stick the lens on the outside. It does increase the chance of letting water through however there isn’t really much choice and a big chunk of gaffer tape should sort it out.

PIR
Dismantled PIR sensor (main). I cut a hole and pushed the sensor section through it, then fixed the diffuser on the outside (insert)

Control programme

I modified the control program from raspberry pi spy to run the previous version of the trail cam however this version only needs to turn the light on an off, hence why it is a bit of a mess.

EDIT: Revised version here

#!/usr/bin/python
# Original
# By : Raspberry Pi Spy
# Author : Matt Hawkins
# Date : 21/01/2013
# Modification
# By : G
# Date : 24/04/2016


# Import required Python libraries
import RPi.GPIO as GPIO
import time
import subprocess

# Use BCM GPIO references
# instead of physical pin numbers
GPIO.setmode(GPIO.BCM)

# Define PIR GPIO to use on Pi
GPIO_PIR = 22

print "PIR Running (CTRL-C to exit)"

# Set PIR pin as input
GPIO.setup(GPIO_PIR,GPIO.IN) # Echo

Current_State = 0
Previous_State = 0

# Define GPIO for lamp control
GPIO_LIGHT_ON = 17

# Set lamp on GPIO as output
GPIO.setup(GPIO_LIGHT_ON,GPIO.OUT) # Echo

try:

 print "Waiting for PIR to settle ..."

 # Loop until PIR output is 0
 while GPIO.input(GPIO_PIR)==1:
 Current_State = 0 

 print " Ready" 
 
 # Loop until users quits with CTRL-C
 while True :
 
 # Read PIR state
 Current_State = GPIO.input(GPIO_PIR)
 if Current_State==1 and Previous_State==0:
 # PIR is triggered
 print " Motion detected!"
 GPIO.output (GPIO_LIGHT_ON,1)
 print " Light on"
 # Capture a 5 second video
 # print " Record start"
 time.sleep(20)
 # Record previous state
 Previous_State=1
 elif Current_State==0 and Previous_State==1:
 # PIR has returned to ready state
 GPIO.output (GPIO_LIGHT_ON,0) 
 print " Light off" 
 print " Ready"
 Previous_State=0
 
 # Wait for 10 milliseconds
 time.sleep(0.01) 
 
except KeyboardInterrupt:
 print " Quit" 
 GPIO.output (GPIO_LIGHT_ON,0)
 # Reset GPIO settings
 GPIO.cleanup()