Python Forum

Full Version: SOLVED python script help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey, super new to python and after following a few different tutorials and making a complete mess I was able to create a file to capture an image from my raspberry pi. however I cant figure out why or how to have the file always waiting for a button press. It works the first time then I assume stops. No errors.

Program was created in Thonny and F5 to run it. (not sure if its intended to only run once?) idk how else to start the script.

what I wanted to achieve was have it run and wait for a button press then take a picture and save it, which is does currently, however when I press the button a second time nothing happens.


This is my super messy code.

import picamera
from time import sleep
from subprocess import call
from gpiozero import Button
from datetime import datetime

button = Button(17)

#filepath
filePath = "/home/pi/Desktop/mmPictures/images/"

#setting up date and time
#grab the current time
currentTime = datetime.now()
#create filename for our picture
picTime = currentTime.strftime("%Y.%m.%d-%H%m%S")
picName = picTime + '.jpg'
completeFilePath = filePath + picName

button.wait_for_press()
#setup the camera such that it closes when we are done with it
print("Setting up")
sleep(1)

commandStop = "sudo systemctl stop motioneye"
call ([commandStop], shell=True)
print("Motioneye stopped")
sleep(1)

with picamera.PiCamera() as camera:
    camera.resolution = (1280, 720)
    #camera.capture("/home/pi/Desktop/mmPictures/images/newimage.jpg")
    camera.capture(completeFilePath)
    
print("Picture taken")
sleep(1)
commandStart = "sudo systemctl start motioneye"
call([commandStart], shell=True)
print("Motioneye started")
You need to put your button.wait_for_press and subsequent code in a loop. For starters, I suggest adding
while True:
at line 19 and indent all of your code after that one level. You will need to ctrl-C to get out of the program, but once it prints Motioneye started it will go back to the top of the loop and wait for press again.
(Nov-15-2020, 02:17 PM)jefsummers Wrote: [ -> ]You need to put your button.wait_for_press and subsequent code in a loop. For starters, I suggest adding
while True:
at line 19 and indent all of your code after that one level. You will need to ctrl-C to get out of the program, but once it prints Motioneye started it will go back to the top of the loop and wait for press again.

Sorta works, however when i press the button again it runs though the loop but it overwrites the same image? but when i run it without the loop it saves to a new image..

EDIT: it creates a new save for the image if i start and stop the program, but if i dont stop the program it overwrites the current image.
Move your while to line 11 so you get a new filename based on time
(Nov-15-2020, 10:24 PM)jefsummers Wrote: [ -> ]Move your while to line 11 so you get a new filename based on time

I have figured it out, just forgot to post another update! but thank you for pointing me in the right direction!