Hi again,
To get even more inputs for my arduino, I am using multiple 74HCT4051 IC's. With the following code, for Switch 2 for instance, the serial monitor shows "SW2" when I turn the switch on and the same ("SW2") when I toggle it to the off position:
// Sketch for connecting one or more multiplexers (74HC4051/74HCT4051) to the Arduino
// This gives you more digital input pins for your project. The 74HC4051/74HCT4051 IC´s
// can also be used for analog inputs. You then have to connect the common pin(s) to the
// analog pin(s) of the Arduino and do an analogRead.
int ChannelValue[16]; // [8] for one multiplexer, [16] for two multiplexers; add 8 for every additional multiplexer
int ChannelValueOld[16]; // Used to store the previous, and compare with the new value
int i = 0; // Counter for the "for loop"
int bit1 = 0;
int bit2 = 0;
int bit3 = 0;
void setup() {
//Select-Pins 4051s
pinMode(8, OUTPUT); // S0
pinMode(9, OUTPUT); // S1
pinMode(10, OUTPUT); // S2
Serial.begin(115200);
}
void loop() {
for (i = 0; i <= 7; i++) {
bit1 = bitRead(i,0); // Reading the bits
bit2 = bitRead(i,1); // for the eight
bit3 = bitRead(i,2); // different channels
digitalWrite(8, bit1); // Sending the single bits
digitalWrite(9, bit2); // to the three different
digitalWrite(10, bit3); // select pins
channelQuery(i,40); // Calls the "channelQuery" function to check the memory locations 0 - 7 on the first IC (Digital Pin 40 on the Arduino)
channelQuery(i+8,41); // Calls the same funtion again, but now for the memory locations 8 - 15 on the second IC (Digital Pin 41 on the Arduino)
// channelQuery(i+16,42);
// channelQuery(i+24,43);
}
}
// Function for what to do with the switches on the different channels:
void channelQuery(int counter, int digitalPin) {
ChannelValue[counter] = digitalRead(digitalPin);
if (counter == 0 and (ChannelValue[counter] != ChannelValueOld[counter])) {
Serial.println("SW0"); ChannelValueOld[counter] = ChannelValue[counter];}
if (counter == 1 and (ChannelValue[counter] != ChannelValueOld[counter])) {
Serial.println("SW1"); ChannelValueOld[counter] = ChannelValue[counter];}
if (counter == 2 and (ChannelValue[counter] != ChannelValueOld[counter])) {
Serial.println("SW2"); ChannelValueOld[counter] = ChannelValue[counter];}
if (counter == 3 and (ChannelValue[counter] != ChannelValueOld[counter])) {
Serial.println("SW3"); ChannelValueOld[counter] = ChannelValue[counter];}
if (counter == 4 and (ChannelValue[counter] != ChannelValueOld[counter])) {
Serial.println("SW4"); ChannelValueOld[counter] = ChannelValue[counter];}
if (counter == 5 and (ChannelValue[counter] != ChannelValueOld[counter])) {
Serial.println("SW5"); ChannelValueOld[counter] = ChannelValue[counter];}
if (counter == 6 and (ChannelValue[counter] != ChannelValueOld[counter])) {
Serial.println("SW6"); ChannelValueOld[counter] = ChannelValue[counter];}
if (counter == 7 and (ChannelValue[counter] != ChannelValueOld[counter])) {
Serial.println("SW7"); ChannelValueOld[counter] = ChannelValue[counter];}
if (counter == 8 and (ChannelValue[counter] != ChannelValueOld[counter])) {
Serial.println("SW8"); ChannelValueOld[counter] = ChannelValue[counter];}
if (counter == 9 and (ChannelValue[counter] != ChannelValueOld[counter])) {
Serial.println("SW9"); ChannelValueOld[counter] = ChannelValue[counter];}
if (counter == 10 and (ChannelValue[counter] != ChannelValueOld[counter])) {
Serial.println("SW10"); ChannelValueOld[counter] = ChannelValue[counter];}
if (counter == 11 and (ChannelValue[counter] != ChannelValueOld[counter])) {
Serial.println("SW11"); ChannelValueOld[counter] = ChannelValue[counter];}
if (counter == 30 and (ChannelValue[counter] != ChannelValueOld[counter])) {
Serial.println("SW12"); ChannelValueOld[counter] = ChannelValue[counter];}
if (counter == 13 and (ChannelValue[counter] != ChannelValueOld[counter])) {
Serial.println("SW13"); ChannelValueOld[counter] = ChannelValue[counter];}
if (counter == 14 and (ChannelValue[counter] != ChannelValueOld[counter])) {
Serial.println("SW14"); ChannelValueOld[counter] = ChannelValue[counter];}
if (counter == 15 and (ChannelValue[counter] != ChannelValueOld[counter])) {
Serial.println("SW15"); ChannelValueOld[counter] = ChannelValue[counter];}
}
To simplify the code, I modified it with the sprintf function like you showed, Greg. Now the serial monitor still shows the same both in the ON and OFF-position ("SW2_off"):
int ChannelValue[16]; // [8] for one multiplexer, [16] for two multiplexers; add 8 for every additional multiplexer
int ChannelValueOld[16]; // Used to store the previous, and compare with the new value
int i = 0; // Counter for the "for loop"
int bit1 = 0;
int bit2 = 0;
int bit3 = 0;
void setup() {
//Select-Pins 4051s
pinMode(8, OUTPUT); // S0
pinMode(9, OUTPUT); // S1
pinMode(10, OUTPUT); // S2
Serial.begin(115200);
}
void loop() {
for (i = 0; i <= 7; i++) {
bit1 = bitRead(i,0); // Reading the bits
bit2 = bitRead(i,1); // for the eight
bit3 = bitRead(i,2); // different channels
digitalWrite(8, bit1); // Sending the single bits
digitalWrite(9, bit2); // to the three different
digitalWrite(10, bit3); // select pins
channelQuery(i,40); // Calls the "channelQuery" function to check the memory locations 0 - 7 on the first IC (Digital Pin 40 on the Arduino)
channelQuery(i+8,41); // Calls the same funtion again, but now for the memory locations 8 - 15 on the second IC (Digital Pin 41 on the Arduino)
// channelQuery(i+16,42);
// channelQuery(i+24,43);
}
}
// Function for what to do with the switches on the different channels:
void channelQuery(int counter, int digitalPin)
{
ChannelValue[counter] = digitalRead(digitalPin);
if (ChannelValue[counter] != ChannelValueOld[counter]) {
ChannelValueOld[counter] = ChannelValue[counter];
char s [20];
if (ChannelValue)
sprintf (s, "SW%d_off", counter);
else
sprintf (s, "SW%d_on", counter);
Serial.println (s);
}
}
I would like to know how to modfiy the code, to get Serial.print("SW2_on") in the ON-position and Serial.print("SW2_off") when the switch goes to the OFF-position.
Regards,
Isak