2

I'm working on a project for my Raspberry Pi. I have stumbled upon a problem with creating interrupts through a button on my breadboard. I have bought a PiCamera and I intend to make it so that whenever I push the applied button, the PiCamera will take a picture. For now, I'm just trying to make a message show up on my console whenever I would push the button such as: "button pressed" to make sure the interrupt works. I've tried it with my code but it doesn't seem to work. I'm pretty much lost ideas on how I could possibly solve this issue. Help is very appreciated!

I have my code down here.

I have put the button in the middle of the breadboard, connecting both the top layer and the bottom layer. I've connected one side with ground and the other one with GPIO17 (GPIO for Pi2).

Also the code contains another part of my project. Namely I have recently set my components up to measure the amount of light with a light meter. The darker the area, the more LEDs will be turned on. I have three LEDs on my breadbord. This works 100%. Now I'm trying to force in an interrupt by using a button.

My Button interrupt is at the bottom of the code.

My code: http://pastebin.com/Y0bTLE4Y OR...

#!/usr/bin/python

import spidev
import time
import os
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)

GPIO.setup(5, GPIO.OUT)
GPIO.setup(6, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)

# Open SPI bus
spi = spidev.SpiDev()
spi.open(0,0)

# Function to read SPI data from MCP3008 chip
# Channel must be an integer 0-7

def ReadChannel(channel):
  adc = spi.xfer2([1,(8+channel)<<4,0])
  data = ((adc[1]&3) << 8) + adc[2]
  return data

# Function to convert data to voltage level,
# rounded to specified number of decimal places.

def ConvertVolts(data,places):
  volts = (data * 3.3) / 1023
  volts = round(volts,places)
  return volts

# Define sensor channels
light_channel = 0

# Define delay between readings
delay = 1


while True:
  # Read the light sensor data
  light_level = 1024 - ReadChannel(light_channel)
  light_volts = ConvertVolts(light_level,2)

  print "--------------------------------------------"
  print("Light: {} ({}V)".format(light_level,light_volts))

  if light_level <350:
    GPIO.output(5, GPIO.LOW)
    GPIO.output(6, GPIO.LOW)
        GPIO.output(13, GPIO.LOW)

  if light_level >=350 and light_level <= 600:
    GPIO.output(5, GPIO.HIGH)
    GPIO.output(6, GPIO.LOW)
        GPIO.output(13, GPIO.LOW)

  if light_level >600 and light_level <= 700:
    GPIO.output(5, GPIO.HIGH)
    GPIO.output(6, GPIO.HIGH)
        GPIO.output(13, GPIO.LOW)


  if light_level >700:
    GPIO.output(5, GPIO.HIGH)
    GPIO.output(6, GPIO.HIGH)
    GPIO.output(13, GPIO.HIGH)

# Wait before repeating loop
  time.sleep(delay)


GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)  
print("Button pressed: " + str(index))
#TakePicture()  
try:  
    GPIO.wait_for_edge(17, GPIO.FALLING)  
except KeyboardInterrupt:  
    GPIO.cleanup()       # clean up GPIO on CTRL+C exit  
GPIO.cleanup()           # clean up GPIO on normal exit  
2

The code after the while True will never be exectuted. It would only be executed if the while loop terminates. So the following code has no effect.

GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)  
print("Button pressed: " + str(index))
#TakePicture()  
try:  
    GPIO.wait_for_edge(17, GPIO.FALLING)  
except KeyboardInterrupt:  
    GPIO.cleanup()       # clean up GPIO on CTRL+C exit  
GPIO.cleanup()           # clean up GPIO on normal exit  
  • Oh okay thanks! I will move that piece to a different place next time! – Maicoh Dec 9 '15 at 21:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.