Go Down

Topic: 2x MEGA controlling a relay (Read 694 times) previous topic - next topic

septillion

Instead of answering the question (and possibly letting the OP dig a bigger hole, but that is their problem) you all wander off on some tangent rather than answer the question.
So you're an even more terrible reader then the OP than...

And yeah, peripherals can be an issue but I thing OP makes it way more complicated then it need to be by using two huge Mega's. Single Pro Mini + PCA9685 + mux is all you need :) Newbies tend to dig deeep deep wholes because they only see obvious but hard ways to do stuff.

And if you want to use the output of Arduino A to drive an input of Arduino B, add a resistor in series (10K+) to stop possible damage because of phantom powering.
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

adwsystems

Which is EXACTLY what I said in #1, with a link to 7432, 5 minutes after the OP posed the question, so if that's what you mean by
And THAT's what I said in #4, 30 minutes after the question was asked.
I know you did. I was just relisting all the options to skip over the tangent questions related to redesigning the OPs project.

..... I'm keen to see what a more direct route is.
Just answer the question. How many pins he is using on the Mega and the fact there are a ton of them is irrelevant. If he wants to use two Megas then that is his choice. Just stick to the question.

He didn't actually specify the sensors so I didn't know they are analog, and that's why I was asking how the pins are deployed.
With my experience, 9 of 10 times sensors are analog. There are analog sensors that provide a digital output due to internal thresholds, etc. and switches can be used as sensors and are 9 of 10 times digital (there are some devices with the word switch in the name that provide an analog output). With that background, if a question is posted and states 'sensors' then I interpret the layout to be using analog inputs. If that's wrong then the OP should have stated that fact in the first place and that's on them, not me.



adwsystems

So you're an even more terrible reader then the OP than...
Except that I'm not of the people the one delving into the OPs design choices and reworking their project.

And yeah, peripherals can be an issue but I thing OP makes it way more complicated then it need to be by using two huge Mega's. Single Pro Mini + PCA9685 + mux is all you need :) Newbies tend to dig deeep deep holes because they only see obvious but hard ways to do stuff.
I agree that is also a fine alternative, if he has the stuff to built it out and wire to it together. Two Megas and 2 4x relay boards takes nothing more than jumper wires to connect. The lowliest Arduino noob can do that.


LesserMole

If that's wrong then the OP should have stated that fact in the first place.
Pretty much my point.

Don't be so bloody grumpy: lighten up. If we want grump, we can go to stack exchange.




LesserMole

If he wants to use two Megas then that is his choice. Just stick to the question.
Next year it's 40 years since I graduated as an engineer. Engineering success is about peer review, questioning choices, testing. Even though OP is the design authority in this case, it behooves others to ask about choices.

That said, I actually don't give a rat's if OP buys 14 Megas, one for each sensor. I was more curious than anything....

I'm gone from this one. OP has my suggestions in #1 and #4, use them, don't use them.


toxsickcity

#20
Feb 16, 2018, 10:17 pm Last Edit: Feb 16, 2018, 10:30 pm by toxsickcity
Can anyone explain the compilations of the following

EG:
If I grab PIN 34 from MEGA1 and MEGA2 and wired it together to the relay.

Would the MEGAs work fine?

Would it actually work?
Could I use a signal diode to protect the megas if need be?

I don't have or gates.. sorry, thanks for the suggestion but I'm not going to buy add-on chips anymore as my last addition of TLC5940 ended terrible.. should stick to what I know. Also trying to get answers here sometimes is incredible hard and answers sometimes incredible vague for noobs.. sorry..

Gotta stick with what I know and my head shows using 2 megas is easier than working with a TLC to expand my pwms..

:( It's all a friggen mess in my head.

EDIT: relay board has an input for direct Arduino connection.

I like the suggestion to feed outputs of mega 1 to mega 2.. can I have a simple code to add that if I want to use one mega to detect another mega and output accordingly.

Such as, would I code it like this?
If digitalRead(25, HIGH)
digitalWrite (29, HIGH)
If digitalRead (25, LOW)
digitalWrite (29, LOW)

(I'd make 25 the readpin on mega 2) and sync output of pinn29 which mega 2 would be writing to to turn on relays.. just like mega 1
In sense I'd connect pin 29 on mega 1 to pin 25 on mega 2

Edit: my comment on answers coming from a noob is from other posts.. I'm sorry if it struck a cord.
I appreciate all answers and comments.. truly..

adwsystems

#21
Feb 16, 2018, 10:26 pm Last Edit: Feb 16, 2018, 10:31 pm by adwsystems
Maybe. You risk damaging the pin when configured for output by being connected to another output. We can't say about the diode as that will depend on whether the output to turn on the relay is 5V or 0V. There are relay modules out there that require 0V to be applied to activate the relay. (don't believe me, I have one I can sell you) There will also be a voltage drop (loss) across the diode, so you will risk not having enough voltage to activate the relay or input circuit. This is entirely dependent on the diode and relay your choose and have.

If you don't want to add an OR gate or transistors, and are set on having both programs identical. Then I would wire up two relay modules to act as the or gate. Guaranteed the Arduinos won't harm each other and will work.

adwsystems

Such as, would it be like this?
If digitalRead(29, HIGH)
digitalWrite (33, HIGH)
If digitalRead (29, LOW)
digitalWrite (29, LOW)
I think the last line should be digitalWrite (33, LOW)

That would read the input from the pin from the other Mega. What about the output requirement from "this" Arduino?

If digitalRead(29, HIGH) || (this mega HIGH)
digitalWrite (33, HIGH)
If digitalRead (29, LOW) && (this mega LOW)
digitalWrite (33, LOW)

toxsickcity

Your right.

The code we just spoke about would interfere with the main code in such if main code want to turn pin 29 on.. and new code wants 29 off! And vice versa Hahaha

If this can be solved by code would be great otherwise

What are those && and ||

Is it for creating or/and to a line of code?
Eg
Pin 29 && 30 would be pins 29 and 30
Pin 29 || 30 would be or?

I might make 4 new pins to truly activate the relays..35,36,37,38

Detect other Arduino on pins 25-29
Main code still trying to activate on 30-34
So if pin 25 & 30 are high = 35 high
If pin 25 or 30 are high = 35 high
If pin 25 or 30 are low = 35 high
If pin 25 & 30 are low = 35 low

adwsystems

#24
Feb 17, 2018, 12:09 am Last Edit: Feb 17, 2018, 12:23 am by adwsystems
What are those && and ||

Is it for creating or/and to a line of code?
Eg
Pin 29 && 30 would be pins 29 and 30
Pin 29 || 30 would be or?


As for this logic:
I might make 4 new pins to truly activate the relays..35,36,37,38

Detect other Arduino on pins 25-29
Main code still trying to activate on 30-34
So if pin 25 & 30 are high = 35 high
If pin 25 or 30 are high = 35 high
If pin 25 or 30 are low = 35 high
If pin 25 & 30 are low = 35 low
You haven't stated what the mapping is between the 14 sensors and the 4 relays. So no one can give you any specific help.

What I can say is the logic is that it doesn't make any sense. If Mega 1 pins 25-29 are the input pins connected to Mega 2 output pins, what are pins 30-34 doing, what are they connected to? Why is Mega 1 setting those pins? What are they connected to (obviously not the relays on pins 35-38)?

From another aspect, if you draw out the possible conditions for

If pin 25 & 30 are high = 35 high
If pin 25 or 30 are high = 35 high
If pin 25 or 30 are low = 35 high
If pin 25 & 30 are low = 35 low
you will see there is a lot of overkill.

A. The conditions "If pin 25 & 30 are high = 35 high" and "If pin 25 or 30 are low = 35 high" are covered by "If pin 25 or 30 are high = 35 high", so only the last is needed

B. The condition "If pin 25 & 30 are low = 35 low" is fine.

Hence why my pseudo code only showed the two line of code. That is all that is needed. Less is more.

toxsickcity

#25
Feb 17, 2018, 01:08 am Last Edit: Feb 17, 2018, 01:26 am by toxsickcity
Hi, adwsystems

thanks for the replies

I am writing this reply the 2nd time, I lost the first by clicking your name instead of right clicking hahaha!

anyways
I think its cool you can use the 2 lines of code instead of 4, eg.. correct me if wrong!?

If pin 25 & 30 are high = 35 high
If pin 25 or 30 are high = 35 high
If pin 25 or 30 are low = 35 high
If pin 25 & 30 are low = 35 low

is equal to

If pin 25 or 30 are high = 35 high
If pin 25 & 30 are low = 35 low

I dont see the logic of (25 or 30 are high = 35 high) allowing one of them to be low!? it's my lack of understanding of code!

anyways,

I want to know if I am overcomplicating things by adding pins 35-38

My system works as follows,
NTC thermistors are read, data converted to temps. all 7 sensors are in an ARRAY to give me highest temp.
The highest temp via if statements turns the relays on depending on heat. so
low heat no relays,
low-med - 1 relay
medium heat 2 relays,
med-high - 3 relays
high heat 4 relays on.

the relays ARE connected to pins 30-33 right now...

If I am going to add (&& / ||) to my code I dont know if I need to use pins 30-33 as a reference only and move the relays to pins 35-38.

so if it's ok to use pins 30-33 for the relays, I can write as follows.
If pin 25 or 30 are high = 30 high
If pin 25 & 30 are low = 30 low

as long as this code is ran after where I set the relays on or off via the temperatures, the new code (&&/||) it should rule and turn pin 30 high if pin 25 is high.... even is pin 30 via relays is low.

BUT... here where I dont know... would the next time the loop is ran.. if pin 30 is LOW at the if statement for thermal load. It would trip the relay and hammer it as it goes from the first if statement to the second?

unless I am suppose to include the && / || in the same statement..

here is my relay section..
Code: [Select]
unsigned int currentTemp;


    if (min < fan1SetPoint - deltaT) {
      digitalWrite(RELAY1, HIGH);
    }
    if (min < fan2SetPoint - deltaT) {
      digitalWrite(RELAY2, HIGH);
    }
    if (min < fan3SetPoint - deltaT) {
      digitalWrite(RELAY3, HIGH);
    }
    if (min < fan4SetPoint - deltaT) {
      digitalWrite(RELAY4, HIGH);
    }

    if (min > fan1SetPoint + deltaT) {
      digitalWrite(RELAY1, LOW);
    }
    if (min > fan2SetPoint + deltaT) {
      digitalWrite(RELAY2, LOW);
    }
    if (min > fan3SetPoint + deltaT) {
      digitalWrite(RELAY3, LOW);
    }
    if (min > fan4SetPoint + deltaT) {
      digitalWrite(RELAY4, LOW);
    }

adwsystems

Now that you have provided that detail it changes EVERYTHING.

The four input pins are not which fans to run, but how many fans to run

What happens if Mega 1 says to run 1 fans and Mega 2 says to run 2 fans? How many fans should be running?

toxsickcity

It's 1 fan at different speeds.. so if the code is the same

It will be the same relays at the same thermal output..

I use an array to combine all sensors..

adwsystems

What happens if Mega 1 says to run the fan on low and Mega 2 says to run 2 medium? What speed should be run?

toxsickcity

If mega 1 want low and mega 2 wants high = high (4x relay)
Is mega 1 want high and mega 2 wants low = high

If mega 1 want medium and mega 2 want low = medium (2x relay)
If mega 1 wants low and mega 2 wants medium = medium

As long as pin assignment does not change the relay 1-4 should stay the same..

Both megas will assign same relays for same speeds

Go Up