Maxbotix EZ0 to RGB LED

/* 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);
}

RDS1_jun27d2ANALOG_pde.pde (1.82 KB)

Mark,

it's hard to help you here without you providing more data. The code looks ok and as if it was doing something. If that something is what you want or need is another matter.

I have this working to some degree, but it is very, very jumpy.

Well here we have the core of the problem. It could be many things.

First you should investigate the behaviour of sensorValue. If the input data itself is jumpy, expecting anything besides a jumpy output would be unreasonable. Also pay attention how well the range from sensorMin to sensorMax is used. If after your calibration sensorValue jumps mostly between too low and too high, you have a problem to work on. Also if the spread between sensorMin and sensorMax is below the output range of 256 levels, map() won't take advantage of the additional levels in between. You'll have to fix the jumpiness in other ways in this case.

Once you get reasonable smooth input values, you then can start working on the output side if the problem persists. Here again, you need to investigate what kind of jumpiness you see. You might end up to rework you mapping algorithm to something a little better tuned to your needs than just you the plain call to the map() function. But to know which algorithm works better, you need to understand the problem first.

Perhaps this gives you some ideas on how to proceed. If you have more information, we might be able to help you more.

Korman