hi there,
I know this is not strictly related to the topic of this sub, but I think this might be the best place to ask this question anyways.
I have this sketch that is reading the colour input of 24 APDS9960 sensors via 4 multiplexers:
/**/
#include<Wire.h>
#include "Adafruit_APDS9960.h" /*include libraries*/
/***********************************************************************************************************************************************************************************************************************************************************/
// declarations
Adafruit_APDS9960 apds[1]; /*initialize sensor*/
byte tcaI2CAddress[] = {0x70, 0x71, 0x72, 0x73}; /*define array containing all the addresses*/
byte numberOfTCAs = 4; /* define number of TCAs used */
byte numberOfDevicesPerTCA = 6; /* define number of Sensors per TCA */
const int numberOfDevices = 24; /* define number of total devices */
/***********************************************************************************************************************************************************************************************************************************************************/
void setup() {
Wire.begin();
Serial.begin(115200); /*begin serial communication (Arduino -> PC) at 115200 Baud rate*/
// 0. run i2c scanner
Serial.println("I2C Scanner ready!");
Serial.println();
scanI2C(400000); /*run i2c scanner function*/
Serial.println("-------------------------------");
Serial.println();
// 1. switch off all TCAs to make sure no two TCAs are active at the same time
for (int i = 0; i < numberOfTCAs; i++){
// Wire.beginTransmission(tcaI2CAddress[i]);
// Wire.write(0);
// Wire.endTransmission();
closeTCA(i);
}
// 2. initialize sensors
for (int i = 0; i < numberOfDevices; i++){
byte tca = setTCAAndChannel(i);
Serial.print("Initializing APDS_");
Serial.println(i);
Serial.println();
if(!apds[i].begin()){ /*if function returns 0, print "failed to initialize...", else print "initialized!"*/
Serial.println("failed to initialize device! Please check your wiring.");
Serial.println();
}
else {Serial.println("Device initialized!");
Serial.println();
}
apds[i].enableColor(true); /*enable color sensing mode*/
// 3. close TCA
closeTCA(tca);
}
}
void loop() {
// 1. switch off all TCAs to make sure no two TCAs are active at the same time
for (int i = 0; i < numberOfTCAs; i++){
// Wire.beginTransmission(tcaI2CAddress[i]);
// Wire.write(0);
// Wire.endTransmission();
closeTCA(i);
}
// 2. run color readings
for (int i = 0; i < numberOfDevices; i++){
byte tca = setTCAAndChannel(i);
uint16_t r, g, b, c; /*create some variables to store the color data in*/
while(!apds[i].colorDataReady()){ /*wait for color data to be ready*/
delay(1); /*Adafruit example: 5ms; THIS DELAY ALONE WOULD AMOUNT TO 125 ms !!!!!*/
}
apds[i].getColorData(&r, &g, &b, &c); /*get the data and print the different channels*/
int cl = map(c, 0 , 4097, 0, 255); /*map values 0-4097 to 0-255 to send less data (and because sensor outputs 0-4097 instead of 0-4096, so it's not possible to divide by 16 - maybe better to just clip?*/
Serial.print(i);
Serial.print(" ");
Serial.println(cl);
// 3. close TCA
closeTCA(tca);
}
// Serial.println("****************************");
delay(1);
}
/**************************************************************************************************************************************************************************************************************************************************************/
byte setTCAAndChannel(byte i){ /* define function for setting TCA and sensor */
byte tca = i/numberOfDevicesPerTCA; /* divide i (total # of devices) by # of TCAs, that way because it's int, 1,5 will still be 1, thereby selecting the correct TCA */
byte channel = i%numberOfDevicesPerTCA; /* modulo... divide i (the total number of devices) by the # of devices per TCA and give the rest, e.g. 2%4 = 0*/
Wire.beginTransmission(tcaI2CAddress[tca]);
Wire.write(1 << channel);
Wire.endTransmission();
return tca;
}
void closeTCA(byte tca){
Wire.beginTransmission(tcaI2CAddress[tca]);
Wire.write(0);
Wire.endTransmission();
}
void scanI2C(long frequency){
String normal = "standard mode (100 kHz):";
String fast = "fast mode (400 kHz):";
String defaultStr = " !!!!! Invalid Frequency !!!!!";
bool error = true;
bool addressFound = false;
Serial.print("Scanning in ");
switch(frequency){ /* switch...case is similar to an if...then statement */
case 100000:
Serial.println(normal); /*print string "standard mode (100kHz)", previously defined as 'normal'*/
break;
case 400000:
Serial.println(fast); /*print string "fast mode (400kHz)", previously defined as 'fast'*/
break; /*ends the switch...case statement*/
}
Wire.setClock(frequency);
for(int i=1; i<128; i++){
Wire.beginTransmission(i); /*begins transmission on the specified address i, e.g. 127*/
error = Wire.endTransmission(); /*Wire.endTransmission() returns bytes for status of transmission: 0 = success, 1 = data too long, ... 4 = other ; here "error" is set to 0 or "false" if Wire.endTransmission() returns 0 (= transmssion successful) and "true" for everything else (1 - 4)*/
if(error == 0){ /*if the "error" Boolean is set to "false" (meaning the transmission was successful), proceed by ... */
addressFound = true; /*setting "addressFound" to true*/
Serial.print("0x"); /*prints the address of the i2c device from two components, first "0x" ... */
Serial.println(i,HEX); /* ... and second the number where a device was found as HEX, i.e. 127 = 7F, totalling 0x7F*/
}
}
if(!addressFound){ /*if no address was found for all 128 addresses, print: */
Serial.println("No address recognized");
}
Serial.println();
}
I've tested it on a breadboard with all 24 sensors, the complete layout, on an Arduino Nano Every, and it works exactly as intended. I had absolutely no quarrels with it.
Now I've begun soldering my circuit and tested it with four muxers and one sensor each for starters, but for some reason, the loop doesn't execute and I'm not receiving any error messages either. It's simply not working. and interrupting in the middle of the setup sequence.
Has anyone ever experienced something similar? I've checked all the wires and they are soldered correctly. Any pointers what could be wrong here? A cold solder? anything?
I'm attaching a screenshot to show what the interrupted setup sequence looks like. the error message showed up also when the sketch did work during the breadboard trials that I did, I think it's related to the ATmega328 emulator on the Nano Every, so I don't think it's relevant here.
thanks, and best