Greetings,
This is my first time posting here, and I have been working on a project for a few weeks now. Unfortunately, I am a bit stumped as I am experiencing issues with extra button pushes. For instance, when I press button 0, it triggers a button 8 press as well.
My project comprises of two sides: a server-side and a client-side. I am using a feather board with ethernet and a mux for the server-side, which will switch between inputs to a serial of another device (let's say LEDs for now). The server-side works fine, and I can send TCP commands to change the LEDs.
However, the problem lies on the client-side, which also uses the same hardware (feather board, ethernet, and mux). The idea is to have 10 buttons that will send commands to the server, essentially acting as a TCP remote control.
I have rewritten the code a few times in an attempt to narrow down the issue. I even removed the ezbutton library because I suspected it was buggy. However, the problem persists.
I would appreciate any help, and I am willing to pass along some coffee in exchange for assistance. Thank you.
#include <SPI.h>
#include <Ethernet.h>
const int muxSIG = A4;
const int muxS0 = A0;
const int muxS1 = A1;
const int muxS2 = A2;
const int muxS3 = A3;
void SetMuxChannel(byte channel) {
digitalWrite(muxS0, bitRead(channel, 0));
digitalWrite(muxS1, bitRead(channel, 1));
digitalWrite(muxS2, bitRead(channel, 2));
digitalWrite(muxS3, bitRead(channel, 3));
}
int buttonValues[16] = { 0 }; // initialize previous values to 0
void SetMuxChannelInt(int channel) {
const int controlPins[] = { muxS0, muxS1, muxS2, muxS3 };
const int muxChannel[16][4] = { { 0, 0, 0, 0 }, //1
{ 1, 0, 0, 0 }, //2
{ 0, 1, 0, 0 }, //3
{ 1, 1, 0, 0 }, //4
{ 0, 0, 1, 0 }, //5
{ 1, 0, 1, 0 }, //6
{ 0, 1, 1, 0 }, //7
{ 1, 1, 1, 0 }, //8
{ 0, 0, 0, 1 }, //9
{ 1, 0, 0, 1 }, //10
{ 0, 1, 0, 1 }, //11
{ 1, 1, 0, 1 }, //12
{ 0, 0, 1, 1 }, //13
{ 1, 0, 1, 1 }, //14
{ 0, 1, 1, 1 }, //15
{ 1, 1, 1, 1 } }; //16
for (int i = 0; i < 4; i++) {
digitalWrite(controlPins[i], muxChannel[channel][i]);
}
int value = digitalRead(muxSIG);
buttonValues[channel] = value;
}
const int serverPort = 4080;
byte mac[] = { 0x98, 0x76, 0xB6, 0x12, 0x0F, 0x84 };
IPAddress serverAddress(192, 168, 6, 150);
EthernetClient TCPclient;
void setup() {
pinMode(muxS0, OUTPUT);
pinMode(muxS1, OUTPUT);
pinMode(muxS2, OUTPUT);
pinMode(muxS3, OUTPUT);
pinMode(muxSIG, INPUT_PULLUP);
digitalWrite(muxS0, LOW);
digitalWrite(muxS1, LOW);
digitalWrite(muxS2, LOW);
digitalWrite(muxS3, LOW);
Ethernet.init(11);
Serial.begin(9600);
Serial.println("ARDUINO #1: TCP CLIENT");
IPAddress ip(192, 168, 6, 151);
IPAddress subnet(255, 255, 255, 0);
IPAddress gateway(192, 168, 6, 1);
IPAddress dns(192, 168, 6, 1);
Ethernet.begin(mac, ip, dns, gateway, subnet);
if (TCPclient.connect(serverAddress, serverPort)) {
Serial.println("Connected to TCP server");
} else {
Serial.println("Failed to connect to TCP server");
}
}
void loop() {
for (int i = 0; i < sizeof(buttonValues) / sizeof(buttonValues[0]); i++) {
SetMuxChannelInt(i);
int value = buttonValues[i];
if (value != 1) {
Serial.print("Value at channel ");
Serial.print(i);
Serial.print(" changed to: ");
Serial.println(value);
TCPclient.print(i);
}
}
}