Hey folks -
I've been making some significant headway on my car-sensor input system, combining the arduino, the rPi, some 3d printing, and automotive wiring. For more on that, read here:
Anyhow, I'm running into some issues w/ my perf-board arduino, and I hope that the problem is in the code so it can be easily fixed.
With the two 4051 multiplexers, I'm simply looping through the channels, creating a name:value set, concatenating it all together, and then sending the combined string across the serial communication into the rPi.
It works. But as I'm testing this out with a single sensor at a time before wiring it all up, I'm running into some issues. Namely, I would expect that all of the sensors to read at "0" value when they are grounded w/ a 10k pull-down resistor. So if I just hook up a single sensor (let's say the Turbo boost sensor, which should provide a voltage of 0.5v+ to 4.5v+) while grounding all the other input wires, every sensor value ends up being 0, including the Turbo boost sensor. But if I unground everything and let it all float, every input goes kind of haywire, floating around and chasing whatever value I put into the Turbo boost sensor wire.
Does that make sense?
Anyhow, here's the code. Maybe the problem is in here. But maybe it's with the way the multiplexers are wired up? I currently have the power source of the arduino coming from the rPi USB, if that helps explain anything. Some of the sensors are labeled, and some are just labeled "sensor" until I figure out what's going to be sensed.
Thoughts?
int r0 = 0; // registers.
int r1 = 0;
int r2 = 0;
int count = 0;
int mplexes = 0; // 0 and 1; the number of multiplexers. Currently, there are 2.
float multiplexOutput = 0;
int incomingByte = 0;
int clientKeepAlive = 0;
float init_X; // X, Y, Z for the accelerometer
float init_Y;
float init_Z;
String label;
void setup() {
//4051 multiplexer digital control pins
pinMode(8, OUTPUT); // s0
pinMode(9, OUTPUT); // s1
pinMode(10, OUTPUT); // s2
Serial.begin(19200);
}
void loop() {
if (Serial.available() > 0) {
incomingByte = Serial.read();
if (incomingByte == 'y') {
clientKeepAlive = 1;
}
}
if (clientKeepAlive) {
// cycle through both multiplexers
for (mplexes = 0; mplexes <= 1; mplexes++) {
for (count = 0; count <= 7; count++) {
r0 = bitRead(count,0);
r1 = bitRead(count,1);
r2 = bitRead(count,2);
digitalWrite(8, r0);
digitalWrite(9, r1);
digitalWrite(10, r2);
multiplexOutput = analogRead(mplexes);
// GROUP 1
// 0: Fuel_Level
if (count == 0 && mplexes == 0) {
label="Fuel_Level: ";
multiplexOutput = map(multiplexOutput,1,1023,0,100);
}
// 1: WaterTemp
if (count == 1 && mplexes == 0) {
label="WaterTemp: ";
multiplexOutput = map(multiplexOutput,1,1023,0,100);
}
// 2: Oil_Pressure
if (count == 2 && mplexes == 0) {
label="Oil_Pressure: ";
multiplexOutput = map(multiplexOutput,1,1023,0,100);
}
// 3: Boost
if (count == 3 && mplexes == 0) {
label= "Boost: ";
float newValue = multiplexOutput * (5.00/1023.00) * 100;
multiplexOutput = newValue;
}
// GROUP 2
// 4: Fuel_Pressure
if (count == 4 && mplexes == 0) {
label="Fuel_Pressure: ";
multiplexOutput = map(multiplexOutput,1,1023,0,100);
}
// 5: Throttle Position Sensor
if (count == 5 && mplexes == 0) {
label="TPS: ";
multiplexOutput = map(multiplexOutput,1,1023,0,100);
}
// 6: Tachometer
if (count == 6 && mplexes == 0) {
label="Tachometer: ";
multiplexOutput = map(multiplexOutput,1,1023,0,100);
}
// 7: Speedometer
if (count == 7 && mplexes == 0) {
label="Speedometer: ";
multiplexOutput = map(multiplexOutput,1,1023,0,100);
}
// GROUP 3
// Accelerometer (8, 9, 10):
if (count == 0 && mplexes == 1) {
label="Accel_X: ";
}
if (count == 1 && mplexes == 1) {
label="Accel_Y: ";
}
if (count == 2 && mplexes == 1) {
label="Accel_Z: ";
}
// 11:
if (count == 3 && mplexes == 1) {
label="Sensor13: ";
multiplexOutput = map(multiplexOutput,1,1023,0,100);
}
// GROUP 4
// 12:
if (count == 4 && mplexes == 1) {
label="Sensor14: ";
multiplexOutput = map(multiplexOutput,1,1023,0,100);
}
// 13:
if (count == 5 && mplexes == 1) {
label="Sensor15: ";
multiplexOutput = map(multiplexOutput,1,1023,0,100);
}
// 14:
if (count == 6 && mplexes == 1) {
label="Sensor16: ";
multiplexOutput = map(multiplexOutput,1,1023,0,100);
}
// 15:
if (count == 7 && mplexes == 1) {
label="Sensor17: ";
multiplexOutput = map(multiplexOutput,1,1023,0,100);
}
Serial.print(label); Serial.println(multiplexOutput,0);
}
}
Serial.println("*");
clientKeepAlive = 0;
}
}