Arduino to Arduino Serial Communication

Thread Starter

Mehroz Irshad

Joined Dec 10, 2015
6
Hi all.
i am trying to connect two arduinos using serial wired communication.
Arduino1 will be interfaced with a 4 x 3 keypad and Arduino2 will be interfaced with a seven segment display.
the key pressed on keypad must be displayed on seven segment display.
but i am unable to work it out please see what's wrong.
code for arduino1


//connected the pin of keypad as follows
int row_1=6;
int row_2=7;
int row_3=8;
int row_4=9;
int col_1=10;
int col_2=11;
int col_3=12;

void setup()
{
Serial.begin(9600);
// set row as a output and coloum as a input
pinMode(row_1,OUTPUT);
pinMode(row_2,OUTPUT);
pinMode(row_3,OUTPUT);
pinMode(row_4,OUTPUT);
pinMode(col_1,INPUT);
pinMode(col_2,INPUT);
pinMode(col_3,INPUT);
}
void loop()
{
int val;
//setting the columns as high initially
digitalWrite(col_1,HIGH);
digitalWrite(col_2,HIGH);
digitalWrite(col_3,HIGH);
//checking everycondition one by one

digitalWrite(row_1,LOW);
digitalWrite(row_2,HIGH);
digitalWrite(row_3,HIGH);
digitalWrite(row_4,HIGH);
//checking each column for row1
if(digitalRead(col_1)==0)
{
Serial.print(1);
}
else if(digitalRead(col_2)==0)
{
Serial.print(2);
}
else if(digitalRead(col_3)==0)
{
Serial.print(3);
}
//case 2:
digitalWrite(row_1,HIGH);
digitalWrite(row_2,LOW);
digitalWrite(row_3,HIGH);
digitalWrite(row_4,HIGH);
//checking each column for row2
if(digitalRead(col_1)==0)
{
Serial.print(4);
}
else if(digitalRead(col_2)==0)
{
Serial.print(5);
}
else if(digitalRead(col_3)==0)
{
Serial.print(6);
}
//case 3:
digitalWrite(row_1,HIGH);
digitalWrite(row_2,HIGH);
digitalWrite(row_3,LOW);
digitalWrite(row_4,HIGH);
//checking each column for row3
if(digitalRead(col_1)==0)
{
Serial.print(7);
}
else if(digitalRead(col_2)==0)
{
Serial.print(8);
}
else if(digitalRead(col_3)==0)
{
Serial.print(9);
}
//case 4:
digitalWrite(row_1,HIGH);
digitalWrite(row_2,HIGH);
digitalWrite(row_3,HIGH);
digitalWrite(row_4,LOW);
//checking each column for row4
if(digitalRead(col_1)==0)
{
Serial.print(10);
}
else if(digitalRead(col_2)==0)
{
Serial.print(0);
}
else if(digitalRead(col_3)==0)
{
Serial.print(11);
}
delay(100);
}

code for arduino 2


int a=2;
int b=3;
int c=4;
int d=5;
int e=6;
int f=7;
int g=8;
int h=9;

void setup()
{
pinMode(a, OUTPUT);
pinMode(b, OUTPUT);
pinMode(c, OUTPUT);
pinMode(d, OUTPUT);
pinMode(e, OUTPUT);
pinMode(f, OUTPUT);
pinMode(g, OUTPUT);
pinMode(h, OUTPUT);
digitalWrite(h, 0); // start with the "dot" off
}

