Pages: [1]   Go Down
Author Topic: Reading with Serial port  (Read 329 times)
0 Members and 1 Guest are viewing this topic.
Czech republic, Brno
Offline Offline
Jr. Member
**
Karma: 0
Posts: 52
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
I  have newbie question.
I have one arduino sending some data by the serial port.
Here is the code from sending arduino:

if ((zona1 > 900) && (xzona1 < 1))
   {    
   Serial1.println(1,DEC);
   delay(250);
   xzona1 += 1;        
   }  
if (zona1 < 200)
   {  
   xzona1 = 0;  
   }



So I am sending '1'. But this arduino is sedning more data like for example K1001, S11 etc...  

The other arduino might detect the incomimg message '1' and ligh the led and reprint the '1' to Serilaport.

How should I do it?
Here is my test code>

int tmp=0;
int pre=0;
int data=0;

void setup ()
Serial.begin(115200);
Serial1.begin(115200);

void loop()
if ( Serial1.available() > 0)
   {
   tmp = Serial1.read();
   if (tmp == 10 || tmp == 13)
      {
       pre = 1;
      }
   if (pre == 1)
      {
       data = tmp;
       pre = 0;
      }  
   }

Serial.println (data);


if (data == '1')
   {
    analogWrite (ledPin1, 500);
    }
   else
   {
    analogWrite (ledPin1, 0);
   }


But it doesnot work. It doesnot print any data.

 When I use just this simple code it works (the led shines, but the serial data wans reprinted...). Problem was, that the led shines even when any data with number 1 come to Rx.  (like mentioned K1001,S11... but this data must do nothing with led)

if ( Serial1.available() > 0)
   {
   data = Serial1.read();
    }

Serial.println (data);   // this doesnot reprint the incoming massages  
                                    like '1', S11, or anything / just prints only
                                    many zeros.....


if (data == '1')
   {
    analogWrite (ledPin1, 500);     // but the LED shines, when I send
                                                    the '1', or anthing with 1
   delay (100);
    }
   else
    {
    analogWrite (ledPin1, 0);
    }



Please can anyone help me with this?
I am still very newbie with programming and this is quite difficult for me.

Thanks a lot! smiley
Logged

Albury, NSW, Australia
Offline Offline
Full Member
***
Karma: 0
Posts: 188
Soon to be master of the Electric Scooter!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

How is your code actually compiling when you have Serial1 in there?  This is invalid
Code:
data = Serial1.read();

Read the documentation on Serial.read().  It'll only read the first byte of data in the buffer.
Logged

Pages: [1]   Go Up
Jump to: