Uno and Mega communication

Hi I have a quick quesiton I have a soil sensor on an Arduino Mega. And that Arduino Mega is wired connected to an Arduino Uno. On the uno, I have a 16x2 LCD Display. So I was trying to get a reading from one soil sensor to display the number on the Lcd on the other Arduino. But when I try my current code I can't get a continuous reading on my LCD.
Here is my Mega code
`#include <Wire.h>
int msensor=2;
int x=0;
int receiveEvent;
void setup() {
Wire.begin();
Serial.begin(9600);
pinMode(msensor, INPUT);
Wire.onReceive(receiveEvent);
}

void loop() {
x = digitalRead(msensor);
Serial.println(x);
Wire.beginTransmission(1);
Wire.write(x);
Wire.endTransmission();
}`

Here is my reciver
`#include <LiquidCrystal.h>
#include <Wire.h>
const int rs = 4, en = 6, d4 = 10, d5 = 11, d6 = 12, d7 = 13;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int x = 0;
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
Wire.begin(1);
Wire.onReceive(receiveEvent);
}
void receiveEvent(int howmany) {
x = Wire.read();
if ( (x >= 500 ) )
{
Serial.print(x);
lcd.print(x);
lcd.clear();

}

if ( (x <= 300 ) )
{
Serial.println(x);
lcd.print(x);
lcd.clear();

}
// read one character from the I2C
}

void loop() {
// Turn on the display:
delay(100);
}
`

Please do not hijack topics. And in this case, do not hijack an unrelated topic.

Your attempt at the use of code tags failed. Start with ```on its own line and end with ``` on its own line. Please edit your post accordingly.

ok I was in a rush. This is a project which was working before but now and it due soon. So I need help quickly. Do you have a solution?

why are you using mega and uno both ? and you should use pwm pin or analog pin for msensor. if you are getting data on lcd but that is not continuous you should check your wiring connections.

i am using 2 because I'm trying to make a remote control type of thing its complicated. And I tried analog pin before too and it doesn't work the right way. I wanted to get the lcd to update they number every time I get a new value. I also tested both components and they both work, but when combining them they don't seem to work as intended.

I have deleted your other cross-posts and hijacks @arduinonewbie12345.

Cross-posting is against the Arduino forum rules. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting can result in a suspension from the forum.

In the future, please only create one topic for each distinct subject matter. This is basic forum etiquette, as explained in the "How to get the best out of this forum" guide. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

Try these sketches which are yours' with slight modifications:

//Here is my Mega code
`#include <Wire.h>

int msensor = 2;
int x=0;
//int receiveEvent;
volatile bool flag = false;

void setup() 
{
  Wire.begin();
  Serial.begin(9600);
  pinMode(msensor, INPUT);
  //Wire.onReceive(receiveEvent);
}

void loop() 
{
   x = analogRead(msensor);
   Serial.println(x);
   Wire.beginTransmission(8); //0 - 7 reserved
   Wire.write(highByte(x));
   Wire.write(lowByte(x));
   Wire.endTransmission();
   delay(1000);
}`

//Here is my reciver
`#include <LiquidCrystal.h>
#include <Wire.h>
const int rs = 4, en = 6, d4 = 10, d5 = 11, d6 = 12, d7 = 13;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int x = 0;
byte xH;
byte xL;

void setup() 
{
   Serial.begin(9600);
   lcd.begin(16, 2);
   Wire.begin(8);
   Wire.onReceive(receiveEvent);
}

void loop()
{
    if(flag == true)
    {
         if ( (x >= 500 ) )
         {
             Serial.print(x);
              lcd.setCursor(0, 0);             
              lcd.print(x);
              flag = false;
         }
         if ( (x <= 300 ) )
         {
              Serial.println(x);
              lcd.setCursor(0, 1);  //2nd line             
              lcd.print(x);
              flag = false;
          }
}

void receiveEvent(int howMany) 
{
   xH = Wire.read();
   xL = Wire.read();
   x = xH << 8 | xL;
   flag = true;
}

What is the distance between Uno and Mega?

around 8in

@GolamMostafa , pin 2 on Mega is not an analog pin

my soil sensor takes digital and analog. I tried A1 and D2 for the different selection I made on the component.

Which analog channel you have used to acquire signal from your sensor?

I2c is short of "inter integrated circuits", it designed to connect devices on the same PCB, and works on short distances only.
Guaranteed distance for i2c connection is no more than 30cm

Edition: I am blind tomorrow, I read your "8 in" as "8m"

But, x = analogRead(2); should acquire signal from Ch-2 (A2) -- is not it?

it is but when transmitting it to the other Arduino it didn't transmit correctly.

I still not understanding what the point to use a two arduinos. Remote control for 8 in? Really?
Do you separate the sensor reading and display because you have a trouble to manage them in the same code?

It looks like the project is an School assignment.

It explains the rush, but not a design. Using a two Arduino is four times more complicated than one...

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.