void loop()
{
if(Serial.available())
{

int letter = Serial.read();
if(letter==9)
{
digitalWrite(a, 1);
digitalWrite(b, 1);
digitalWrite(c, 1);
digitalWrite(d, 0);
digitalWrite(e, 0);
digitalWrite(f, 1);
digitalWrite(g, 1);

}
else if(letter==8)
{
// write '8'
digitalWrite(a, 1);
digitalWrite(b, 1);
digitalWrite(c, 1);
digitalWrite(d, 1);
digitalWrite(e, 1);
digitalWrite(f, 1);
digitalWrite(g, 1);

}
else if(letter == 7)
{
// write '7'
digitalWrite(a, 1);
digitalWrite(b, 1);
digitalWrite(c, 1);
digitalWrite(d, 0);
digitalWrite(e, 0);
digitalWrite(f, 0);
digitalWrite(g, 0);

}
else if(letter == 6)
{
// write '6'
digitalWrite(a, 1);
digitalWrite(b, 0);
digitalWrite(c, 1);
digitalWrite(d, 1);
digitalWrite(e, 1);
digitalWrite(f, 1);
digitalWrite(g, 1);

} else if(letter == 5)
{ // write '5'
digitalWrite(a, 1);
digitalWrite(b, 0);
digitalWrite(c, 1);
digitalWrite(d, 1);
digitalWrite(e, 0);
digitalWrite(f, 1);
digitalWrite(g, 1);

}else if(letter == 4)
{ // write '4'
digitalWrite(a, 0);
digitalWrite(b, 1);
digitalWrite(c, 1);
digitalWrite(d, 0);
digitalWrite(e, 0);
digitalWrite(f, 1);
digitalWrite(g, 1);

}else if(letter == 3)
{ // write '3'
digitalWrite(a, 1);
digitalWrite(b, 1);
digitalWrite(c, 1);
digitalWrite(d, 1);
digitalWrite(e, 0);
digitalWrite(f, 0);
digitalWrite(g, 1);

}else if(letter == 2)
{ // write '2'
digitalWrite(a, 1);
digitalWrite(b, 1);
digitalWrite(c, 0);
digitalWrite(d, 1);
digitalWrite(e, 1);
digitalWrite(f, 0);
digitalWrite(g, 1);

}else if(letter == 1)
{ // write '1'
digitalWrite(a, 0);
digitalWrite(b, 1);
digitalWrite(c, 1);
digitalWrite(d, 0);
digitalWrite(e, 0);
digitalWrite(f, 0);
digitalWrite(g, 0);

}else if(letter == 0)
{ // write '0'
digitalWrite(a, 1);
digitalWrite(b, 1);
digitalWrite(c, 1);
digitalWrite(d, 1);
digitalWrite(e, 1);
digitalWrite(f, 1);
digitalWrite(g, 0);

}
}

}
attached is the proteus simulation diagram
 

Attachments

nerdegutta

Joined Dec 15, 2009
2,684
Hi.

You don't have Serial.begin(9600); in the Arduino2 code.

And please use code-tags. You'll find them in the horizontal menu above the editor.

:)
 

ErnieM

Joined Apr 24, 2011
8,377
The task will be much easier if you split this in two and put your PC to work in the middle.

First you need a real RS232 level translator so your PC can read the serial data... Assuming you PC has a serial port. If not go buy a serial to USB translator.

Now with your referee in the middle you can read what one unit is sending, and then either send known data direct from the PC or from your now debugged unit.

Otherwise, does the Arduino have in circuit debugging capability? That right there can help you get thru some nasty problems (I am not an Arduino user).
 

djsfantasi

Joined Apr 11, 2010
9,156
First, you have defined the pins col_1, col_2 and col_3 as INPUTs. Then, you write to those pins. Note that writing to an INPUT pin doesn't change the value of the pin; it enables or disables an internal pullup resistor.
Code:
// set row as a output and coloum as a input
pinMode(row_1,OUTPUT);
pinMode(row_2,OUTPUT);
pinMode(row_3,OUTPUT);
pinMode(row_4,OUTPUT);
pinMode(col_1,INPUT);
pinMode(col_2,INPUT);
pinMode(col_3,INPUT);
}
void loop()
{
int val;
//setting the columns as high initially
digitalWrite(col_1,HIGH);
digitalWrite(col_2,HIGH);
digitalWrite(col_3,HIGH);
If you want the col_x inputs to be high initially, then in pinMode, set the type as INPUT_PULLUP, and don't perform the digitalWrite.
 

MrAl

Joined Jun 17, 2014
11,389
Hi,

You should probably try to get ONE switch to work first before doing all the switches. That way you can make sure the serial part works and then go on to keyboard scanning, maybe starting with just TWO switches only, getting that to work, then on to all the switches.

When you are designing both your own boards, both master and slave, you are not bound by the standard communication rules either. You can make up your own protocol which can be simpler than others. I recommend using a 2 wire interface, where one is the clock and the other is the data and no acknowledge signal. Perhaps a long dead time to sync, then start sending data. If you like, you can have the slave send the same data back and that way you have a check for data transfer accuracy, where if the data that comes back is not the same then send it again, and after say 10 tries trigger an error somewhere that displays a comm error.
There are no hard rules here, so as long as it works you can do whatever you want.
Long time ago i did this with a computer and home build controller board with the Z80 CPU. If i remember right the computer would send the data, check the return data, and send an 0xEE for a command, which would follow. To send an 0xEE as data it would be sent twice in a row (or something like that, been a long time now) and command word 0xEE (the command that followed 0xEE) was never used so was never sent.
Cant remember why i chose 0xEE either.
But the main idea is you have a lot of room for inventing a protocol you like, which can be interesting in itself and can be slow or fast whatever your system can handle, one wire or two wires or three wires :)
 
Top