[英]UART connection between ESP32 as master and Arduino as Slave
Stackoverflow 的公民
我想将按钮值从 ESP32 发送到 Arduino UNO,由于某种原因,Arduino 不会收到从 ESP32 到 Arduino 的值,但值显示在 ESP32 中。 这是程序:
大师 ESP32
const int buttonPin = 15; // push button pin
int buttonState = 0; // button state
int period = 500; // for using delay in output
unsigned long time_now = 0; // delay purposes
char* state = "00" ; // first state
void setup() {
// put your setup code here, to run once:
pinMode(buttonPin, INPUT);
Serial.begin(9600);
pinMode(13, OUTPUT); // LED PIN for proving purposes
}
void loop() {
buttonState = digitalRead(buttonPin); // reading push button
if (buttonState == 1) { // checking push button state
if (state == "01") { // reading the state now
state = "00"; // change state to OFF
}
else {
state = "01"; // change state to ON
}
}
while (millis() >= time_now + period) { // serial delay purposes
time_now += period;
Serial.write(state);
int x = atol(state);
digitalWrite(13, x); // checking state true or not using LED
}
Serial.flush(); // clean buffer
}
从机 Arduino
char data_1; // variable for recieved data
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(13, OUTPUT); // LED pin
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available()) { // checking Serial
data_1 = Serial.read(); // reading value from ESP
int x = atol(data_1); // change value to integer
digitalWrite(13, x); // proving using LED
delay(500);
Serial.flush(); // clean buffer
}
else{
Serial.println("Not Available"); // checking if It's actually sending data
}
}
正如您在代码中看到的,来自 Arduino 的 output 是“不可用”,而来自 ESP32 的 output 可以很好地触发 LED。 也许有人可以帮我弄清楚?
Stackoverflow 的公民
我想将按钮值从 ESP32 发送到 Arduino UNO,由于某种原因,Arduino 不会收到从 ESP32 到 Arduino 的值,但值显示在 ESP32 中。 这是程序:
大师 ESP32
const int buttonPin = 15; // push button pin
int buttonState = 0; // button state
int period = 500; // for using delay in output
unsigned long time_now = 0; // delay purposes
char* state = "00" ; // first state
void setup() {
// put your setup code here, to run once:
pinMode(buttonPin, INPUT);
Serial.begin(9600);
pinMode(13, OUTPUT); // LED PIN for proving purposes
}
void loop() {
buttonState = digitalRead(buttonPin); // reading push button
if (buttonState == 1) { // checking push button state
if (state == "01") { // reading the state now
state = "00"; // change state to OFF
}
else {
state = "01"; // change state to ON
}
}
while (millis() >= time_now + period) { // serial delay purposes
time_now += period;
Serial.write(state);
int x = atol(state);
digitalWrite(13, x); // checking state true or not using LED
}
Serial.flush(); // clean buffer
}
从机 Arduino
char data_1; // variable for recieved data
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(13, OUTPUT); // LED pin
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available()) { // checking Serial
data_1 = Serial.read(); // reading value from ESP
int x = atol(data_1); // change value to integer
digitalWrite(13, x); // proving using LED
delay(500);
Serial.flush(); // clean buffer
}
else{
Serial.println("Not Available"); // checking if It's actually sending data
}
}
正如您在代码中看到的,来自 Arduino 的 output 是“不可用”,而来自 ESP32 的 output 可以很好地触发 LED。 也许有人可以帮我弄清楚?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.