Problem with defining digital pins by connecting two Arduino UNO boards

Hi, I’m currently working on a project based om using the arduino, in which I had to use two Arduino UNO’s due to less number of pins that I have been using Arduino, So I plan to connect two arduino’s together. Though I still can use Arduino Mega which has many than Arduino, since I have given my list as I’m using Arduino UNO for my project. Therefore I had to connect another arduino UNO in-order so i can use more digital pins.

The main problem after this is that I have connected two arduino UNO’s together and I wonder how can I define the second arduino UNO’s digital pins, as both of the arduino UNO got 1 - 14 digital pins.

I have got the programming to connect two arduino UNO’s

//---------------------------------------------//
//---------------------------------------------//
// Master Writer Code - Program for Arduino 1 //
//---------------------------------------------//
//---------------------------------------------//

#include <Wire.h>

void setup()
{
Wire.begin(); // Start I2C Bus as Master
}

byte x = 0;

void loop()
{

Wire.beginTransmission(9); // transmit to device #9
Wire.write(“x is”) // sends five bytes
Wire.send(x); // sends one byte
Wire.endTransmission(); // stop transmitting
x++;
delay(500);
}

//---------------------------------------------//
//---------------------------------------------//
// Slave Receiver Code - Program for Arduino 2 //
//---------------------------------------------//
//---------------------------------------------//

#include <Wire.h>

void setup()
{
Wire.begin(4); // join i2c bus with address #4
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output
}

void loop()
{
delay(100);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
while(1 < Wire.available()) // loop through all but the last
{
char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
}
int x = Wire.read(); // receive byte as an integer
Serial.println(x); // print the integer

//----------------------------------------------------------------//
//----------------------------------------------------------------//
//----------------------------------------------------------------//
//----------------------------------------------------------------//

Only I’m confused about defining pins, where when I connect any new components to the arduino
and wanted to define the new digital pins, I’m having trouble with defining the new digital pins

Would anyone can help on this please ? Urgent for my project in order to continue
much appreciated.

You need different code in each Arduinos. How are you expecting to communicate with them? Using two arduinos is almost always a very bad idea unless you know precisely what you are doing, which you don't.

Why not get more I/O pins with a port expander or shift register.

Though I still can use Arduino Mega which has many than Arduino, since I have given my list as I'm using Arduino UNO for my project.

This is a stupid restriction and has exactly no place in any engineering, this is not a game you know.

Of course I have used two different code codes One for the master arduino and the other for slave arduino. Why it' a bad idea, as I seen many project used two arduino's together.

Both arduino's have 20 digital pins. You can use the hardware serial port on the 'master' to talk to the outside world (i.e.USB) and use Software Serial on the 'master' to talk to Rx/Tx on the 'slave'.

Why it' a bad idea

Because the communications between the two are about ten times more complex than adding extra I/O. If you have trouble adding up to 20 then they are probably beyond you.

as I seen many project used two arduino's together

Where? Beginners often try this but it is seldom a good idea. The only way it could be viable is if the tasks are separate and asynchronous.

If you insist on using 2 Unos (in spite of all the good advice to the contrary) you should designate each of them to carry out separate parts of your project. It will be difficult to the point of impossible to treat them as a single larger Arduino.

If each of them has it's own task then the numbering of pins is easy to deal with.

As @Crossroads has said using SoftwareSerial for communication between the boards is almost certainly easiest. If you use SoftwareSerial on the two Arduinos you could do the development on the slave while it is connected to your PC which would make things a lot easier.

...R