Trouble running two seperate if-else statement

import machine
import sensor, image
import pyb
from pyb import Pin
from pyb import RTC
import time


rtc = RTC() # Real Time Clock
#Will set the clock, only need to uncomment and run once to set real time clock
#rtc.datetime((2020, 10, 8, 3, 28, 0, 0, 0))


sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.RGB565) # or sensor.GRAYSCALE
sensor.set_framesize(sensor.QVGA) # or sensor.QQVGA (or others)
sensor.skip_frames(time = 2000) # Let new settings take affect.
BLUE_LED_PIN = 3
button2 = machine.Pin('P3', machine.Pin.IN, machine.Pin.PULL_UP)
button1 = machine.Pin('P1', machine.Pin.IN, machine.Pin.PULL_UP)
valve = pyb.Pin("P0", pyb.Pin.OUT_PP)
img = sensor.snapshot
while True:
    first = button1.value() #if else statement for solenoid control
    time.sleep(1)
    second = button1.value()
    if first and not second:
         print('Button 1 not pressed')
         valve.value(1)
    elif not first and second:
         print('Button 1 pressed')
         valve.value(0)
         
    first2 = button2.value() #if else statement for camera interfacing
    time.sleep(1)
    second2 = button2.value()
    if first2 and not second2:
        print('Button 2 released')
    elif not first2 and second2:
         print('Button 2 pressed!')
         dt=rtc.datetime()
         dtfilename = 'spacecam_'
         for item in dt:
           dtfilename = dtfilename+str(item)
         dtfilename = dtfilename+".jpg"
         pyb.LED(BLUE_LED_PIN).on()
         sensor.snapshot().save(dtfilename)
         print(dtfilename+" saved to SD card")
         pyb.LED(BLUE_LED_PIN).off
         
   Hi when i run the code, only the if else statement for camera interfacing works, the statement for the solenoid control doesnt. Help will be greatly appreciated thanks!

Hi when i run the code, only the if else statement for camera interfacing works, the statement for the solenoid control doesnt. Help will be greatly appreciated thanks!

What behavior are you expecting? Are you trying to look for a neg/pos edge? The 1 second delays there will really make things choppy.

If the valve control isn’t working I’d verify the pin works by printing the state of “first” and “second” each time in the loop.

The behaviour im expecting is that when i press a respective button, for eg button1, i only want the if else statement of button1 to run.

Can you give me the print output I requested? I don’t see a bug for starting at your code. So, Im thinking is a hardware/mechanical issue.

im looking for a negative edge, from high to low, i will post my individual if elsif loops.

import machine
import pyb
from pyb import Pin
import time
button1 = machine.Pin('P1', machine.Pin.IN, machine.Pin.PULL_UP)
valve = pyb.Pin("P0", pyb.Pin.OUT_PP)
while True:
    first = button1.value()
    time.sleep(2)
    second = button1.value()
    if first and not second:
        print('Solenoid valve closed')
        valve.value(1)
    elif not first and second:
        print('Solenoid valve open')
        valve.value(0)

Please post the print output of you testing it. As I said, it doesn’t look wrong.