Expansion board using adafruit IR sensor to trigger 5 second lights for Burning Man Honorarium

Hello Pixel Blazers, long time user, not at all good at coding. I am a Burning Man artist and have prolly installed 25 Pixel Blazes over the last 3-4 years into various laser cut projects. This year I am helping a friend with a larger project. I have my pixelblaze all set up with an expansion board and I can see the Adafruit IR sensor functioning properly using the export var function.

export var analogInputs
//when the IR sensor is unblocked it returns a value of approx 0.500±
//when the IR sensor is blocked it returns a value of 0.000

What I am trying to do is when a participant fills out a piece of paper and slips it into a slot, the IR sensor detects the paper by crossing the beam path and the lights change to white flashing for 5 seconds, after 5 seconds the lights return to a simple rainbow pattern and thats it.

I started by reading throught the tutorials again and brushing up on my terrible coding skills, I’m more of a sound engineer and 4,000watt laser cutter user/Laser safety officer. You can see my latest project here www.ednatheelephant.com

I found some code online as a start point, and have been trying to edit the code to work with the
//export function analogInput on the A0 header on the expansion board.

I am using this tutorial as my base code and have been trying to edit to to work with what i need to do.

Instead of using buttons though I want to use the IR sensor Arduino | IR Breakbeam Sensors | Adafruit Learning System through the expansion board. (If that is what I should be doing)

Here is the code I am starting with, it obviously needs alot of adjustment just to get the analogInput to replace the button functions in this code, I have tried many changes to the code using the Editor and referring to the instructions in the editor and I’m not getting anywhere:
//
// Example of a momentary button connected to a GPIO pin
//
// Each time the button is pressed, the value of ‘buttonValue’
// should be 1

export var buttonValue // Use export to watch values in the PixelBlaze editor

var BUTTON_PIN = 26

pinMode(BUTTON_PIN, INPUT_PULLDOWN)

export function beforeRender(delta) {
buttonValue = digitalRead(BUTTON_PIN);
}

export function render(index) {
hsv(.0, 0, 0)

if (buttonValue == 1) {
    hsv(.97, 1, 1) // Set color if

HERE IS THE CODE THAT I HAVE EDITED TO USE analogInputs BUT DON’T KNOW WHAT I AM DOING SO IT DOESN’T FUNCTION

export var analogInputs // Use export to watch values in the PixelBlaze editor

var BUTTON_PIN = 26 //Im sure this needs to be changed but not sure what to

analogRead(0) //this is the input I am using A0

export function beforeRender(delta) {
buttonValue = analogRead(0);
}

export function render(index) {
hsv(.97, 1, .2)
}
export function toggleTestButton(isEnabled){ //I added this to have a GUI button while testing
if (buttonValue == 1) {
hsv(.97, 1, 1) // Set color if button is pressed
}
}
Any help to guide me in making this work and teach me along the way would be amazing.
Thanks in advance, I feel like I’m starting to drop the ball on this project and don’t want to let down a big team of people putting the rest of this large art project together next week on the playa.

1 Like

Hi and welcome!

Although I won’t be there this year, I’m a huge mutant vehicle fan. Edna is pretty darn cool!
I know there’s not a lot of time left to get things working, so here’s a version that should work to get a sensor value from Analog input 0. Give it try and see if it reads your sensor and sets the color correctly.

Have a great burn!

(Edit: Not completely sure how you’re configured, so I added comments showing how to make
it work w/either the sensor board or Pixelblaze AnalogRead(). )

export var analogInputs 

var isIRBlocked = 0;    

// for testing 
export function toggleIRState(isEnabled) { 
  analogInputs[0] = isEnabled ? 0.0 : 0.5;
}

export function beforeRender(delta) {
  // Get the current sensor state
  //
  // My understanding is unblocked==0.5v, blocked== 0v
  // this statement sets the isIRBlocked flag if voltage 
  // is very close to 0. (You may need to change this comparison
  // tolerance to suit your hardware.)
  // NOTE: this version works with the analog inputs on the sensor board
  // Swap it for the line below to read from the Pixelblaze analog pins instead.
  //  isIRBlocked = (abs(AnalogRead(0)) < 0.005);

  isIRBlocked = (abs(analogInputs[0]) < 0.005);
}

export function render(index) {
  
  // choose a color according to the button state
  if (isIRBlocked) {
    hsv(.97, 1, 1)    
  } 
  else {
    hsv(.97,1,.1)
  }
}
2 Likes

HI Zranger1!!! Thank you for the help on this! It works and I’m learning alot from this project. I will adjust the different states and submit what I finished with for the code. I was to have the normal state to be a rainbow sequence, and the IR blocked state to be flashing white for 5 seconds.
Do you know how to make this when blocked to flash for 5 seconds then move back to the normal state?(rainbow)?

I’m glad it worked! Here’s a version that will display a basic rainbow when the sensor isn’t blocked, and flash white for 5 seconds when the sensor is triggered. Re-triggering the sensor resets the timer, so it will keep flashing as long as the sensor is activated, and for 5 seconds after it is clear.

(if you had to switch to using AnalogRead() rather than analogInputs[0], be sure to do that in this version too!)

export var analogInputs 

var isIRBlocked = 0
var lastSensorState = 0

var blinkActive = 0;            // are we currently blinking?
var blinkTimer = 0              // holds blinking elapsed time
var MINIMUM_BLINK_TIME = 5000   // milliseconds
var CYCLE_RATE = MINIMUM_BLINK_TIME / 1000;
var speed = 0.08;

// for testing 
export function toggleIRState(isEnabled) { 
  analogInputs[0] = isEnabled ? 0.0 : 0.5
}

// a control to adjust speed of rainbow
export function sliderSpeed(v) {
 speed = mix(0.2,0.005,v)  
}

var t1
export function beforeRender(delta) {
  // get the current value of the rainbow color timer
  t1 = time(speed);


  // if we're blinking because the sensor was previously blocked...
  if (blinkActive) {
    // keep track of elapsed time
    blinkTimer += delta
   
    // if the blink timer has expired, stop blinking 
    if (blinkTimer > MINIMUM_BLINK_TIME) {
      blinkActive = 0;
    }
  }
  
  // Get the current sensor state
  //
  // My understanding is unblocked==0.5v, blocked== 0v
  // this statement sets the isIRBlocked flag if voltage 
  // is very close to 0. (You may need to change this comparison
  // tolerance to suit your hardware.)
  isIRBlocked = (abs(analogInputs[0]) < 0.005)
  
  if (isIRBlocked != lastSensorState) {
    if (isIRBlocked) {
      blinkActive = 1
      blinkTimer = 0;
    }
    
    lastSensorState = isIRBlocked
  }
}

export function render(index) {
  
  // choose a color according to the current state of the
  // blinkActive flag
  if (blinkActive) {
    // blink on/off in white at about 2hz
    // to change the blink speed, change the cycle rate.
    hsv(0,0,square(CYCLE_RATE * (blinkTimer/MINIMUM_BLINK_TIME) ,0.5))    
  } 
  else {
    // display a bright rainbow when IR sensor is
    // not blocked. (Use t1 - index/etc. if you want it
    // to travel the other way. 
    hsv(t1 + index/pixelCount,1,1)
  }
}
1 Like