I'm new beginner in programming arduino, and I want ask a question about arduino code, this is the problem " i used two arduino to build a robot, but i don't know how to set the code that for two arduino uno that pins in first arduino i used them all and now i wanna use the pins in second arduino, and how write code for the second arduino, which it is the same pin number i used in first arduino, how to creat a code? thanks a lot guys for answer my question... (sorry for my bad english)...
2 Answers
The notion that “both Arduinos are in the same sketch” is slightly off. Each of the Arduinos will contain a program; their programs might be identical if you do some fancy programming, but it is simpler to write one sketch per Arduino.
Thus, your first Arduino will run a sketch designed to control the devices it is connected to, and your second Arduino will run a sketch designed to control the devices that it's connected to. Conceivably, you might have some devices that both Arduinos connect to.
Calling the two sketches S1 and S2, the device on pin number n as referred to in S1 will typically be a different device than the one on pin number n as referred to in S2. Most but not all of the pin usage assignments for each pin number will be different in one sketch versus the other.
It's likely the two Arduinos will need to communicate with each other. For a range of methods, see for example How to have an Arduino communicate with 3 other Arduinos. For additional detail see Arduino-to-Arduino serial communication and How to network many Arduinos with sensors using I2C.
If you decide to use I2C, then also see MasterWriter Tutorial and Wire Library webpages at arduino.cc.
To expand on my comment, if you begin your sketch with conditional definitions, you can compile it for different sets of pin assignments just by changing the #define ARDUINONUMBER xxx
statement:
#define ARDUINONUMBER 1
#if ARDUINONUMBER == 1
#define FRONTMOTORPIN 7
#define REARMOTORPIN 8
#elif ARDUINONUMBER == 2
#define FRONTMOTORPIN 12
#define REARMOTORPIN 13
#else
#error Board number must be 1 or 2.
#endif
void setup(void)
{
pinMode(FRONTMOTORPIN, OUTPUT);
pinMode(REARMOTORPIN, OUTPUT);
; // etc
}
void loop(void)
{
;
}
#define ARDUINONUMBER 1
and#if ARDUINONUMBER == 1
,#else
, and#endif
can make the pin numbers change by changing just the#define
statement. If that isn't the problem, please explain more.