Help with Serial library

I'm working on a project on Tinkercad using 2 Arduinos, where I press a button on one and the other lights up a light, and vice versa. This is my code:

int buttonPin = 7;
int led8 = 8;
int buttonState = 0;

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

  Serial.begin(9600);
}

void loop() {
  buttonState = digitalRead(buttonPin);
  
  if (Serial.find("On")) {
    digitalWrite(led8, HIGH);
    delay(1000);
    digitalWrite(led8, LOW);
  }
  
  if (buttonState == HIGH) {
    Serial.write("On");
  }
  
  delay(2000);
}

Apparently, Serial.find always returns true, even if I don't press any button; both LEDs blink (the 2 Arduinos contain the same code).
I tried using Serial.read and Serial.write with the value 1 as well, and I had the same problem. Since I'm doing the project on Tinkercad, I imagine the problem isn't garbage in the buffer or
something like that. Thanks for any help!

I've never heard of Serial.find().

And the documentation is ambiguous to me.

Perhaps try the alternate call

    Serial.find("On", 2);

where you say the length explicitly.

It is not clear to me what means calling the function with just one argument like "On".

a7

I'm working on a project on Tinkercad using 2 Arduinos, where I press a button on one and the other lights up a light, and vice versa. This is my code:

int buttonPin = 7;
int led8 = 8;
int buttonState = 0;

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

  Serial.begin(9600);
}

void loop() {
  buttonState = digitalRead(buttonPin);
  
  if (Serial.find("On")) {
    digitalWrite(led8, HIGH);
    delay(1000);
    digitalWrite(led8, LOW);
  }
  
  if (buttonState == HIGH) {
    Serial.write("On");
  }
  
  delay(2000);
}

Apparently, Serial.find always returns true, even if I don't press any button; both LEDs blink (the 2 Arduinos contain the same code).
I tried using Serial.read and Serial.write with the value 1 as well, and I had the same problem. Since I'm doing the project on Tinkercad, I imagine the problem isn't garbage in the buffer or
something like that. Thanks for any help!

This is the circuit:

I have merged your cross-posts @vendru.

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.

On this forum, there is a guide called "Serial Input Basics" by Robin2. It's excellent and you'll find your answers there

@vendru ,

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.