I have a 74HC4067 multiplexer, a LIS3DH accelerometer (SPI), and a TCS34752 colour sensor (I2C). Right not I setup the Mux for only 11 analog sensors, but I may add more in the future.
I want to send out the sensor data to Max MSP with all the sensor data printed in one row (easiest way for Max to make sense of it). The way I have it, it works, just every few lines data is dropped and it only prints one or two numbers from the mux. I've taken a screen shot of it in Max for reference because it is easier to take screenshots with the max console than with the arduino serial monitor. But it looks the exact same the way the data gets printed in the serial monitor.
I think the problem is every once in a while the data starts to get sent out faster than the arduino can collect all the sensor data and put it together. I read that Serial.flush could help with this, but I'm not sure how to implement it. I started doing a trial and error with adding Serial.flush to different parts of the code, with no luck.
Or is there a better solution to my problem?
// 74HC4067 multiplexer demonstration (16 to 1)
//https://www.instructables.com/id/Tutorial-74HC4067-16-Channel-Analog-Multiplexer-De/
// control pins output table in array form
// see truth table on page 2 of TI 74HC4067 data sheet
// connect 74HC4067 S0~S3 to Arduino D7~D4 respectively
// connect 74HC4067 pin 1 to Arduino A0
byte controlPins[] = {B00000000,
B10000000,
B01000000,
B11000000,
B00100000,
B10100000,
B01100000,
B11100000,
B00010000,
B10010000,
B01010000,
/*B11010000,
B00110000,
B10110000,
B01110000,
B11110000*/ };
// holds incoming values from 74HC4067
//byte muxValues[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,};
byte muxValues[] = {0,0,0,0,0,0,0,0,0,0,0,};
#include "SparkFunLIS3DH.h"
#include "Adafruit_TCS34725.h"
#include "Wire.h"
#include "SPI.h"
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_24MS, TCS34725_GAIN_1X);
LIS3DH myAccel(SPI_MODE, 10); // constructed with parameters for SPI and cs pin number
void setup()
{
Serial.begin(9600);
DDRD = B11111111; // set PORTD (digital 7~0) to outputs
delay(1000); //relax...
if (tcs.begin()) {
Serial.println("Found TCS34725");
} else {
Serial.println("No TCS34725 found ... check your connections");
while (1);
}
myAccel.begin();
}
void setPin(int outputPin)
// function to select pin on 74HC4067
{
PORTD = controlPins[outputPin];
}
void displayData()
// dumps captured data from array to serial monitor
{
Serial.println();
//Serial.println("Values from multiplexer:");
//Serial.println("========================");
for (int i = 0; i < 11; i++)
{
//Serial.print("input I");
Serial.print(i);
//Serial.print(" = ");
Serial.print(muxValues[i]);
Serial.print(" ");
Serial.flush(); //this isn't helping????
}
}
void loop(){
for (int i = 0; i < 11; i++) {
setPin(i); // choose an input pin on the 74HC4067
muxValues[i]=analogRead(0); // read the value on that pin and store in array
Serial.print(" ");
Serial.flush(); //still not helping :(
}
// display captured data
displayData();
//Serial.print("Accel ");
Serial.print(myAccel.readFloatAccelX(), 2);
Serial.print(" ");
Serial.print(myAccel.readFloatAccelY(), 2);
Serial.print(" ");
Serial.print(myAccel.readFloatAccelZ(), 2);
Serial.print(" ");
uint16_t r, g, b, c, colorTemp, lux;
tcs.getRawData(&r, &g, &b, &c);
colorTemp = tcs.calculateColorTemperature(r, g, b);
colorTemp = tcs.calculateColorTemperature_dn40(r, g, b, c);
lux = tcs.calculateLux(r, g, b);
Serial.print(r, DEC);
Serial.print(" ");
Serial.print(g, DEC);
Serial.print(" ");
Serial.print(b, DEC);
Serial.print(" ");
Serial.flush(); //still not helping!!!! :(
Serial.println(" ");
delay(1000); //slow for troubleshooting purposes. Speed it up in real life
}
Edit: I think my screenshot didn't get posted properly. Here is the link: Troubleshooting - Album on Imgur