Push button controlling optocoupler weird behavior

Dear @StefanL38

thanks for your help.

I can recall one of the many tests codes with some of that technique :


 byte buttonState1 = digitalRead(buttonPin1);
  byte buttonState2 = digitalRead(buttonPin2);

  // Manejo del debounce para botón 1
  if (buttonState1 != prevButtonState1) {
    lastDebounceTime1 = millis();
  }
  if ((millis() - lastDebounceTime1) > debounceDelay) {
    if (buttonState1 != prevButtonState1) {
      prevButtonState1 = buttonState1;
      if (buttonState1 == HIGH) {
        optoStates[0] = !optoStates[0];
        optoStates[1] = !optoStates[1];
      }
    }
  }

  // Manejo del debounce para botón 2
  if (buttonState2 != prevButtonState2) {
    lastDebounceTime2 = millis();
  }
  if ((millis() - lastDebounceTime2) > debounceDelay) {
    if (buttonState2 != prevButtonState2) {
      prevButtonState2 = buttonState2;
      if (buttonState2 == HIGH) {
        optoStates[2] = !optoStates[2];
        optoStates[3] = !optoStates[3];
      }
    }
  }

However I got similar results to the code implementing a delay at the end of the code.

Best,

Alexis

Dear @StefanL38 ,

thanks a lot for your time.

I'll try that and let you know.

Best,

Alexis

You can't power that relay board from an UNO, you need a separate power supply

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.

Dear @jim-p ,

thanks for your help.

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.

Best,

Alexis

Dear @camsysca ,

thanks again.
I'll do that and let you know.

Best,

Alexis

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

Thanks again

You should really test with the code-version that I posted and then
post what gets printed in the serial monitor

Dear @StefanL38
thanks again
I'll do that

I'm actually using an external power supply to drive the relays.

Is the relay board ground connected to the UNO GND?

@jim-p,

the relay board it's connected to the UNO GND
thanks

no testing with serial printing no advice

thanks for help @StefanL38

I believe I have finally got the code to work.

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]);

}

Dear @StefanL38
your debugging thing was really helpful
Thanks again

Dear @camsysca

I did the LED thing, it worked and helped me to keep going with the testings
thanks again

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.