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?