Code Help with Flora & Maxbotix
Moderators: adafruit_support_bill, adafruit

Code Help with Flora & Maxbotix

by newbish1 on Sat Aug 10, 2013 12:39 pm

I am making a 3D printed bow_tie that flashes four flora pixels when someone gets too close, a space invading close talker; a project and idea that was mused about by PT some time ago and I have yet to see it built. So I'm trying to make it. I need a way to eliminate false triggers, like drinking from a wine glass or waving an arm in front of the sensor. Everything else works fine. I want to sensor to be able to compare or delay for 5 seconds before the pixels start blasting away flashing. Here is the current code, a mashup of sloppyness.

I familiar enough about coding to know that I should be using a variable for the pixels and i++ them to make the code cleaner, but not enough to do that properly. Any help would be very appreciated. I'm using Flora and the Maxbotix EZ1. Also I'm using this sensor over others because it aesthetically fits better in the not of the tie more than a sharp, ping or other. Thanks for the help and I look forward to any suggestions.


Code: Select all | TOGGLE FULL SIZE
#include <Adafruit_NeoPixel.h>

// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_RGB Pixels are wired for RGB bitstream
// NEO_GRB Pixels are wired for GRB bitstream
// NEO_KHZ400 400 KHz bitstream (e.g. FLORA pixels)
// NEO_KHZ800 800 KHz bitstream (e.g. High Density LED strip)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(4, 6, NEO_GRB + NEO_KHZ400);
int sonarPin = 9; //pin connected to analog out on maxsonar sensor

int inchesAway; // inches away from the maxsonar sensor


void setup() {
strip.begin();
strip.show(); // Initialize all pixels to 'off'
Serial.begin(9600);
}

void loop() {
inchesAway = analogRead(sonarPin) /2; // reads the maxsonar sensor and divides the value by 2
// approximate distance in inches
Serial.print(inchesAway); // prints the sensor information from the maxsonar to the serial monitor
Serial.println(" inches from sensor");
if (inchesAway < 19) { // if something is 24 inches away then make a 1khz sound

// turn LED on:
strip.setPixelColor(0, strip.Color(250, 0, 0));
strip.show();
strip.setPixelColor(1, strip.Color(250, 0, 0));
strip.show();
strip.setPixelColor(2, strip.Color(250, 0, 0));
strip.show();
strip.setPixelColor(3, strip.Color(250, 0, 0));
strip.show();
delay(100);
strip.setPixelColor(0, strip.Color(0, 0, 0));
strip.show();
strip.setPixelColor(1, strip.Color(0, 0, 0));
strip.show();
strip.setPixelColor(2, strip.Color(0, 0, 0));
strip.show();
strip.setPixelColor(3, strip.Color(0, 0, 0));
strip.show();
delay(100);
}
else if (inchesAway > 18){
 
strip.setPixelColor(1, strip.Color(0, 0, 0));
}





}
newbish1
 
Posts: 8
Joined: Fri Sep 30, 2011 9:47 am

Re: Code Help with Flora & Maxbotix

by adafruit_support_bill on Sat Aug 10, 2013 2:33 pm

Looks like it would probably work. Have you tested it?

To simplify the code a bit, you can do loops like this:
Code: Select all | TOGGLE FULL SIZE
for(int i = 0; i < 4; i++)
{
  strip.setPixelColor(i, strip.Color(250, 0, 0));
}
strip.show();
User avatar
adafruit_support_bill
 
Posts: 29182
Joined: Sat Feb 07, 2009 9:11 am

Re: Code Help with Flora & Maxbotix

by newbish1 on Sat Aug 10, 2013 4:02 pm

Thanks for the quick response Bill. And I appreciate the loop. I have the hardest time with those things.
Yeah, I have tested this and it does work returning an instantaneous response to the pixels. What I want is a delay from the sensor. I have modded a thingiverse item to hold the parts. https://tinkercad.com/things/lMtQ1vbMAHI-space-invader

I am really close to finishing and publishing my project to Instructables. my profile http://www.instructables.com/member/Grissini/

My problem is that when wearing this bow_tie, if myself or PT takes a sip of water or motions in front of the sensor briefly, the lights will flash. I want the code to take a reading at time A and then compare it to time B(5 seconds later) and THEN flash the lights. Blowing the surprise before its time, ruins the effect of the bright blinkies in someones face.

My guess is that I have to create new variables for time A and B, delay(5000), and then use if ( A >= B) {blink hard in someone face which is to close to mine}. I just don't know exactly how to do that, although I am willing to tough it out. But before I spend 10 hours fighting with code, I was hoping to find the right direction. I looked the Pumkin Code http://www.ladyada.net/make/pumpkin/pumpkin.html and this looks promising, but I think when I change the delay from 50 to 5000, I'll end up infinitely stalling the code.
Code: Select all | TOGGLE FULL SIZE
 distsensor = 0;
   for (i=0; i<8; i++) {
     distsensor += analogRead(0);
     delay(50);
   }
   distsensor /= 8;

   putstring_nl("Sensor = "); Serial.println(distsensor);


I haven't tried this code yet. Maybe it'll work if a take the loop from 8 to 100 or some other number that fits with the time delay I'm looking for. I don't really know though. Agian, thanks for the help and my hope is that this project can fit in well with the wearable, fun elements that Adafruit is currently fostering. Also this is my anti-project of element14-Get Closer. This projects goal is to keep people at a safe distance.
newbish1
 
Posts: 8
Joined: Fri Sep 30, 2011 9:47 am

Re: Code Help with Flora & Maxbotix

by adafruit_support_bill on Sat Aug 10, 2013 7:02 pm

As I understand it, what you want to do is to make sure that the proximity sensing event lasts for some minimum time period before you decide to take action.

Currently, you have this test:
Code: Select all | TOGGLE FULL SIZE
if (inchesAway < 19) { // if something is 24 inches away then make a 1khz sound


You need to add a time constraint to that:

Code: Select all | TOGGLE FULL SIZE
if (inchesAway < 19)  // if something is <= 19 inches away then see how long it stays there
{
    long startTime = millis(); // record the beginning of the proximity event.
    while (inchesAway < 19)
    {
         if ((millis() - startTime) >= 5000)  // If we have been in this loop for 5 seconds
         {
              break;  // exit the loop
         }
         inchesAway = analogRead(sonarPin) /2; // keep reading the sensor
     }
     if ((millis() - startTime) >= 5000)  // we passed the 5 second test
     {
          // Flash the leds
     }
}

User avatar
adafruit_support_bill
 
Posts: 29182
Joined: Sat Feb 07, 2009 9:11 am

Re: Code Help with Flora & Maxbotix

by newbish1 on Mon Aug 12, 2013 4:12 pm

thanks bill. That WORKED!
the sensor will now wait 5 seconds before it does anything. Your help is greatly appreciated. It's not perfect but I can live with that. The lights only flash on and off once rather than while(sensor<19 for 5 seconds) {flash leds}. I think I'm just going to loop the flashing enough times that it gets the point across. I tried changing some of the statement to WHILE's rather than IF's and it didn't worked. The light then never stopped flashing.
Thanks again, you've been a great help!
newbish1
 
Posts: 8
Joined: Fri Sep 30, 2011 9:47 am