Trouble running two seperate if-else statement

General discussion about topics related to OpenMV.
thomasaung
Posts: 5
Joined: Thu Jul 16, 2020 11:34 pm

Trouble running two seperate if-else statement

Postby thomasaung » Thu Aug 06, 2020 12:07 am

Code: Select all

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!
thomasaung
Posts: 5
Joined: Thu Jul 16, 2020 11:34 pm

Re: Trouble running two seperate if-else statement

Postby thomasaung » Thu Aug 06, 2020 12:07 am

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!
User avatar
kwagyeman
Posts: 4430
Joined: Sun May 24, 2015 2:10 pm

Re: Trouble running two seperate if-else statement

Postby kwagyeman » Thu Aug 06, 2020 12:21 am

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.
Nyamekye,
thomasaung
Posts: 5
Joined: Thu Jul 16, 2020 11:34 pm

Re: Trouble running two seperate if-else statement

Postby thomasaung » Thu Aug 06, 2020 12:56 am

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.
User avatar
kwagyeman
Posts: 4430
Joined: Sun May 24, 2015 2:10 pm

Re: Trouble running two seperate if-else statement

Postby kwagyeman » Thu Aug 06, 2020 2:22 pm

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.
Nyamekye,
thomasaung
Posts: 5
Joined: Thu Jul 16, 2020 11:34 pm

Re: Trouble running two seperate if-else statement

Postby thomasaung » Fri Aug 07, 2020 12:18 am

im looking for a negative edge, from high to low, i will post my individual if elsif loops.
thomasaung
Posts: 5
Joined: Thu Jul 16, 2020 11:34 pm

Re: Trouble running two seperate if-else statement

Postby thomasaung » Fri Aug 07, 2020 12:18 am

Code: Select all

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)
User avatar
kwagyeman
Posts: 4430
Joined: Sun May 24, 2015 2:10 pm

Re: Trouble running two seperate if-else statement

Postby kwagyeman » Fri Aug 07, 2020 1:31 am

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

Return to “General Topics”

Who is online

Users browsing this forum: No registered users and 1 guest