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'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!
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.