Hi there,
I have 2 Arduino Nanos that need to send potentiometer values to each other.
Arduino-A in reading value of potentiometer-A (analogRead), and sends this value to Arduino-B via TX/RX pins ((wired - short distance)). So Arduino-B can read this value and use it. Arduino-B is doing the same... reading value of potentiometer-B and sending this value to Arduino-A, this is happening simultaneous.
Below is a code I use for transmitting and receiving. It works fine for one side, but not properly if I use it for both sides.
I would be pleased if anyone could help me, or even send me a working code for both Arduinos. Code can use delay of ~20-500 ms, more is going to effect the project.
If this seems meaningless, it's a part of a bigger project.
Absolutely simultaneously does not work.
Can you explain how the Nanos are going to be scheduled ?
For example, does one wake up every X minutes, check its potentiometer and attempt to transmit its value to the other side ?
It does not need to work absolutely simultaneously, for example, one writes and sends the value to other and when the other receives and process this value it sends the value of its potentiometer back. So it separately transmits and receives value of potentiometer. This has to happen around every 20-500 ms.
OK. So the trigger is then the change in potentiometer value.
Device A notices that its potentiometer has changed, waits until a stable value has been obtained for X milliseconds, then transmits the results to device B. The same happens in the opposite direction.
How should device B react to a signal from device A ? Display something on an LCD screen or what ?
Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.
The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.
You can send data in a compatible format with code like this (or the equivalent in any other programming language)
Robin2:
The technique in the 3rd example will be the most reliable.
Robin, I tried and it receives value from potentiometer just fine, but how do I use that value in other parts of my program, for example in void loop?
I tried just with a LED (analogWrite LedPin,receivedChars), but it did nothing, LED was just ON.
And how do I separate, if I transmit 2 values at the same time, for example which value will control LED-1 and which LED-2?
I tried with just one, but something wasn't working
You should have come to the forum with this problem to be solved, rather than launching into a problematic architecture. Let us take a look at the original problem.
just_a_bubble:
Robin, I tried and it receives value from potentiometer just fine, but how do I use that value in other parts of my program, for example in void loop?
I tried just with a LED (analogWrite LedPin,receivedChars), but it did nothing, LED was just ON.
And how do I separate, if I transmit 2 values at the same time, for example which value will control LED-1 and which LED-2?
Dealing with the last question first, there is a parse example in my Tutorial that shows how to separate the values.
I suspect when you study that you will also see an answer to your first question.
just_a_bubble:
in short - two pins were effecting each other, even though they shouldn't have
Affecting. This is a classic X-Y problem. It's like "I wanted to get from London to Paris, but I couldn't get there for some reason, so I went to Easter Island because it was my last hope. Can you help me get from Easter Island to Paris?"
I might start a new topic on the original problem I had
Absolutely, yes. Go back to where you left the path and got lost in the woods.