If you have LEDs and resistors available, I'd suggest wiring an LED with resistor to each digital output, INSTEAD of your relays. Get your code working with the LEDs, make sure it does what you want, THEN try adding the relays. If your code works with LEDs, but fails with Relays, you've pinpointed that the relays are the source of your problem, which is very likely to be the case. If you do it this way, you'll be more likely to believe that the relays are the problem, and we can then explain why this is so.
I'm actually using an external power supply to drive the relays.
However I'm using a USB cable to communicate the UNO board to the PC, since I need the state from the Switches to be retrieved by PD.
This makes me suspicious of either the relay circuit introducing a transient on operation, or incorrect/flaky button wiring. That's why I'm suggesting the LED circuits, if any 'wrong' behavior remains you can narrow down the problem.
@camsysca
ok
I'll try the LED thing
However, in regards that particular Relay Module, I might add I've tried a different Module with same result
Strange
It's an absolute Frankenstein made possible thanks to all of you who have helped.
Thanks a lot guys
I'm pasting the actual code below.
I've left Firmata aside, since I've found a more reliable way to interface with PD through Serial
const int buttonPin1 = 3; // Botón para optoacopladores 1 y 2
const int buttonPin2 = 4; // Botón para optoacopladores 3 y 4
const int num_of_digital_pins = 3;
int digital_values[num_of_digital_pins];
int buttonStateA = 0;
int buttonStateB = 0;
const int opto1Pin = 10; // Pin del primer optoacoplador
const int opto2Pin = 11; // Pin del segundo optoacoplador
const int opto3Pin = 12; // Pin del tercer optoacoplador
const int opto4Pin = 13; // Pin del cuarto optoacoplador
byte optoStates[4] = {LOW, LOW, LOW, LOW}; // Estados de los optoacopladores
byte prevButtonState1 = LOW; // Estado previo del botón 1
byte prevButtonState2 = LOW; // Estado previo del botón 2
unsigned long lastDebounceTime1 = 10; // Último tiempo de debounce para botón 1
unsigned long lastDebounceTime2 = 10; // Último tiempo de debounce para botón 2
const unsigned long debounceDelay = 20; // Tiempo de debounce
byte myArray[6]; // Array para almacenar los estados de los optoacopladores
void setup() {
Serial.begin(9600);
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(opto1Pin, OUTPUT);
pinMode(opto2Pin, OUTPUT);
pinMode(opto3Pin, OUTPUT);
pinMode(opto4Pin, OUTPUT);
}
void loop() {
for(int i = 0; i < num_of_digital_pins; i++) digital_values[i] = !digitalRead(i + 2);
Serial.print("Digital_values: ");
for(int i = 0; i < (num_of_digital_pins - 1); i++){
Serial.print(digital_values[i]);
Serial.print(" ");
}
Serial.println(digital_values[num_of_digital_pins - 1]);
byte buttonState1 = digitalRead(buttonPin1);
byte buttonState2 = digitalRead(buttonPin2);
// Cambiar estado de optoacopladores 1 y 2 con el botón 1
if (buttonState1 != prevButtonState1) {
if (buttonState1 == HIGH) {
optoStates[0] = !optoStates[0];
optoStates[1] = !optoStates[1];
}
prevButtonState1 = buttonState1;
delay (20);
}
// Cambiar estado de optoacopladores 3 y 4 con el botón 2
if (buttonState2 != prevButtonState2) {
if (buttonState2 == HIGH) {
optoStates[2] = !optoStates[2];
optoStates[3] = !optoStates[3];
}
prevButtonState2 = buttonState2;
delay (20);
}
digitalWrite(opto1Pin, optoStates[0]);
digitalWrite(opto2Pin, optoStates[1]);
digitalWrite(opto3Pin, optoStates[2]);
digitalWrite(opto4Pin, optoStates[3]);
}