for loop pin mapping

im trying to correspond analog (inputs) to digital pwm pins using a for loop however I only seem to able to map one analog input to multiple pmw pins.

const int N_LEDS = 3;

const int numReadings = 250;

int readings[numReadings];      
int total = 0;             
int average = 0;               
int index =0;
int inputPin[] = {

int ledPin[]={

int minDistance = 2;        
int maxDistance = 50;

void setup()
  for (int thisReading = 0; thisReading < numReadings; thisReading++)
    readings[thisReading] = 0;          

void loop() {

  for (int i = 0; i < N_LEDS; i++ ) {

    total= total - readings[index];        
    readings[index] = analogRead(inputPin[i]);
    total= total + readings[index];        
    index = index + 1;                    

    if (index >= numReadings)              
      // ...wrap around to the beginning:
      index = 0;                          

    average = constrain(average, minDistance, maxDistance);     
    int outputValue = map(average, minDistance, maxDistance, 200, 0);

    average = total / numReadings;        

    analogWrite(ledPin[i], outputValue);


Return key got stuck, huh? Better get that fixed.

I don't see anything obviously wrong, although I'm a bit confused about what you expect to see.

What do you expect to see, and what do you actually get?

I expected for each input to coiside with a pwm output ie A0 would translate to Pwm 3 A1 to 5 ect. however A0 is the outputting to all pwm pins (long listing )

I didn't fully understand that. Perhaps display the inputs, and the outputs, and show some of that here.

ok I have 3 sensor (ez0 range finders) trying to control 3 lights however I have messed up some where in the code (more likely the way I have structured the code) as one sensor is controlling all 3 lights (the 3 outputs)

The code that calculates average looks muddled - you are constraining and scaling it before you have updated it with the new sample. The main problem I can see, though, is that you are interleaving samples from all three sensors in the same readings[numReadings] array so that the value you output for any sensor is actually the average of the last 250 readings from all sensors. You might want to have a separate history buffer for each sensor, although you need to keep an eye on your memory consumption.

If all you want is a decaying average of recent values, you can do this more easily just storing a single smoothed value per input and updating it with the weighted average of the previous smoothed value and the new sample.


This isn't printing what you think it is. You need to call Serial.println() with two calls to display two variables.