non-volatile variables / "network theory... what?"

How would I go about creating a variable thats value is saved across power cycles, but still modifiable during execution?

The reason I ask is I want to add functionality to a device where I can send commands via serial, and have it change vars based on them. but remember the last values it was given even once powered off and on again. (smart thermostat with on/off temps that I want to change remotely via a serial connection)

Save the variable to EEPROM and read them again at startup.

could I have a more detailed description of how I would go about doing that? I have never used EEPROM (PROGMEM ?) at all, and I'm new to arduino, and control electronics , still learning.

I would only be storing/reading two int's if that helps.

Have a look at File/Examples/EEPROM in the IDE

EEPROM is not the same as PROGMEM

Thanks for the help fellas, a bit of poking around the EEPROM examples, and I think I have an idea what I'm doing now.

Now lets have some fun connecting 3 arduinos with one serial connection :smiley:

One more quick question, can you use one pin as both INPUT, and OUTPUT?
I want any arduino that wants to send data over serial to check to see if pin 2 is LOW, and if it is drive it HIGH while your sending data then LOW again. but if pin 2 is already HIGH (another device is transmitting), wait X mills and try again. all connected devices would common pin 2 together.

The idea here is to common most tx lines and run to one rx, and same with rt-tx. this would make one arduino the 'master' it can send and receive to/from all, but the slaves cant talk to each other, so if I want to send a message from slave one to slave 2, I have to send messages to the master, and have it relay them back to the appropriate slave

not a big deal if i'm going to have to common two pins together as in/out, but if I can use one pin and dynamically switch between in/out that leaves me one more pin to actually use.

the 5min post delay is really annoying for a 'newbie' I can't even edit a post I just made for 5 min...

You can change the pinMode() of a pin on the fly but I don't know whether the rest of you plan will work.

Darkassassin07:
One more quick question, can you use one pin as both INPUT, and OUTPUT?
I want any arduino that wants to send data over serial to check to see if pin 2 is LOW, and if it is drive it HIGH while your sending data then LOW again. but if pin 2 is already HIGH (another device is transmitting), wait X mills and try again. all connected devices would common pin 2 together.

Yes you can set any pin to input or output on the fly. One suggestion though: If you plan on "paralleling" two or more Arduino pins together, you MUST insure that any pin on one device is not an output while the other one is an output and each one trying to output the opposite logic level. That's called "contention" and it can burn out one or both ports.

Either your software MUST insure this doesn't happen OR ELSE connect the pins together through a resistor of around 100 ohms. A 100 ohm resistor is "stiff" enough to transmit the logic level where it needs to go but "loose" enough to limit the current if two pins try to fight.

Hope this helps.....

The best way I can think of dealing with that is to keep all the common slave pins (pin2 on each) as an input until I want to send data and have checked the pin state is LOW, then flip it to output and drive it HIGH.

Thanks, though I think I would have defaulted them to input anyway, that's really good to know. I probably would have burned at least one pin at some point and wouldn't know why.

Darkassassin07:
The best way I can think of dealing with that is to keep all the common slave pins (pin2 on each) as an input until I want to send data and have checked the pin state is LOW, then flip it to output and drive it HIGH.

Thanks, though I think I would have defaulted them to input anyway, that's really good to know. I probably would have burned at least one pin at some point and wouldn't know why.

Nope, can't do that. You have no guarantee that ONE CLOCK CYCLE after you check it to be OK, it goes active.

What you CAN do safely (aside from the resistors I told you about) is to simulate "open collector" outputs. Have every multi-Arduino pin connected together through a small diode (like a 1N4148 or 1N914) with one common pullup resistor. The idea is that the resistor pulls the node HIGH, and ANY Arduino, at ANY time, can safely pull is LOW )through the diode) without hurting any other pin.

Arduino1                     VCC
+------+  diode        10K    |
|      |---|<

---+---/\/\/\/---+

+------+        |
Arduino2        |
+------+  diode |
|      |---|<---+
+------+        |
Arduino3        |
+------+  diode |
|      |---|<---+
+------+

See?

Krupski:
What you CAN do safely (aside from the resistors I told you about) is to simulate "open collector" outputs. Have every multi-Arduino pin connected together through a small diode (like a 1N4148 or 1N914) with one common pullup resistor. The idea is that the resistor pulls the node HIGH, and ANY Arduino, at ANY time, can safely pull is LOW )through the diode) without hurting any other pin.

I believe this is how i2c works.

Ok, that makes alot of sense. Should I be putting diodes on the TX lines of the slaves too? they will all be commoned together into the RX of the master.

