Pages: [1]   Go Down
Author Topic: problem with serial communication arduino - pic  (Read 543 times)
0 Members and 1 Guest are viewing this topic.
jordan
Offline Offline
Jr. Member
**
Karma: 0
Posts: 56
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
char yt[3];
void main(){
 ANSEL  = 0;
UART1_Init(9600);                         // initialize UART1 module
Delay_ms(100);
TRISA = 0xFF;
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:
#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'},
                    {'0','0','2','o'}};
char e[4];
                   
void setup(){
 //create serial object
Serial.begin(9600);
myserial.begin(9600);
Serial.println("power on"); }

void loop(){ 
for (int i=0;i<2;i++){
  for (int p=0;p<4;p++)
{array[i][p];
myserial.write(array[i][p]);
}
r();}
}

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 ;
index++;}
index=0;
if (i[0]!='a' && i[1]!='a' && i[2]!='a' ){
Serial.print(i[0]);Serial.print(i[1]);Serial.print(i[2]);
Serial.print("\n");
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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45976
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

jordan
Offline Offline
Jr. Member
**
Karma: 0
Posts: 56
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
« Last Edit: April 19, 2013, 09:14:14 am by TTU.ABD » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45976
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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().

Quote
is there another way to use serial pins?
Yes. Use the appropriate HardwareSerial instance, Serial, Serial2, Serial2, or Serial3, that goes with the pins.
Logged

Pages: [1]   Go Up
Jump to: