Go Down

Topic: problem with serial communication arduino - pic (Read 734 times) previous topic - next topic


Hi , I want to make simple project , the project is two fire(smoke) detectors in two rooms where every smoke detector has a pic16f88 and the arduino is the controller , I wrote the code , the principle of code is the arduino send serial msgs where every pic has special msg(special number).Note :- (this step is even don't happen synchronization)
when pic receives its special msg and the pin 3(input) is high (when the fire occur) ,send serial msg(number of room) to  arduino then  arduino specify the room by print in termial monitor.
I simulate the code of pic in proteus and code of arduino in terminal monitor and work well.

1'st room (pic16f88) code:
Code: [Select]
char yt[3];
void main(){
ANSEL  = 0;
UART1_Init(9600);                         // initialize UART1 module
while (1) {
  if (UART1_Data_Ready()== 1)           // if data is received
        {UART1_Read_Text(yt,"o",5);    // reads text until 'OK' is found
        if (yt[0] == '0' && yt[1]=='0' && yt[2]=='1' && PORTA==0B00001000)
        {UART1_Write_Text("001");Delay_ms(5000); }
          }           // sends back text } }

arduino mega code :
Code: [Select]

#include <SoftwareSerial.h>
SoftwareSerial myserial(15,14); // RX, TX
char i[4]={'a','a','a'};
char ch;
int index=0;
char array[2][4] = {{'0','0','1','o'},
char e[4];
void setup(){
//create serial object
Serial.println("power on"); }

void loop(){ 
for (int i=0;i<2;i++){
  for (int p=0;p<4;p++)

void r(){
//have the arduino wait to receive input
while(myserial.available() > 0){
//Read the input
Serial.println("the fire in the room :");
ch = myserial.read();
i[index]= ch ;
if (i[0]!='a' && i[1]!='a' && i[2]!='a' ){

the code hasn't error, I made the pin 3 of 1'st smoke detector always high then must print in terminal monitor the number 001(every 5 seconds) but I don't know why don't print.


First, I suggest that you do something about the format of your code. Put every { on a new line. Then use Tools + Auto Format.

With that format, it is hard to see what the code is supposed to be doing.

Second, give meaningful names to functions. You'll notice that the function to read a digital pin is called digitalRead(), not R93krg(). So, what the hell does r() do? There is NOTHING in the name that gives a clue.

Third, it is plain stupid to be doing SoftwareSerial on the hardware serial pins. Stop that.


Apr 19, 2013, 01:16 pm Last Edit: Apr 19, 2013, 04:14 pm by TTU.ABD Reason: 1

So, what the hell does r() do?

the function r()to read serial msg(the number of room) from pic16f88 and print it on serial monitor.

Third, it is plain stupid to be doing SoftwareSerial on the hardware serial pins. Stop that.

is there another way to use serial pins?


he function r()to read serial msg(the number of room)

So, getRoomNumber() or readSerialData() might actually describe the purpose of the function. At least far better than r().

is there another way to use serial pins?

Yes. Use the appropriate HardwareSerial instance, Serial, Serial2, Serial2, or Serial3, that goes with the pins.

Go Up

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.

shortcuts: alt+s submit/post or alt+p preview