What do you think of the plan to common the RX of the slaves to the TX of the master? I'm not sure if the signal will make it to all the RX pins intact...

I'm trying to create a network of sorts between various nodes of a home-built smart home controller with minimal wiring. Id like to connect them with phone cables, so that gives me 4 wires between each device: RX,TX,Active, NC. If my plan won't work, can someone point me in a better direction?

It's all theory atm as I'm waiting for my second/third arduinos to get here.

Network theory is hard. Every time you think you've invented something, there's either a really good reason why it won't work reliably or that type of network already has a name.

I2C is good for multi-master communications. But it's bad at long distances. I've had problems with 10cm of wire.

SPI works at longer distances (metres) but it's best to do that with one master that polls the slaves to find out if they have data to send.

RS485 works well with multiple masters and it doesn't take too many wires. It can drive wires several kilometers in length.

CAN BUS is also good. Good for long distances (but not kilometers) and it allows for everyone to virtually all talk at the same time. Even though the Arduino Due and Teensy has CAN pins on the board, you still need specific driver chips to actually connect it to a CAN bus.

PaulMurrayCbr:
I believe this is how i2c works.

i2c may use open collector (open drain) signaling, but open drain connections are not i2c.....

(or did I misunderstand you?)

MorganS:
RS485 works well with multiple masters and it doesn't take too many wires. It can drive wires several kilometers in length.

This seems like my best option, but I'm having trouble figuring out how this system is connected. I'm pondering both full-duplex and half-duplex systems,but nothing i have found says how I'm actually converting serial to RS485 and back again...

What are the triangles? all the tutorials I have found are woefully incomplete in that aspect...

Click thru to the datasheet link, it's a little too big to attach here.

See Figure 1 for half duplex (simplex) wiring, only one transmitter can send at a time.
See Figure 2 for duplex, both sides can send at the same time.

The Arduino Serial pins connect to the "outside" of the transceiver (figure 1) or transmitter/receiver (figure 2).

Couple of App Notes too

Damn... network theory is complicated... I better get studying, this is gonna be harder than I thought.

Darkassassin07:
This seems like my best option, but I'm having trouble figuring out how this system is connected. I'm pondering both full-duplex and half-duplex systems,but nothing i have found says how I'm actually converting serial to RS485 and back again...

What are the triangles? all the tutorials I have found are woefully incomplete in that aspect...

That triangle symbol in electronics is usually an amplifier. In this case it's a line driver, which applies the proper voltage to the transmission lines. That voltage is much higher than the input, so it's an amplifier.

Any time you see an amplifier symbol like that, you can be sure that there are a lot of components "inside" the triangle. The datasheets for operational amplifiers always seem to show excessively complex circuits of transistors, as if that would help you use the amplifier better.

// OOPS, missed the second page with the reply above :frowning:

Triangles usually indicate some kind of amplifier; note that they do not necessarily have to amplify. In this case, the left amplifier has one input (which is asymmetrical) and a symmetrical output (two outputs with opposite signal), the right one has a symmetrical input (two inputs) and an asymmetrical output.

           +---+   +---+   +---+   +---+   +---+   +---+
           |   |   |   |   |   |   |   |   |   |   |
IN      ---+   +---+   +---+   +---+   +---+   +---+

        ---+   +---+   +---+   +---+   +---+   +---+
           |   |   |   |   |   |   |   |   |   |   |
OUT A      +---+   +---+   +---+   +---+   +---+   +---+

           +---+   +---+   +---+   +---+   +---+   +---+
           |   |   |   |   |   |   |   |   |   |   |
OUT B   ---+   +---+   +---+   +---+   +---+   +---+
        ---+   +---+   +---+   +---+   +---+   +---+
           |   |   |   |   |   |   |   |   |   |   |
IN A       +---+   +---+   +---+   +---+   +---+   +---+

           +---+   +---+   +---+   +---+   +---+   +---+
           |   |   |   |   |   |   |   |   |   |   |
IN B    ---+   +---+   +---+   +---+   +---+   +---+

           +---+   +---+   +---+   +---+   +---+   +---+
           |   |   |   |   |   |   |   |   |   |   |
OUT     ---+   +---+   +---+   +---+   +---+   +---+

I have ordered a few MAX483CPA transceiver's for this project. I decided to go with half-duplex to simplify wiring: A,B, and Active to prevent multiple drivers being active at once. the phone cables I'm using for network cable only has 4 wires, to go up to full-duplex I would need 6 to reliably prevent multiple active drivers (A,B,Z,Y, ActAB, ActZY)

In a few weeks I will either be back with a success story, or more questions :stuck_out_tongue:

Thanks for your help!