for (int i =... easy peasy...

the theory is sound, but the application is beyond!

i have 16 sensors and leds and 16 independent brightness's

and a million lines of code, mostly repetition.

so im trying to use the for loop, which works fine inside the variable as it should, but i'm trying to get this working

set i to be between 1 and 16

variable called

variable1
variable2
variable3

to now be called

variable[ i ]

and then increase i

but i get all the errors about undefined variables, namely, error:'variable' was not declared in this scope

  for(int i = 1; i < 16; i++) {
static int lastBright[i] = 0;                                                    // set the lastbright variable
if (mappedValue[i]<sensitivity) {                                                // check to see if the ir is detecting above the base level 
      unsigned long currentMillis = millis();
      if (lastBright[i] > 0 && (currentMillis - previousMillis > interval))      // check to see if the last brightness is more than 0 and the interval time has passed
      lastBright[i] = lastBright[i]/100*fadespeed;                                 // Dim the light til it reaches 0
      previousMillis = currentMillis; 
}  else {
  lastBright[i] = maxBright;                                                     //  Set the brightness to full
}
    Tlc.set(led[i], lastBright[i]);                                                  // set the brightness of the led1
    Tlc.update();                                                                // send the data to the tlc
  }

[edited for italics!]

You need to post the full code.

the full code is massive, larger than this forum can accept, but i will add now and delete all commenting to get the size down

You can always attach it is a zip file.

You need to use arrays.

that'll save me a fortune in time! lol

irarduino2_7.ino (39.3 KB)

The part of the code you posted in your original post assumes variables such as lastBright, mappedValue and led are declared as arrays; they aren't.

//calibration routine
  int sensorValue1 = 0;         // the sensor value
  int sensorValue2 = 0;         // the sensor value
  int sensorValue3 = 0;         // the sensor value
  int sensorValue4 = 0;         // the sensor value
  int sensorValue5 = 0;         // the sensor value
  int sensorValue6 = 0;         // the sensor value
  int sensorValue7 = 0;         // the sensor value
  int sensorValue8 = 0;         // the sensor value
  int sensorValue9 = 0;         // the sensor value
  int sensorValue10 = 0;         // the sensor value
  int sensorValue11 = 0;         // the sensor value
  int sensorValue12 = 0;         // the sensor value
  int sensorValue13 = 0;         // the sensor value
  int sensorValue14 = 0;         // the sensor value
  int sensorValue15 = 0;         // the sensor value
  int sensorValue16 = 0;         // the sensor value

Should look like this if you want a single 16 element array:

int sensorValue[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

Keep in mind that the values indices for sensorValue would be 0 to 15, not 1 to 16 like your for loop uses.

static int lastBright[i] = 0;                                                    // set the lastbright variable

That is not the right way to declair an array.

But first use auto format to make it readable!. I for 1 will never download large files. Why not create a small sketch to show the problem you are having. Post the errors you are getting.

Mark

ahh, i get it now.

i thought the for i loop when set to 16, would set the (for instance)

setled[ i ], variable[ i ]

to setled1, variable1 on the first loop, and then settled, variable2 on the second loop

when actually, it just calls the figures from the previously set up arrays.

this actually is starting to make sense!

[edited for italics!]

say i set 4 variables with the for "i" loop, is it ok to keep the same "i" to do the "counting" / setting

or better practice to use i, j, k, l

i have seen loops where its like

for x +1, y - 1
x + y

which they HAVE to be different

  for (int i = 1; i < 16; i++) {
    sensorValue[i] = readMux(i-1);
    }

immediately followed by

  for (int i = 1; i < 32; i++) {
    sensorMin[i] = i;
    }

the for loop loops until the end of the sequence, which in the first instance is 16, and then continues to the second sequence and loops until 32 and then continues again. is this correct?

Array index runs from 0 to size-1. So for int myArray[16] the indexing is 0 to 15! not 1 to 16.

So the for loop should look like

for (1=0;i<16;i++){
}

There is no bounds checking in c/c++ so you will not be given any warning if you try to read/write outside the bounds to the array.

Writing out side the array will over write other data with random results

Mark

for 16 variables, then shouldn't the formula be'

for (i=0;i<15;i++){
}

being that 0 is inclusive?

No because "less than" 15 is 14, and you want to go from 0 to 15.

Your earlier posts are half in italics because [ i ] (without the spaces) is treated as italic by the forum. This doesn't add to the sense of the questions.

Try adding a space like I did there, then you don't see the italics and you do see what you are typing.

heh! yeah...

gone through the entire code, and its now compiling (and working) at 4700 instead of 9600! almost half the size.

is there any speed / efficiency difference to using array and for, barring the ease of reading?

Indexing into an array might possibly be slightly slower, but I wouldn't personally use lots of variables like this:

  int sensorValue1 = 0;         // the sensor value
  int sensorValue2 = 0;         // the sensor value
  int sensorValue3 = 0;         // the sensor value
  int sensorValue4 = 0;         // the sensor value
  int sensorValue5 = 0;         // the sensor value

There is the issue of fitting the code into the processor, for one thing, and readability and maintenance for another.

kelvinmead:
is there any speed / efficiency difference to using array and for, barring the ease of reading?

The reduced program code size and complexity greatly outweighs the negligible speed decrease.

sorry, tried posting with all the colour codes, as formatted for the forum, but too big!

/*

Kelvin Mead
May 2013

Interactive LED panels

Design for 4 LEDs, 4 IR sensors and 4 IR Emitters
Addition for 4067, 16 channel input expander
Addition for TLC5940, 16 channel pwm expander
Reesign for 16 LEDs, 16 IR sensors and 16 IR Emitters
Compressed code using arrays and for loops

*/

#include "Tlc5940.h"

// set up the arrays for later usage!
  
// set up the tlc pin assignments
  int led[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};

// calibration routine
  int sensorValue[16];
  int sensorMin[16] = {1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023};

// initiate mappedValue range  
  int mappedValue[16];  
  
// set up the lastBright variable array
  int lastBright[16];

//Mux control pins 4067
  int s0 = 2;
  int s1 = 4;
  int s2 = 7;
  int s3 = 8;

//Mux in "SIG" pin 4067
  int SIG_pin = 0;
  
// fade speed
// 1 is instant, 9 is slow (not slow enough though)
  int fadespeed = 99;
  
// this is for changing the fade time without using the delay command  
  long previousMillis = 0;
  // speed for the fades in milliseconds
  long interval = 50;
  
// full brightness  
// 4095 for the 5490
  int maxBright = 4095;

// variables for high and low led
  int sensitivity = 25;

void setup() {
// TLC5940
// Call Tlc.init() to setup the tlc.
//You can optionally pass an initial PWM value (0 - 4095) for all channels.
  Tlc.init();

// initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
    
// setup mux pins as outputs
  pinMode(s0, OUTPUT); 
  pinMode(s1, OUTPUT); 
  pinMode(s2, OUTPUT); 
  pinMode(s3, OUTPUT); 

// and set to low to initiate 
  digitalWrite(s0, LOW);
  digitalWrite(s1, LOW);
  digitalWrite(s2, LOW);
  digitalWrite(s3, LOW);
 
// turn on LED to signal the start of the calibration period:
// TLC5940 uses the 13 pin for data transfer
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);

// calibrate during the first 1 second 
  for (int i = 0; i < 16; i++) {
  while (millis() < 1000) {
    sensorValue[i] = readMux(i);
    if (sensorValue[i] < sensorMin[i]) {
    sensorMin[i] = sensorValue[i];
    }
  }
  }

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

void loop() {
    
// read sensor values
   for (int i = 0; i < 16; i++) {
   sensorValue[i] = readMux(i);
   } 
    
// apply the calibration to the sensor reading
   for (int i = 0; i < 16; i++) {
   mappedValue[i] = map(sensorValue[i], sensorMin[i], 1023, 0, 255);
   } 
   
// main detect, light and fade routine    
   for (int i = 0; i < 16; i++) {
   lastBright[i] = 0;                                                            // set the lastbright variable
   if (mappedValue[i]<sensitivity) {                                             // check to see if the ir is detecting above the base level 
      unsigned long currentMillis = millis();
      if (lastBright[i] > 0 && (currentMillis - previousMillis > interval))      // check to see if the last brightness is more than 0 and the interval time has passed
      lastBright[i] = lastBright[i]/100*fadespeed;                               // Dim the light til it reaches 0
      previousMillis = currentMillis; 
}  else {
  lastBright[i] = maxBright;                                                     //  Set the brightness to full
}
    Tlc.set(led[i], lastBright[i]);                                              // set the brightness of the led1
    Tlc.update();                                                                // send the data to the tlc
  }  
  
// print out the value you read: WARNING... activating this slows the entire program down
  for (int i = 0; i < 16; i++) {    
  Serial.print(i+1), Serial.print(":"),Serial.print(" Mux:");Serial.print(readMux(i)), Serial.print(" lastBright:"),Serial.print(lastBright[i]), Serial.print(" mappedValue:"), Serial.print(mappedValue[i]), Serial.print(" sensorMin:"), Serial.print(sensorMin[i]), Serial.print(" sensorValue:"), Serial.print(sensorValue[i]), Serial.print(" sensitivity:"), Serial.print(sensitivity); 
  Serial.println(" "); 
  }
// delay for the crack of it
  delay(5000);

}

// single definition code for the reading of the 16 channels from the 4067
// for some reason this bit of code is chucked at the end of the program
  int readMux (int channel) {
  digitalWrite(s3,channel & 8);
  digitalWrite(s2,channel & 4);
  digitalWrite(s1,channel & 2);
  digitalWrite(s0,channel & 1);
  return analogRead(SIG_pin);
  }

and the serial.output

1: Mux:348 lastBright:0 mappedValue:0 sensorMin:341 sensorValue:343 sensitivity:25 
2: Mux:334 lastBright:0 mappedValue:1 sensorMin:1023 sensorValue:333 sensitivity:25 
3: Mux:219 lastBright:0 mappedValue:1 sensorMin:1023 sensorValue:218 sensitivity:25 
4: Mux:157 lastBright:0 mappedValue:1 sensorMin:1023 sensorValue:157 sensitivity:25 
5: Mux:225 lastBright:0 mappedValue:1 sensorMin:1023 sensorValue:227 sensitivity:25 
6: Mux:195 lastBright:0 mappedValue:1 sensorMin:1023 sensorValue:197 sensitivity:25 
7: Mux:106 lastBright:0 mappedValue:1 sensorMin:1023 sensorValue:109 sensitivity:25 
8: Mux:190 lastBright:0 mappedValue:1 sensorMin:1023 sensorValue:191 sensitivity:25 
9: Mux:125 lastBright:0 mappedValue:1 sensorMin:1023 sensorValue:143 sensitivity:25 
10: Mux:133 lastBright:0 mappedValue:1 sensorMin:1023 sensorValue:130 sensitivity:25 
11: Mux:125 lastBright:0 mappedValue:1 sensorMin:1023 sensorValue:111 sensitivity:25 
12: Mux:132 lastBright:0 mappedValue:1 sensorMin:1023 sensorValue:101 sensitivity:25 
13: Mux:27 lastBright:0 mappedValue:1 sensorMin:1023 sensorValue:27 sensitivity:25 
14: Mux:31 lastBright:0 mappedValue:1 sensorMin:1023 sensorValue:30 sensitivity:25 
15: Mux:55 lastBright:0 mappedValue:1 sensorMin:1023 sensorValue:37 sensitivity:25 
16: Mux:70 lastBright:0 mappedValue:1 sensorMin:1023 sensorValue:33 sensitivity:25

the sensorMin is just pulling the original set values (1023) in the initial calibration routine, this means the wrong figures are being used in the mapped values.

i can see that the multiplexed values are coming in, but they are weird, if you cover the ir detectors as much as possible, then the initial values are higer, but they decay across the sensors, which looks more like its one sensor reporting the decaying figure of one sensor, not 16 sensors.

1: Mux:756 
2: Mux:867 
3: Mux:768 
4: Mux:193 
5: Mux:260 
6: Mux:303 
7: Mux:244 
8: Mux:461 
9: Mux:292 
10: Mux:271 
11: Mux:235 
12: Mux:222 
13: Mux:29 
14: Mux:36 
15: Mux:66 
16: Mux:82

can anyone see anything obvious that ive bucked up?

can anyone see anything obvious that ive bucked up?

The formatting,
for one thing.
It's nearly impossible to
read code that
jumps all over
the place.

And, given that there is an Auto Format option on the Tools menu, completely unacceptable to leave it that way.

There is NO reason to put more than one statement on each line. Scroll bars work just fine.

please find the auto-formatted code

/*

 Kelvin Mead
 May 2013
 
 Interactive LED panels
 
 Design for 4 LEDs, 4 IR sensors and 4 IR Emitters
 Addition for 4067, 16 channel input expander
 Addition for TLC5940, 16 channel pwm expander
 Reesign for 16 LEDs, 16 IR sensors and 16 IR Emitters
 Compressed code using arrays and for loops
 
 */

#include "Tlc5940.h"



// set up the arrays for later usage!

// set up the tlc pin assignments
int led[16] = {
  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};

// calibration routine
int sensorStart[16];
int sensorValue[16];
int sensorMin[16] = {
  1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023};

// initiate mappedValue range  
int mappedValue[16];  

// set up the lastBright variable array
int lastBright[16];

//Mux control pins 4067
int s0 = 2;
int s1 = 4;
int s2 = 7;
int s3 = 8;

//Mux in "SIG" pin 4067
int SIG_pin = 0;

// fade speed
// 1 is instant, 9 is slow (not slow enough though)
int fadespeed = 5;

// this is for changing the fade time without using the delay command  
long previousMillis = 0;
// speed for the fades in milliseconds
long interval = 20;

// full brightness  
// 4095 for the 5490
int maxBright = 4095;

// variables for high and low led
int sensitivity = 50;

void setup() {
  // delay for no reason
  delay(10);

  // TLC5940
  // Call Tlc.init() to setup the tlc.
  //You can optionally pass an initial PWM value (0 - 4095) for all channels.
  Tlc.init();

  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);

  // setup mux pins as outputs
  pinMode(s0, OUTPUT); 
  pinMode(s1, OUTPUT); 
  pinMode(s2, OUTPUT); 
  pinMode(s3, OUTPUT); 

  // and set to low to initiate 
  digitalWrite(s0, LOW);
  digitalWrite(s1, LOW);
  digitalWrite(s2, LOW);
  digitalWrite(s3, LOW);

  // new calibration 
  //  for ( int i = 0; i < 16; i++) {
  //    sensorStart[i] = readMux(i);
  //  }



  // turn on LED to signal the start of the calibration period:
  // TLC5940 uses the 13 pin for data transfer
  //  pinMode(13, OUTPUT);
  //  digitalWrite(13, HIGH);
  //  change to using the Tlc.init(100) to turn all leds on for the calibration part
  Tlc.init(100);

  // calibrate during the first 1 second 

  while (millis() < 1000) {
    for (int i = 0; i < 16; i++) {
      sensorValue[i] = readMux(i);
      if (sensorValue[i] < sensorMin[i]) {
        sensorMin[i] = sensorValue[i];
      }
    }
  }

  // signal the end of the calibration period
  Tlc.init();
}

void loop() {

  // read sensor values
  //   for (int i = 0; i < 16; i++) {
  //   sensorValue[i] = readMux(i);
  //   } 

  // apply the calibration to the sensor reading
  for (int i = 0; i < 16; i++) {
    //   mappedValue[i] = map(sensorValue[i], sensorMin[i], 1023, 0, 255);
    mappedValue[i] = map(readMux(i), sensorStart[i], 1023, 0, 255);
  } 

  // main detect, light and fade routine    
  for (int i = 0; i < 16; i++) {
    lastBright[i] = 0;                                                            // set the lastbright variable
    if (mappedValue[i]<sensitivity) {                                             // check to see if the ir is detecting above the base level 
      unsigned long currentMillis = millis();
      if (lastBright[i] > 0 && (currentMillis - previousMillis > interval))      // check to see if the last brightness is more than 0 and the interval time has passed
        lastBright[i] = lastBright[i]/100*fadespeed;                               // Dim the light til it reaches 0
      previousMillis = currentMillis; 
    }  
    else {
      lastBright[i] = maxBright;                                                     //  Set the brightness to full
    }
    Tlc.set(led[i], lastBright[i]);                                              // set the brightness of the led1
    Tlc.update();                                                                // send the data to the tlc
  }    

  // print out the value you read: WARNING... activating this slows the entire program down
  for (int i = 0; i < 16; i++) { 
    Serial.print(i+1), Serial.print(":"),Serial.print(" Mux:");
    Serial.print(readMux(i)), Serial.print(" lstBri:"),Serial.print(lastBright[i]), Serial.print(" mapVal:"), Serial.print(mappedValue[i]), Serial.print(" sensMin:"), Serial.print(sensorMin[i]), Serial.print(" sensVal:"), Serial.print(sensorValue[i]); 
    Serial.println();
  }
  Serial.println();


  // delay for the crack of it
  delay(5000);

}

// single definition code for the reading of the 16 channels from the 4067
// for some reason this bit of code is chucked at the end of the program
int readMux (int channel) {
  digitalWrite(s3,channel & 8);
  digitalWrite(s2,channel & 4);
  digitalWrite(s1,channel & 2);
  digitalWrite(s0,channel & 1);
  return analogRead(SIG_pin);
}