mre182
Posts: 1
Joined: Wed May 30, 2018 4:50 pm

My 1st RPi/Project Help! - help needed with time delay photography

Wed May 30, 2018 5:12 pm

PROJECT:
I would like to buy an RPi and program it to take photos with the pi camera after a touch sensor is pressed and the 3 second timer counts down.

Is that possible and if so, I'd like to know what else I need besides: Rpi3, Camera v2, touch display (3.5").
Do I need a stronger power adapter to run the camera and the display?

ANY HELP with items/necessities would be greatly appreciated!

Thanks!
- Mark

gordon77
Posts: 4117
Joined: Sun Aug 05, 2012 3:12 pm

Re: My 1st RPi/Project Help!

Wed May 30, 2018 5:22 pm

It should work fine with the official pi psu assuming the touch display is powered by the pi.

Which touch sensor, or do you mean the touch display?

Here's an Python example with on screen button and a switch between GPIO pin 26 and gnd

Code: Select all

#!/usr/bin/env python3
import os
import pygame, sys
import time
import shutil
from pygame.locals import *
import subprocess, glob
import RPi.GPIO as GPIO
import signal
from datetime import datetime
sw = 26 # Take photo button - GPIO pin 26

# set screen sizes
width = 480
height = 320

GPIO.setmode (GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(sw,GPIO.IN,pull_up_down = GPIO.PUD_UP)

pygame.init()

windowSurfaceObj = pygame.display.set_mode((width, height), pygame.NOFRAME, 24)
start = 0

# start subprocess
rpistr = "raspistill -n -o /run/shm/test.jpg -tl 0 -t 0 -w " + str(width) + " -h " + str(height)
p=subprocess.Popen(rpistr,shell=True, preexec_fn=os.setsid)

def button (bx1,by1,bx2,by2,height,bColor):
   greyColor = pygame.Color(128,128,128)
   dgryColor = pygame.Color(64,64,64)
   redColor = pygame.Color(255,0,0)
   whiteColor = pygame.Color(255,255,255)
   colors = [greyColor,dgryColor,redColor, whiteColor]
   Color = colors[bColor]
   pygame.draw.rect(windowSurfaceObj,Color,Rect(bx1,height+by1,bx2,by2))
   pygame.draw.line(windowSurfaceObj,whiteColor,(bx1,height+by1),(bx1+bx2-1,height+by1))
   pygame.draw.line(windowSurfaceObj,whiteColor,(bx1+1,height+by1+1),(bx1+bx2-2,height+by1+1))
   return()

def keys(msg,fsize,fcolor,fx,fy,upd):
   dgryColor = pygame.Color(64,64,64)
   greenColor = pygame.Color(0,255,0)
   colors = [dgryColor,greenColor]
   color = colors[fcolor]
   fontObj = pygame.font.Font('freesansbold.ttf',fsize)
   msgSurfaceObj = fontObj.render(msg, False,color)
   msgRectobj = msgSurfaceObj.get_rect()
   msgRectobj.topleft =(fx,fy)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
   windowSurfaceObj.blit(msgSurfaceObj, msgRectobj)
   if upd ==1:
      pygame.display.update(pygame.Rect(fx,fy,250,32))
   return()
  
pygame.display.update() 

while True:
   while os.path.exists('/run/shm/test.jpg') == False:
      time.sleep(.005)
   imagefile = ('/run/shm/test.jpg')
   image = pygame.image.load(imagefile)
   catSurfaceObj = image
   windowSurfaceObj.blit(catSurfaceObj,(0,0))
   button (width - 50,height - 50,50,50,0,0)
   if start == 0:
      keys ("Save",14,0,width - 40,height- 30,1)
   else:
      keys ("Save",14,1,width - 40,height- 30,1)
   pygame.display.update(0,0,width,height)

   if GPIO.input(sw)== 0: # save picture if GPIO button pressed
      start = time.time()
      keys ("Save",14,1,width - 40,height- 30,1)
      
   if start > 0:
      keys (str(3-(int(time.time()-start))),14,1,width - 30,height- 15,1)
      if time.time() - start > 3: # sets 3 second timer
         now = datetime.now()
         fname = ('/home/pi/"{0:%c}".jpg'.format(now)) 
         shutil.copy('/run/shm/test.jpg',fname)
         os.rename('/run/shm/test.jpg','/run/shm/oldtest.jpg')
         keys ("Save",14,0,width - 40,height- 30,1)
         start = 0
       
# read mouse or keyboard

   for event in pygame.event.get():
       if event.type == QUIT:
          os.killpg(p.pid, signal.SIGTERM)
          pygame.quit()
          sys.exit()
           
       elif event.type == MOUSEBUTTONDOWN or event.type == KEYDOWN:
          kz = 0
          if event.type == KEYDOWN:
             kz = event.key
             if kz == K_ESCAPE:
                os.killpg(p.pid, signal.SIGTERM)
                pygame.quit()
                sys.exit()
             
          if event.type == MOUSEBUTTONDOWN:
             mousex,mousey = event.pos
             # save picture if on mouse button or touch screen pressed, whilst cursor on screen button
             if mousex > width - 50 and mousey > height - 50:
                start = time.time()
                keys ("Save",14,1,width - 40,height- 30,1)
                
Last edited by gordon77 on Thu May 31, 2018 9:49 am, edited 1 time in total.

User avatar
bensimmo
Posts: 4161
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: My 1st RPi/Project Help!

Wed May 30, 2018 5:51 pm

Your should have a read and do
https://projects.raspberrypi.org/en/pro ... h-picamera

Then at the end have a look at the stop motion project, this shows you how to use a physical push button to take a picture.

(For the push button, if you don't have one, just have two bits of wire and touch them together to be your button.)

This is all easier on a monitor.

If you are using a touch screen, you'll need to follow instructions to wire it up and wire up the touch part.
Then create an on screen button, perhaps look at the developing GuiZero python module to just create an on screen button. Or create a webpage button with a live camera display.

And then develop it further as your imagination an knowledge takes you.

texy
Forum Moderator
Forum Moderator
Posts: 5155
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: My 1st RPi/Project Help! - help needed with time delay photography

Thu May 31, 2018 12:44 pm

Thread title changed to something more meaningful
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

Return to “General discussion”