1

I am trying to implement a light sensors input so it outputs a value in the larger python script.

Specifically print the LED_BRIGHTNESS = 75 or LED_BRIGHTNESS = 255

Basically I when this script runs I want the sensor to dictate the LED brightness for the string of LED's

I started out trying to do the following...

import urllib2
import xml.etree.ElementTree as ET
import time
from neopixel import *
import sys
import os    
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(4,GPIO.IN)

if GPIO.input(4) == 1:
        print "LED_BRIGHTNESS = 75"
else:
        print "LED_BRIGHTNESS = 255"
# LED strip configuration:
LED_COUNT      = 95      # Number of LED pixels.
LED_PIN        = 18      # GPIO pin connected to the pixels (18 uses PWM!).
#LED_PIN        = 10      # GPIO pin connected to the pixels (10 uses SPI /dev/spidev0.0).
LED_FREQ_HZ    = 800000  # LED signal frequency in hertz (usually 800khz)
LED_DMA        = 5       # DMA channel to use for generating signal (try 5)
#LED_BRIGHTNESS = 255     # Set to 0 for darkest and 255 for brightest
LED_INVERT     = False   # True to invert the signal (when using NPN transistor level shift)
LED_CHANNEL    = 0       # set to '1' for GPIOs 13, 19, 41, 45 or 53
LED_STRIP      = ws.WS2811_STRIP_GRB   # Strip type and colour ordering

That would output the result into the terminal, but I need it to output into the larger script.

So then I tired the following...

import urllib2
import xml.etree.ElementTree as ET
import time
from neopixel import *
import sys
import os
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(4,GPIO.IN)


if GPIO.input(4) == 1: "LED_BRIGHTNESS = 75"
if GPIO.input(4) == 0: "LED_BRIGHTNESS = 255"


# LED strip configuration:
LED_COUNT      = 95      # Number of LED pixels.
LED_PIN        = 18      # GPIO pin connected to the pixels (18 uses PWM!).
#LED_PIN        = 10      # GPIO pin connected to the pixels (10 uses SPI /dev/spidev0.0).
LED_FREQ_HZ    = 800000  # LED signal frequency in hertz (usually 800khz)
LED_DMA        = 5       # DMA channel to use for generating signal (try 5)
#LED_BRIGHTNESS = 255     # Set to 0 for darkest and 255 for brightest
LED_INVERT     = False   # True to invert the signal (when using NPN transistor level shift)
LED_CHANNEL    = 0       # set to '1' for GPIOs 13, 19, 41, 45 or 53
LED_STRIP      = ws.WS2811_STRIP_GRB   # Strip type and colour ordering

This returns the following error.

Traceback (most recent call last):
  File "metar.py", line 30, in <module>
    strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL, LED_STRIP)
NameError: name 'LED_BRIGHTNESS' is not defined

I am so new at this I don't even know what the correct terminology to use when trying to google the problem. I tried looking at how to print if statement output into string, or how to output if statement into string and I keep finding things that talk about completely predefined values not something coming from a sensors if statement.

If someone could point me in the right direction I'd appreciate it.

4
  • 1
    Surely you didn't mean to just wrap your code in a string...? Commented Apr 9, 2018 at 0:28
  • I think so. I need it to output so the LED_BRIGHTNESS value is based off the sensor input. Commented Apr 9, 2018 at 0:34
  • You just want to set the variable dynamically? Its just LED_BRIGHTNESS = 75 if GPIO.input(4) == 1 else 255. Commented Apr 9, 2018 at 0:38
  • Thanks, I figured this would be stupid easy, just no clue here. Really appreciate your time. Commented Apr 9, 2018 at 0:41

1 Answer 1

1

It looks like you want to set a variable dynamically. In this case, since there are only two possible values, an inline if will do:

LED_BRIGHTNESS = 75 if GPIO.input(4) == 1 else 255

Or you could expand the if:

if GPIO.input(4) == 1:
    LED_BRIGHTNESS = 75
else:
    LED_BRIGHTNESS = 255
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.