Go Down

Topic: Is there an easy way to communicate 2 Arduino? (Read 472 times) previous topic - next topic

ANewbie07

I'm just a student and new to Arduino  :P. I have a project that  involves communication of 2 Arduino. I have found several tutorials but neither of them involves sending analog values(0-1023). The scenario is this, Arduino A is connected to a potentiometer while Arduino B is connected to a 16x2 LCD. is their a way to display the value of the potentiometer from Arduino A to the LCD of Arduino B? And if it is possible, can it still be used as an integer?

groundfungus

Sure it's possible.  You could do it with serial, I2C or SPI.  There are lots of exmples showing how to do this.

ANewbie07

I've seen a softserial tutorial and followed it but it didn't work.

Here's the code for Arduino A
Code: [Select]
#include <SoftwareSerial.h>

SoftwareSerial Arduino(2,3); //rx/tx
int i;
int value;
int potpin = A0;
char reader[10];

void setup(){
  Arduino.begin(9600);
}

void loop(){

  value = analogRead(potpin);
  Arduino.print(value);
}


Arduino B
Code: [Select]
#include <SoftwareSerial.h>
SoftwareSerial Arduino(2,3); //rx/tx
int i;
char reader[4];

void setup(){
  Arduino.begin(9600);
  Serial.begin(9600);
}

void loop(){
  if(Arduino.available()>1){
    for(i=0; i<3; i++){
      reader[i] = Arduino.read();
      delay(2);
    }
  }

  Serial.print(reader[0]);
  Serial.print(reader[1]);
  Serial.print(reader[2]);
  Serial.println(reader[3]);

 
  for(i=0;i<3;i++)
  reader[i] = 0;

}


Sorry for the bad program :P.

UKHeliBob

In what way did it not work ?
Can you describe how you wired the 2 Arduinos together, which pin to which pin ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

tobyb121

Code: [Select]
if(Arduino.available()>1){
    for(i=0; i<3; i++){
      reader[i] = Arduino.read();
      delay(2);
    }
  }

You only check if more than 1 character is available, then try and read 3 characters, just read as many characters as are available.

ANewbie07

Okay, this time It worked! I replaced the potentiometer with a new one. But when i hooked-up my 16x2 LCD it won't display numbers instead it shows steady scribble-like characters.

I just changed the Serial.prints into:
Code: [Select]
  lcd.setCursor(0,0);
  lcd.print(reader[0]);
  lcd.print(reader[1]);
  lcd.print(reader[2]);
  lcd.print(reader[3]);

tobyb121

Did the Serial.print work properly?
Have you tried printing to the lcd on its own?
Break your program down into each individual component and test it. That's how to debug :)

PeterH


when i hooked-up my 16x2 LCD it won't display numbers instead it shows steady scribble-like characters.


If you send it garbage, it's going to display garbage.

If you want it to display a text message, construct the text message and then print it to the display. The sprintf() function is a convenient way to format data values into a text message, if that's what you're trying to do.
I only provide help via the forum - please do not contact me for private consultancy.

zoomkat

Simple test code for sending text from one arduino to another. the code should work on both arduino A and B. Connect the two arduino grounds together, and connect the arduino A tx pin to the arduino B rx pin. Send text with an ending comma , in arduino A serial monitor and it should appear in the both the arduino A and arduino B serial monitors. If that works, setting up a pot on arduino A should be fairly easy.

Code: [Select]

//zoomkat 3-5-12 simple delimited ',' string parce
//from serial port input (via serial monitor)
//and print result out serial port
// CR/LF could also be a delimiter

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}

void loop() {

  //expect a string like wer,qwe rty,123 456,hyre kjhg,
  //or like hello world,who are you?,bye!,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.print(readString); //prints string to serial port out
        Serial.println(','); //prints delimiting ","
        //do stuff with the captured readString
        readString=""; //clears variable for new input
      }
    } 
    else {     
      readString += c; //makes the string readString
    }
  }
}

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

Magicj


Go Up