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

**URL:** <https://forum.electromage.com/t/expansion-board-using-adafruit-ir-sensor-to-trigger-5-second-lights-for-burning-man-honorarium/3706>\
**Category:** Patterns and Code\
**Tags:** beginner\
**Created:** [August 16, 2024, 2:01pm UTC](https://forum.electromage.com/t/expansion-board-using-adafruit-ir-sensor-to-trigger-5-second-lights-for-burning-man-honorarium/3706 "2024-08-16T14:01:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tahoejmfc](https://forum.electromage.com/letter_avatar_proxy/v4/letter/t/45deac/32.png) [@tahoejmfc](https://forum.electromage.com/u/tahoejmfc)\
**Post date:** [August 16, 2024, 2:01pm UTC](https://forum.electromage.com/t/expansion-board-using-adafruit-ir-sensor-to-trigger-5-second-lights-for-burning-man-honorarium/3706/1 "2024-08-16T14:01:25Z")

</div>

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](http://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.

> [@Simple control using momentary buttons](https://forum.electromage.com/t/simple-control-using-momentary-buttons/2615):
>
> Sometimes I want a simple button to perform a specialized function. While the current onboard button is incredibly useful, the functionality that it offers cannot be customized. I had some project ideas that required one or more custom inputs, so I decided to use the available GPIO digital input pins with momentary buttons. The following examples demonstrate how I’ve been implementing buttons in costumes and other projects lately. Example - Basic Button Here I connected a momentary button to t…

Instead of using buttons though I want to use the IR sensor [Arduino | IR Breakbeam Sensors | Adafruit Learning System](https://learn.adafruit.com/ir-breakbeam-sensors/arduino) 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.

---

<div class="post-metadata">

**Author:** ![zranger1](https://forum.electromage.com/user_avatar/forum.electromage.com/zranger1/32/301_2.png) [@zranger1](https://forum.electromage.com/u/zranger1)\
**Post date:** [August 17, 2024, 12:37am UTC](https://forum.electromage.com/t/expansion-board-using-adafruit-ir-sensor-to-trigger-5-second-lights-for-burning-man-honorarium/3706/2 "2024-08-17T00:37:07Z")

</div>

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(). )

```auto
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)
  }
}

```

---

<div class="post-metadata">

**Author:** ![tahoejmfc](https://forum.electromage.com/letter_avatar_proxy/v4/letter/t/45deac/32.png) [@tahoejmfc](https://forum.electromage.com/u/tahoejmfc)\
**Post date:** [August 17, 2024, 7:00pm UTC](https://forum.electromage.com/t/expansion-board-using-adafruit-ir-sensor-to-trigger-5-second-lights-for-burning-man-honorarium/3706/3 "2024-08-17T19:00:31Z")

</div>

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)?

---

<div class="post-metadata">

**Author:** ![zranger1](https://forum.electromage.com/user_avatar/forum.electromage.com/zranger1/32/301_2.png) [@zranger1](https://forum.electromage.com/u/zranger1)\
**Post date:** [August 17, 2024, 10:02pm UTC](https://forum.electromage.com/t/expansion-board-using-adafruit-ir-sensor-to-trigger-5-second-lights-for-burning-man-honorarium/3706/4 "2024-08-17T22:02:42Z")

</div>

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!)

```auto
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)
  }
}

```
