Show Posts
Pages: [1]
1  Topics / Interactive Art / Maxbotix EZ0 to RGB LED on: June 27, 2011, 03:51:28 pm
/* So the project is that the art object: a sunflower-like plant made of PVC, has a ton of RGB LEDS inside of it.  I have a bunch of Maxbotix EZ0's as sensors.
*/

I want the EZ0's to read in either Analog or PWM for the distance of the viewer and then convert the info to the RGB LEDs. I want the undetected state to be Green (0,255,0) and the reading that is within 2 feet or so of the art object to be Red (255,0,0).

I have this working to some degree, but it is very, very jumpy.  I JUST WANT TO SMOOTH OUT THE FADING OF THE RGB LED FROM THE RAW AND JUMPY NUMBERS COMING FROM THE EZ0 (sonar Ranger). I'm not sure what the best solution would be or how to go about it.  perhaps more segmented values? maybe every 50 units would fade in value ie: if EZ0 = 87, then (100,150,0) would fade towards (50,200,0)?

I've used here is an Analog version, so I could calibrate the EZ0.  I also have an Analog version with an averaging function (but it didn't seem to help because of the millisecond gap).  Also a PWM, if you would prefer that version, but again not as accurate and I couldn't figure out how to calibrate it.

Thank you for your consideration, i look forward to your response and I will of course credit you to the code's contributor's list.
Mark.

__________________________________________________________________________________________________________________________________________________________________________________________________________________________
                                                                     code pasted below
__________________________________________________________________________________________________________________________________________________________________________________________________________________________
/*
Based on:
"Calibration"
 created 29 Oct 2008
 By David A Mellis
 Modified 4 Sep 2010
 By Tom Igoe
 */

const int sensorPin = A0;    // pin that the sensor is attached to
int redPin   = 9;   // Red LED,   connected to digital pin 9
int greenPin = 10;  // Green LED, connected to digital pin 10
int bluePin  = 11;  // remains 0 throughout the program

int Rval = 0;    //intended to be directly proporional to "inches"
int Gval = 255;  // intended to be default and inversely proportional to "inches"
int Bval = 0;    // remains 0 throughout the program

// variables:
int sensorValue = 0;         // the sensor value
int sensorMin = 1023;        // minimum sensor value
int sensorMax = 0;           // maximum sensor value


void setup() {
  // turn on LED to signal the start of the calibration period:
  pinMode(redPin,   OUTPUT);   // sets the pins as output
  pinMode(greenPin, OUTPUT);   
  pinMode(bluePin,  OUTPUT);
  digitalWrite(10, HIGH);

  // calibrate during the first five seconds
  while (millis() < 5000) {
    sensorValue = analogRead(sensorPin);

    // record the maximum sensor value
    if (sensorValue > sensorMax) {
      sensorMax = sensorValue;
    }

    // record the minimum sensor value
    if (sensorValue < sensorMin) {
      sensorMin = sensorValue;
    }
  }

  // signal the end of the calibration period
  digitalWrite(10, LOW);
}

void loop() {
  // read the sensor:
  sensorValue = analogRead(sensorPin);

  // apply the calibration to the sensor reading
  sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);

  // in case the sensor value is outside the range seen during calibration
  sensorValue = constrain(sensorValue, 0, 255);

  // fade the LED using the calibrated value:
   
  analogWrite(redPin, 255  - sensorValue);
  analogWrite(greenPin, sensorValue);
  analogWrite(bluePin, 0);
 delay(15);
}
Pages: [1]