// https://wokwi.com/projects/472618374755844097
// for
// https://forum.arduino.cc/t/i-need-some-assistance-with-changing-a-library-initialize-commandd/1455956
// cobbled together multiplexed
// started with this example
////// kludge area 1 for the '595 '595 faked mulitiplexer
void write16(unsigned int); // bits out to two '595s
unsigned int oneBit[] = {
0x0001, 0x0002, 0x0004, 0x0008,
0x0010, 0x0020, 0x0040, 0x0080,
0x0100, 0x0200, 0x0400, 0x0800,
0x1000, 0x2000, 0x4000, 0x8000
};
// code below the fold deals with the shift registers
// OE is the "real" CS input, the outputs are pulled high when disabled
////// end kludge area 1
# include <SPI.h>
# include <Wire.h>
# include <Adafruit_MAX31855.h>
#define DO 3
#define CS 4
#define CLK 5
// Ordinary Adafruit object
Adafruit_MAX31855 thermocouple(CLK, CS, DO);
const byte A0pin = A0;
const byte A1pin = A1;
const byte A2pin = A2;
const byte A3pin = A3;
// Our wrapper
class ThermocoupleMux {
public:
ThermocoupleMux(Adafruit_MAX31855 &tc,
byte a0, byte a1, byte a2, byte a3)
: tc(tc), a0(a0), a1(a1), a2(a2), a3(a3) {}
bool begin() {
pinMode(a0, OUTPUT);
pinMode(a1, OUTPUT);
pinMode(a2, OUTPUT);
pinMode(a3, OUTPUT);
select(0);
return tc.begin();
}
double readCelsius(byte channel) {
select(channel);
return tc.readCelsius();
}
double readInternal(byte channel) {
select(channel);
return tc.readInternal();
}
private:
void select(byte channel) {
write16(~oneBit[channel]);
/*
if a real mux or the 74HC4515 is used, here go the select bits
digitalWrite(a0, bitRead(channel, 0));
digitalWrite(a1, bitRead(channel, 1));
digitalWrite(a2, bitRead(channel, 2));
digitalWrite(a3, bitRead(channel, 3));
*/
}
Adafruit_MAX31855 &tc;
byte a0, a1, a2, a3;
};
// Give the wrapper the Adafruit object and the four 4515 address pins
ThermocoupleMux thermocouples(
thermocouple, A0pin, A1pin, A2pin, A3pin
);
void setup() {
Serial.begin(9600);
delay(500);
if (!thermocouples.begin()) { // all thermocouples, just one object
Serial.println("\nERROR.");
while (1) delay(10);
}
setup595();
Serial.println("\nDONE.");
}
void loop() {
static int tcIndex;
double c = thermocouples.readCelsius(tcIndex);
Serial.print("Thermocouple ");
Serial.print(tcIndex);
Serial.print(" Temp = ");
Serial.println(c);
delay(20);
if (++tcIndex >= 16) {
Serial.println("");
tcIndex = 0;
delay(1000);
}
}
# define DATA 8 // for DS of 74HC595
# define LATCH 9 // for STCP of 74HC595
# define CLOCK 10 // for SHCP of 74HC595
void setup595() {
pinMode(DATA, OUTPUT);
pinMode(CLOCK, OUTPUT);
pinMode(LATCH, OUTPUT);
write16(0);
}
void write16(unsigned int theBits) {
digitalWrite(LATCH, LOW);
for (uint8_t i = 0; i < 16; i++, theBits <<= 1) {
digitalWrite(CLOCK, LOW);
digitalWrite(DATA, (theBits & 0x8000) ? HIGH : LOW);
digitalWrite(CLOCK, HIGH);
}
digitalWrite(LATCH, HIGH);
}
/*
void clearRegisters() {
// Reset all register pins
for (int i = 0; i < 16; i++)
bits[i] = LOW;
}
void setRegisterPin(int index, int value) {
// Set an individual pin HIGH or LOW
bits[index] = value;
}
void writeRegisters() {
// Set and display registers
for (int i = 15; i >= 0; i--) {
digitalWrite(CLOCK, LOW);
digitalWrite(DATA, bits[i]);
digitalWrite(CLOCK, HIGH);
}
digitalWrite(LATCH, HIGH);
}
*/3
2
1
0
15
14
13
12