Adepalma
Posts: 1
Joined: Tue Nov 21, 2017 10:37 pm

Creating If Statement Based off Sensor Reading

Tue Nov 21, 2017 10:41 pm

For a school project I am trying to create an if statement off of a sensor reading. Ideally, I want to have a photocell resistor send data to the pi, and then based off of that read data, proceed with the if/else statements. Does anyone know what command I can use to set a value to the data collected by the photocell?

DirkS
Posts: 7732
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Creating If Statement Based off Sensor Reading

Wed Nov 22, 2017 2:23 pm

What language? What photocell?
Also post the code you already have.

sora03
Posts: 118
Joined: Mon Dec 29, 2014 4:11 pm

Re: Creating If Statement Based off Sensor Reading

Thu Nov 23, 2017 11:59 am

you should post the code you have to help you debug it. generally what you want to do is get sensor data then use conditional if/then/else statements if the sensor reading is at a certain value.

B.Goode
Posts: 4689
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Creating If Statement Based off Sensor Reading

Thu Nov 23, 2017 12:14 pm

If 'photocell' means a Light Dependent Resistor then the Raspberry Pi Foundation have a resource (tutorial) that explains clearly how to use one.

https://projects.raspberrypi.org/en/pro ... -computing


The CamJam EduKit #2 also contains a light dependent resistor, and the associated worksheet describes how to write python scripts to get a reading from it.

https://camjam.me/?page_id=623

alphanumeric
Posts: 1275
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Creating If Statement Based off Sensor Reading

Thu Nov 23, 2017 2:03 pm

I'm doing something similar with an SI1145 light sensor. I snipped some of my python code. I'm using it to dim (sense.low_light = True) the LED matrix on my sense hat in low light conditions. A dark room for example. And switch it back to full bright (sense.low_light = False) in bright light conditions. Outside in the bright sun for example.

Code: Select all

import SI1145.SI1145 as SI1145

uvs = SI1145.SI1145()

def readvis():
    vis = uvs.readVisible()
    if vis < 300:
        sense.low_light = True
    elif vis >= 300:
        sense.low_light = False

bensimmo
Posts: 1928
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Creating If Statement Based off Sensor Reading

Thu Nov 23, 2017 6:18 pm

alphanumeric wrote:
Thu Nov 23, 2017 2:03 pm
I'm doing something similar with an SI1145 light sensor. I snipped some of my python code. I'm using it to dim (sense.low_light = True) the LED matrix on my sense hat in low light conditions. A dark room for example. And switch it back to full bright (sense.low_light = False) in bright light conditions. Outside in the bright sun for example.

Code: Select all

import SI1145.SI1145 as SI1145

uvs = SI1145.SI1145()

def readvis():
    vis = uvs.readVisible()
    if vis < 300:
        sense.low_light = True
    elif vis >= 300:
        sense.low_light = False
That's given me another idea to play with, I have the TCS3472 on an Envirophat so could play with something similar.

alphanumeric
Posts: 1275
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Creating If Statement Based off Sensor Reading

Thu Nov 23, 2017 8:01 pm

bensimmo wrote:
Thu Nov 23, 2017 6:18 pm
That's given me another idea to play with, I have the TCS3472 on an Envirophat so could play with something similar.
I bought the SI1145 to read the UV index. Once I got that working I started using the visible light readings to do other things. Bonus. =)

PingoBags
Posts: 16
Joined: Mon Nov 06, 2017 10:53 pm
Location: NS, Canada

Re: Creating If Statement Based off Sensor Reading

Fri Nov 24, 2017 10:15 am

alphanumeric wrote:
Thu Nov 23, 2017 2:03 pm
I'm doing something similar with an SI1145 light sensor. I snipped some of my python code. I'm using it to dim (sense.low_light = True) the LED matrix on my sense hat in low light conditions. A dark room for example. And switch it back to full bright (sense.low_light = False) in bright light conditions. Outside in the bright sun for example.

Code: Select all

import SI1145.SI1145 as SI1145

uvs = SI1145.SI1145()

def readvis():
    vis = uvs.readVisible()
    if vis < 300:
        sense.low_light = True
    elif vis >= 300:
        sense.low_light = False
You could probably simplify that even more by removing the if/elif

Code: Select all

 sense.low_light = uvs.readVisible() < 300
sense.low_light would still equal false if uvs.readVisible() was greater than 300, and true if it was less.

alphanumeric
Posts: 1275
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Creating If Statement Based off Sensor Reading

Fri Nov 24, 2017 12:21 pm

Yeah, there was another line after that, that I didn't post. I just did a quick edit. I'm doing a bit more than what I posted, and it would have just confused things. I'm also changing the text color on my Sense Hat to all whit if the VIS is above 500. That line read

Code: Select all

  elif vis >= 300 and vis < 500 and m == (0):   
before I edited it. I'm also turning on or off a secondary indicator (10mm LED's)

bensimmo
Posts: 1928
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Creating If Statement Based off Sensor Reading

Fri Nov 24, 2017 2:34 pm

You don't really need the
vis >= 300
part of it as thats implied from the previous if statement. *


It's nice to have it there for reading, but you need to change two parts of you alter the range so for errors it is best no to have it there.

*Also elif should be else otherwise it'll error.

alphanumeric
Posts: 1275
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Creating If Statement Based off Sensor Reading

Fri Nov 24, 2017 2:53 pm

It all works, and no errors. I will have a look at what you mentioned though. I'm no python expert, and a lot of my code was trial and error getting it to work.

alphanumeric
Posts: 1275
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Creating If Statement Based off Sensor Reading

Fri Nov 24, 2017 3:07 pm

Can I put an else: at the end with nothing under it?

I'm using an "and". If m = 1 I want nothing to happen?

Code: Select all


 elif vis >= 500 and m == (0):
        sense.low_light = False
 else:        


bensimmo
Posts: 1928
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Creating If Statement Based off Sensor Reading

Fri Nov 24, 2017 3:16 pm

Sorry I forgot you are actually checking the condition (as you wouldn't need to normally for just the light check)

It'll be fine
didn't need the else..
As you were .. ;-)

(No expert myself :-))

But if/else if/elif/else is the basic start of python. So Adepalma just needs to go and do a starter on Python.
Perhaps the main website examples.

alphanumeric
Posts: 1275
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Creating If Statement Based off Sensor Reading

Fri Nov 24, 2017 3:29 pm

Ok. Works for me.

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 45 guests