Pages: [1]   Go Down
Author Topic: Send/Recive between 2 arduinos  (Read 764 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi

I am trying to send and recive serial communication with the help from 2 max485 and 2 arduinos.

There seems to be something wrong with the code (suprice?  smiley-wink), it doenst get every bit that i am sending. I am sending a "1" .. wait... then sending "0" and so on.

The LED on the reciving board turns on, and off. But doenst do anything for 2-3-4 pulses and then keeps on going like before - totaly random.

they are both powered on by USB from the same PC.

Arduino 1 - Sending unit

Code:
void setup(){
  Serial.begin(9600); //opens serial port, sets data rate to 9600 bps
  pinMode(2, OUTPUT);
}

void loop()
{
  digitalWrite(2, HIGH); // Set pin2 HIGH
  Serial.write(1); // send a byte with the value 1
 
  delay(2000);  // Wait 2sec
  Serial.write(0); // send a byte with the value 0
  delay(2000); // Wait 2sec
}

Arduino 2 - Reciving unit

Code:
int incomingByte = 0;   // for incoming serial data

void setup() {
        Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
        pinMode(13, OUTPUT);
}

void loop() {

        // send data only when you receive data:
        if (Serial.available() > 0) {
                // read the incoming byte:
                incomingByte = Serial.read();
        }
                // turn on/off LED
         if (incomingByte > 0) {
                 digitalWrite(13, HIGH);
                }
                else
                {
                  digitalWrite(13, LOW);
                }                 
        }

Any suggestions i have done wrong? Seems pretty basic, but cant find the mistake.
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 84
Posts: 3400
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why not try to debug it with the serial monitor? You should see what you are getting, and adjust your code from there.
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the quick answer.

I am getting some thing like "110101001" so somehow it doenst recive every time i send a 1 or 0.

The LED changes when it is 1 and 0. So no problem there. But the information recived is not correct with the information send.

**Update**

this is the exact serial monitor "010101001010110101001010010101" - The time between i get each number is difrent. Sometimes i get 0101 as i should. Other times there are a bigger delay.
« Last Edit: March 11, 2013, 08:59:16 am by MrMr » Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 518
Posts: 26341
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

On the receive side:

I think you might have data type mismatches, so that you think you sending
0x00 or B00000000 but are actually sending '0' which is 0x30, B00110000
and 0x01 or B00000001  vs '1', 0x31 B00110001

See asciitable.com

If so, this might solve it:

if (incomingByte == '0') {

and

if (incomingByte == '1') {

or perhaps


switch(incomingByte){
case '0':
//code
break;
case '1':
// code
break;
} // end switch

Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12534
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The code looks reasonable so I would suspect there's a problem with the serial communication mechanism.

You say you're using a pair of max485 transceivers. How have you connected them to each other and to the Arduinos? Did you remember to connect the grounds?
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I just hooked it up again and now i get these numbers "4948494849484949" in the serial monitor. Now i am lost.

Crossroads: Thanks for the suggestions. Neither of the solutions did solve the problem.

PeterH: very bad drawings but hope you understand. https://www.dropbox.com/s/6h4j220xxasc1tr/Arduino-S-R.JPG

DI to TX(1) and RO to RX(0) on both sender and reciver.

*Update*

Forgot about the resistors. I got a resistor of 150ohm between A and B on both max485. Got a 560 resistor from the one end of A to GND and B to 5V. Altso 560 ohm.
« Last Edit: March 12, 2013, 02:24:34 am by MrMr » Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I just hooked it up again and now i get these numbers "4948494849484949" in the serial monitor. Now i am lost.

Not when I just tested it. Is your first post still the current code?
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

48 is hex 0x30 which is '0'
49 is hex 0x31 which is '1'

In ASCII that is. So if you have altered your sketch in some way those numbers are perfectly reasonable.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I just hooked it up again and now i get these numbers "4948494849484949" in the serial monitor. Now i am lost.

Not when I just tested it. Is your first post still the current code?

No. I tried that "case '1' " code as suggested. That was when i got these in weird numbers in the serial monitor. Must be the cause, because i got "10101100101" with the code from first post.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

DI to TX(1) and RO to RX(0) on both sender and reciver.

Are you dropping the enable bit immediately after sending?

Maybe read this: http://www.gammon.com.au/forum/?id=11428

Scroll down to "Flushing the output".
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

No. I tried that "case '1' " code as suggested. That was when i got these in weird numbers in the serial monitor. Must be the cause, because i got "10101100101" with the code from first post.

The code in your first post (Arduino 1 - Sending unit) does not generate those numbers.

Can you post your current sending code please?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No. I tried that "case '1' " code as suggested. That was when i got these in weird numbers in the serial monitor. Must be the cause, because i got "10101100101" with the code from first post.

The code in your first post (Arduino 1 - Sending unit) does not generate those numbers.

Can you post your current sending code please?

The code is exactly the same as in the first post. Thats why i am confused. I will upload the code again and the problem should be gone, cant see why this suddently happens when i am sending 1 and 0s.

At the moment i have the pin2 (my enable) on HIGH always. Maybe this is the problem, i will read your post and see if it explains the problems.

Cant say thanks to many times. So thanks again.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

48 is hex 0x30 which is '0'
49 is hex 0x31 which is '1'

In ASCII that is. So if you have altered your sketch in some way those numbers are perfectly reasonable.

I didnt see this message, sorry. The reciving part had the case 1 case 2 code. That was the diffrence.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think i solved the problem.
Code:
       if (Serial.available() > 0) [b]{[/b]
                // read the incoming byte:
                incomingByte = Serial.read();
    >>>>>>>>>>>>    } <<<<<<<<<<<<<<<<<<<<<<
                // turn on/off LED
         if (incomingByte > 0) {
                 digitalWrite(13, HIGH);
                }
                else
                {
                  digitalWrite(13, LOW);
                }                  
        }

moved the bracket down and it all worked. 1.0.1.0.1.0.1.0


- thanks for the help guys. It is very apriciated!
« Last Edit: March 13, 2013, 04:39:41 am by MrMr » Logged

Pages: [1]   Go Up
Jump to: