so i think i have a pretty decent sketch that does what i need. to sum it up, the values from an infrared sensor are being averaged and then mapped to control pwm range of some LED's. as you increase, decrease, the leds crossfade between each other.
now my question: i want to simply trigger a 3rd LED to digital HIGH and set the other two crossfading LED's to LOW, but when i try this with my if statement, the mapping from sensor is causing confliction and in turn, causing the crossfade LEDS to flicker. suggestions? heres the code:
//Use of MaxSonar EZ1 sonar module in analog mode with arduino
// median filter of five consecutive readings to eliminate spikes
//
// Bill Gentles, Nov. 12, 2010
//borrowing an idea from the Arduino Forum on array sort
//http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1283456170/12
//the "insert sort" code is from whistler
//
//take 5 readings and store them in an array rangevalue[]
//using an "ascending insert sort" rearrange the array values in ascending order
//choose the middle element in the array, which will by definition be the median of the set of values
//
//Analog pin 1 for reading in the analog voltage from the MaxSonar device.
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int vals;
const int anPin = 1;
const int led1 = 10; // DIGITAL output pin that the LED is attached to
const int ledsignal = 13;
const int led2 = 9; // DIGITAL output pin that the LED is attached to
int sensorValue = 0; // value read from the pot
int outputValue1 = 0; // value output to the PWM
int outputValue2 = 0; // value output to the PWM
//variables needed to store values
int arraysize = 3; //quantity of values to find the median (sample size). Needs to be an odd number
int rangevalue[] = {0, 0, 0}; //declare an array to store the samples. not necessary to zero the array values here, it just makes the code clearer
//*********************************************************************************************
void setup()
{
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(ledsignal, OUTPUT);
//This opens up a serial connection to shoot the results back to the PC console
Serial.begin(9600);
printArray(rangevalue, arraysize);
//delay(10); //wait a while to open serial monitor window
}
//********************************************************************************
void loop()
{
// subtract the last reading:
total= total - readings[index];
// read from the sensor:
readings[index] = analogRead(anPin);
// add the reading to the total:
total= total + readings[index];
// advance to the next position in the array:
index = index + 1;
// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;
// calculate the average:
average = total / numReadings;
pinMode(anPin, INPUT);
vals = constrain(vals, 66, 490);
outputValue1 = map(vals, 66, 211, 0, 255);
outputValue2 = map(vals, 490, 241, 0, 255);
// change the analog out value:
analogWrite(led1, outputValue1);
analogWrite(led2, outputValue2);
//delay(1000);
if ( vals >= 212 && vals<=240 ){
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(ledsignal, HIGH);
}
else {
digitalWrite(ledsignal, LOW);
}
//MaxSonar Analog reads are known to be very sensitive. See the Arduino forum for more information.
//Maxbotix does not recommend averaging readings as the occasional value can be wildly inaccurate and skew the average
//A median sort is the preferred method.
for(int i = 0; i < arraysize; i++)
{ //array pointers go from 0 to 4
//Used to read in the analog voltage output that is being sent by the MaxSonar device.
//The MaxSonar Scale factor is (Vcc/512) per inch. A 5V supply yields ~9.8mV/in
//The Arduino will map input voltages between 0 and 5 volts into integer values between 0 and 1023.
//This yields a resolution between readings of: 5 volts / 1024 units or, .0049 volts (4.9 mV) per unit.
//Therefore, one unit from the arduino's ADC represents 0.5 inches
rangevalue[i] = analogRead(anPin);
// Serial.print("i, value ");
// Serial.print(i);
// Serial.print(" , ");
// Serial.print(rangevalue[i]);
// Serial.println();
//delay(1); //wait between analog samples
}
//Serial.print("unsorted ");
printArray(rangevalue, arraysize);
// Serial.println();
isort(rangevalue, arraysize);
//Serial.print("sorted ");
printArray(rangevalue, arraysize);
//Serial.println();
// now show the medaian range
int midpoint = arraysize/2; //midpoint of the array is the medain value in a sorted array
//note that for an array of 5, the midpoint is element 2, as the first element is element 0
// Serial.print("median range value ");
// Serial.print(rangevalue[midpoint]);
// Serial.println();
//Serial.println();
vals = rangevalue[midpoint];
//delay(1); //wait a while so you can read the values on the serial monitor
} //end of loop
//*********************************************************************************
// sort function
void isort(int *a, int n)
// *a is an array pointer function
{
for (int i = 1; i < n; ++i)
{
int j = a[i];
int k;
for (k = i - 1; (k >= 0) && (j < a[k]); k--)
{
a[k + 1] = a[k];
}
a[k + 1] = j;
}
}
//***********************************************************************************
//function to print array values
void printArray(int *a, int n)
{
for (int i = 0; i < n; i++)
{
// Serial.print(a[i], DEC);
//Serial.print(' ');
}
//Serial.println();
}