Unable to differentiate funduino keyes relays

Hi,

I have a problem I do not understand trying to operate 2 funduino keyes relays. I can operate both separately, but when I try to put them separately in the same sketch they both open or close at the same time. What I am actually trying to do is the following, in a permanent setup. I have as an input a source of 0.7 VAC (60 Hz), and I want to operate valves when the source is on. I plan to have a diode wired between the source and an arduino uno to keep only the positive part (1N4148 with 0.4V threshold voltage), and I use the relays to interrupt 120 VAC to the valves. As I have not my diodes yet I am testing my circuit by plugging 3.3V straight in the analog inputs. So here’s my code :

int switch1_pin = 3;
int switch2_pin = 6;
int input1 = A1;
int input2 = A3;
unsigned long t;
boolean state1;
boolean state2;
boolean state1_old = false;
boolean state2_old = false;

void setup() {
  pinMode(switch1_pin, OUTPUT);
  digitalWrite(switch1_pin, LOW);
  pinMode(switch2_pin, OUTPUT);
  digitalWrite(switch2_pin, LOW);
}

void loop() {
  state1 = false;
  state2 = false;
  t = millis();
  while(millis() < t+100){
    if (millis() < t) return;  //millis() reset condition (max once every 2^32 seconds with a lot of bad luck)
    
    if (analogRead(input1) >=100) state1 = true;
    if (analogRead(input2) >= 100) state2 = true;
   if (state1 && state2) break;
  }

  if (state1_old != state1) {
    digitalWrite(switch1_pin, state1);
    state1_old = state1;
  }
  if (state2_old != state2) {
    digitalWrite(switch2_pin, state2);
    state2_old = state2;
  }
}

When I test this code, at the moment I apply voltage on either analog pin 1 or 3, both relays switch at the same time, and the same occurs when I remove voltage (with sometimes a little delay). I’d appreciate if anyone can see my mistake or have an idea why this isn’t working.

Cheers!

Martin

Lets look at this loop:

  while(millis() < t+100){
    if (millis() < t) return;  //millis() reset condition (max once every 2^32 seconds with a lot of bad luck)
    
    if (analogRead(input1) >=100) state1 = true;
    if (analogRead(input2) >= 100) state2 = true;
   if (state1 && state2) break;
  }

The time tests are all wrong - the way you test for a timeout is this:

  while (millis() - t < 100)  // compare a time difference to a timeout delay, safe against wrap-around

  if (((long) (millis() - t)) < 0)  // force a subtraction, then cast to signed long in order to compare to zero.

The other obvious issue is that the loop termination condition is dependent on both state1 and state2 - perhaps
you mean to exit the loop when either has changed? Perhaps the test for state changes should be in the loop?
As it stands the code is not treating state1 and state2 independently.

First, thank you for your answer. For the timeout portion, after modification for your suggestions, the result is exactly the same, and I don't believe this was the problem.

Also, for the "if (state1 && state2) break;" part, it is unnecessary and only to accelerate the loop. As both analogReads are on AC signal, I want to sample over some time (here 100 ms) for each one of the inputs to intercept any voltage peak. Only when both are "true" I can break out the while loop as the remaining of the loop is useless, both peaks found. If I break out when the first is true, I may never find if the second one is also true (for example a dephased signal which I always sample when the first is high... possible, yet unlikely).

As for the test for state change, I hardly see how I can put it in the loop. As it is right now, the "true" state must be read after the while loop, or my valves are going to be shut each loop until it finds once again it has AC on input (for the reversed part of the AC, blocked by the diode, where analogRead == 0). The loop stands only for a time buffer on which I wait for at least one peak of current. Once it is seen, it can modify the state of the relays. if current peak -> power on, else -> power off.

I'm not clear what the meaning of your state variables is - if you want them to act independently then get rid of the while loop, loop() is already a loop.

Then maybe if you could hint me on solution paths it would help me. I don't think I could easily replace the actual structure. The reason for the two loops is simple : one controls the input sampling, while the other controls the relay state. I have to wait for the while loop to end before I can conclude the AC signal is off. If you look closer you will notice that each relay/state/read is independant within the loops.

When you're testing with only one analog pin connected to 3.3V, did you remember to ground the other one to avoid floating input issues?

trying to operate 2 funduino keyes relays.

Most of the relays sold by Keyes have active-low inputs. Do yours have optoisolators? Point to the actual parts online to get better help..

Thank you both for your answers. I had tested beforehand that they were active-high, but it would not matter to me if it was the opposite... I would just have had to wire the switches the other way around on the circuit. Anyway... the exact part is this one:http://dx.com/p/arduino-2-channel-relay-shield-module-red-144140 (a reviewer said it was actually opto-isolated, but appart from vaguely knowing the benefits I do not know what this info changes in my case).

But I think wildbill put the finger on my problem. When I wire the other pin to the ground, I can operate only one switch at a time. If I want this result in the final circuit, would I need a pull-up (or pull-down) resistor?

I'll have time to do some more testing on this one later this week, as I am overwhelmed with exams right now..

Thanks!

That is essentially identical to this: http://yourduino.com/sunshop2/index.php?l=product_detail&p=218 See the schematic...