Coding with serial library using 2 arduinos and UART communication

Both arduinos have the same code. My objective is so when I press a button on one the other lights up a led, and vice versa. Any ideas on why it doesnt work? Ty! :slight_smile:

int buttonPin = 7;
int led = 8;
bool ledState = false;
String serialCommand = "null";
String on = "on";
String off = "off";

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(led, OUTPUT);

  Serial.begin(9600);
}

void loop() {
  if (Serial.available()){
    serialCommand = Serial.readStringUntil('\n');
      if (serialCommand == off){
      digitalWrite(led, LOW);
    }
    if(serialCommand == on){
      digitalWrite(led, HIGH);
  }
  }
  if (digitalRead(buttonPin) == HIGH){
    if (ledState == false){
      Serial.println(on);
      ledState = true;
   }
    if (ledState == true){
      Serial.println(off);
      ledState = false;
    }
    delay(1000);
  }
}

Welcome to the forum

Do you have a question ?

Sorry im stupid, do you have any idea on why it doest work? I believe it has to do with the way im sending the information, ty for the help :slight_smile:

I want to press the button on one and the other one's led turns on. When I simulate it, the serial monitor only prints "off". This is the tinkercad project if it helps Login - Tinkercad

Im trying to make it work on tinkercad before doing it IRL. Im using 2 Arduino unos, when I run the code on tinkercad it shows a serial monitor, where it only keeps spamming "off", I posted a screenshot of the circuit above. Ty for the help :slight_smile:

Hello bruno030815

Welcome to the world's best Arduino forum ever.

To free up port 0 for debugging, use the "SoftwareSerial" library to use a second UART port for communication.
Use single characters like "A" or any other to control the leds remotely. This makes programming the receiver software easier.

hth

Have a nice day and enjoy coding in C++.

I made a post with the same question before but it was poorly formatted and with less info so im redoing it. I have 2 arduinos uno (A1 and A2), both with the same code, I want to press a button on A1 and a led that is connected to A2 turns on. With this code neither of the leds turn on but my serial monitor show on and off.
This is what I have:

int buttonPin = 7;
int led = 8;
bool ledState = false;
String serialCommand = "null";
String on = "on";
String off = "off";

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(led, OUTPUT);

  Serial.begin(9600);
}

void loop() {
  if (Serial.available()){
    serialCommand = Serial.readStringUntil('\n');
      if (serialCommand == off){
      digitalWrite(led, LOW);
    }
    if(serialCommand == on){
      digitalWrite(led, HIGH);
  }
  }
  if (digitalRead(buttonPin) == HIGH){
    if (ledState == false){
      Serial.println(on);
      ledState = true;
   }
    else if (ledState == true){
      Serial.println(off);
      ledState = false;
    }
    delay(1000);
  }
}

My circuit looks like this:

Im doing it on tinkercad before IRL, this is the link for the project: Tinkercad

Any help appreciated :slight_smile:

You have A Mess. Before going further get a copy of the Arduino Cookbook and skim it reading each thing that does something with serial. You will learn the TX and RX pins on the UNO are reserved for your terminal. You need to do some special things to make it work the way you want.

Another approach is software serial. Pin choice other then 0 and 1 is up to you. Just be sure the pin is capable of doing what you are asking it to do.

If this is all you want to do use two pins other then 0 and 1 where you turn one on or off depending on the LED value you want to show and the other will control your LED.

It will work for a few meters on a good day. Shorter wires will work better, but be sure to connect grounds. If you want to go further or do something else pass it before the forum to validate your idea before spending money.

this is a college project, its my first time using arduino/esp etc, even with this problem wouldnt tinkercad be able to properly run it?

I only trust myself, I have never found an error free simulator. Start with an annotated schematic not a frizzy wiring diagram and post it. You probably get a lot of hits and have the solution in a day or less. With the frizzy pictures it is hard to answer if you do not have all the parts. None of mine look like your picture so me wanting to help as many as possible will generally ignore questions with these frizzy pictures. The fact that the code was properly posted peaked my interest.

There is a program called KiCad that if free for the download, a donation is asked but not required. It is a full blown schematic capture program. I use it to go from schematic to finished circuit boards. Do not expect to spend an evening or two and know it. It will take some time depending on your skill set and experience. Note this program has no restraints or limits, you can do a 20 layer board if you so chose.

There are libraries that have the Arduinos in them and many of the modules. If I cannot find them it is easy to make them. The first few I opened something similar in a library and edited it. Be sure to save it with a new name in your library. The more time you spend the faster and easier it will be.

Do you have the following gadgets?:
1. Two Arduinos,
2. Two psuh buttons,
3. One breadboard,
4. 10 Male-to-Male jumpers, and
5. One PC.

@bruno030815 ,
I have merged your 2 English language version of your topic and deleted your Portuguese version. Posting the same question multiple times is against to forum rules as it can result in different helpers answering the same question in different places each unaware of what the others are doing and so wasting the time they give voluntarily. Repeated multiple topics can result in ban from the forum.

Thank you.

1 Like

@bruno030815,
Please reply to the PM I have sent to you. Thank you.

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