#define EN_S0_PIN 13 // New selection pins for EN_MUX
#define EN_S1_PIN 12
#define EN_S2_PIN 14
#define EN_S3_PIN 15
#define S0_PIN 26 // Selection pins for input multiplexers
#define S1_PIN 25
#define S2_PIN 33
#define S3_PIN 32
#define EN_MUX_PIN 27 // Enable pin for the EN_MUX
#define READ_PIN 35 // Common read pin
// The 2D array for one tray
int readings[4][9]; // 4 rows, 9 columns for each tray
void setup() {
// Initialize the control pins as outputs
pinMode(EN_S0_PIN, OUTPUT);
pinMode(EN_S1_PIN, OUTPUT);
pinMode(EN_S2_PIN, OUTPUT);
pinMode(EN_S3_PIN, OUTPUT);
pinMode(S0_PIN, OUTPUT);
pinMode(S1_PIN, OUTPUT);
pinMode(S2_PIN, OUTPUT);
pinMode(S3_PIN, OUTPUT);
pinMode(EN_MUX_PIN, OUTPUT);
// Initialize the read pin as an input
pinMode(READ_PIN, INPUT);
// Start serial communication
Serial.begin(9600);
}
void loop() {
for (int tray = 0; tray < 4; tray++) {
// Enable the correct input multiplexer for the current tray
setEN_MUX(tray);
// Read all sensors for the current tray
for (int row = 0; row < 4; row++) {
for (int column = 0; column < 9; column++) {
setInputMUX(row, column);
readings[row][column] = analogRead(READ_PIN);
// Add a small delay if needed to allow the signal to stabilize
delay(1);
}
}
// Disable the current input multiplexer
digitalWrite(EN_MUX_PIN, HIGH);
// Print the 2D array for the current tray
printTrayReadings();
// Add a space between trays
Serial.println();
// Delay between trays (optional)
delay(1000);
}
// digitalWrite(S0_PIN, LOW);
// digitalWrite(S1_PIN, LOW);
// digitalWrite(S2_PIN, LOW); // Note: Assuming 4 rows are encoded in S2 and S3
// digitalWrite(S3_PIN, LOW);
// digitalWrite(EN_S0_PIN, LOW);
// digitalWrite(EN_S1_PIN, LOW);
// digitalWrite(EN_S2_PIN, LOW);
// digitalWrite(EN_S3_PIN, LOW);
// // Enable the selected input multiplexer
// digitalWrite(EN_MUX_PIN, LOW);
// Serial.println(digitalRead(READ_PIN));
// Delay at the end of all tray readings (optional)
delay(5000); // 5 seconds before reading all trays again
}
void setEN_MUX(int tray) {
// Set the selection lines for the EN_MUX to select the correct input multiplexer
digitalWrite(EN_S0_PIN, (tray >> 0) & 1);
digitalWrite(EN_S1_PIN, (tray >> 1) & 1);
digitalWrite(EN_S2_PIN, (tray >> 2) & 1);
digitalWrite(EN_S3_PIN, (tray >> 3) & 1);
// Enable the selected input multiplexer
digitalWrite(EN_MUX_PIN, LOW);
}
void setInputMUX(int row, int column) {
// Set the selection lines for the input multiplexer
digitalWrite(S0_PIN, (column >> 0) & 1);
digitalWrite(S1_PIN, (column >> 1) & 1);
digitalWrite(S2_PIN, (row >> 0) & 1); // Note: Assuming 4 rows are encoded in S2 and S3
digitalWrite(S3_PIN, (row >> 1) & 1);
}
void printTrayReadings() {
for (int row = 0; row < 4; row++) {
for (int column = 0; column < 9; column++) {
Serial.print(readings[row][column]);
if (column < 8) {
Serial.print(", ");
}
}
Serial.println(); // End of row
}
}