Stop while loop after 1 is added to var

This is the place for queries that don't fit in any of the other categories.

Stop while loop after 1 is added to var

Postby RareAMV » Fri Feb 07, 2014 4:55 pm

This might be a really really basic question, but i cant find a solution.
I have a script that reads a light sensor, i want to use the light sensor to detect movement, and after each time something passes it adds 1 to a variable.
But this script just keeps adding +1 to the variable when the sensor detects light... so what needs to happen is basically that after the sensor detects light, it just add 1, one time to the variable, and when its turned off, and on again it adds 1 again.
This is what i currently have:

Code: Select all
import spidev
import time
import os

# Open SPI bus
spi = spidev.SpiDev()
spi.open(0,0)

# Function to read SPI data from MCP3008 chip
# Channel must be an integer 0-7
def ReadChannel(channel):
  adc = spi.xfer2([1,(8+channel)<<4,0])
  data = ((adc[1]&3) << 8) + adc[2]
  return data

# Function to convert data to voltage level,
# rounded to specified number of decimal places.
def ConvertVolts(data,places):

  volts = (data * 3.3) / 1023
  volts = round(volts,places)
  return volts

# Define sensor channels
light_channel = 0


# Define delay between readings
delay = 0.1

# Defines the start count
registrate = 0

while True:

  # Read the light sensor data
  light_level = ReadChannel(light_channel)
  light_volts = ConvertVolts(light_level,2)


 # Print out results and counts how many times something is detected: that does not work properly
 # This is what i have done:
  while light_level > 1:
   registrate += 1
   print "--------------------------------------------"
   print("Light : {} ({}V)".format(light_level,light_volts))
   print(registrate)
   light_level = 0
   time.sleep(delay)
  else:
   time.sleep(delay)




I did not create the hole script, but that last part was edited by me. I don't have much knowledge about this programming language, so if this problem should be easily solved with common knowledge, tell me were i can learn it. I did follow some tutorials on loops, but it did not gave me an answer. I also know that what i want to happen should happen before or after the while True: statement, at least i think. Because according to me, everything keeps refreshing in the while loop. But whatever i try, it wont work.

Thanks in advance.
Last edited by Yoriz on Fri Feb 07, 2014 5:49 pm, edited 1 time in total.
Reason: First post lock
RareAMV
 
Posts: 3
Joined: Fri Feb 07, 2014 3:56 pm

Re: Stop while loop after 1 is added to var

Postby Yoriz » Fri Feb 07, 2014 5:48 pm

Hi welcome to the forum, please read the new users, read this link in my signature if you haven't already.

I don't know for sure what light_level indicates off im guessing that as you are checking for light_level > 1 that 1 or less is off.
I dont know for sure if the light could still be on from previously detect movement if it can you would need to keep an eye on its previous state as well and only add 1 if it was previously off.

Assuming the above maybe something like this.
Code: Select all
previous_light_level = 1 #i'm guessing its 1 for off

while True:
   
    # Read the light sensor data
    light_level = ReadChannel(light_channel)
    light_volts = ConvertVolts(light_level,2)


    # Print out results and counts how many times something is detected: that does not work properly
    # This is what i have done:
   
    # i don't know if previous_light_level <= 1 is correct for a light off
    #condition adjust condition to suit maybe
    if light_level > 1 and previous_light_level <= 1:
        registrate += 1
        print "--------------------------------------------"
        print "Light : {} ({}V)".format(light_level,light_volts)
        print registrate

    time.sleep(delay)
    previous_light_level = light_level
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 789
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Stop while loop after 1 is added to var

Postby RareAMV » Fri Feb 07, 2014 7:40 pm

hi,
thank you for responding, light_level indicates the brightness of the light, its not calibrated for mine type of light sensor, so when the light is not directed on it, it will give output value of 0 , when light is directed on it, it gives a max value of 1023. with my script it checks if that value goes above 1(it goes always above 1) it starts counting, until it goes back to 0.
Hopefully this information will help.

Thanks in advance
RareAMV
 
Posts: 3
Joined: Fri Feb 07, 2014 3:56 pm

Re: Stop while loop after 1 is added to var

Postby Yoriz » Fri Feb 07, 2014 8:07 pm

This should only add one each time the light goes past 1 for the first time only and then only adds one again after it has gone back to o
Code: Select all
LIGHT_OFF = 0

previous_light_level = LIGHT_OFF

while True:

    # Read the light sensor data
    light_level = ReadChannel(light_channel)
    light_volts = ConvertVolts(light_level,2)

    if light_level > 1 and previous_light_level == LIGHT_OFF:
        registrate += 1
        print "--------------------------------------------"
        print "Light : {} ({}V)".format(light_level,light_volts)
        print registrate

    time.sleep(delay)
    previous_light_level = light_level
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 789
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Stop while loop after 1 is added to var

Postby RareAMV » Sat Feb 08, 2014 11:14 am

Thank you so much!
RareAMV
 
Posts: 3
Joined: Fri Feb 07, 2014 3:56 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 4 guests