Skip to main content
Arduino

Return to Revisions

2 of 2
deleted 84 characters in body; edited title
dda
  • 1.6k
  • 1
  • 12
  • 17

How to add a yield before else if?

So I am running my project on a NodeMCU, but I don't know how to let it add a yield in front of an else if. It will give me an error like this:

Arduino:1.8.12 (Windows 10), 開發板:"NodeMCU 1.0 (ESP-12E Module), 80 MHz, Flash, Legacy (new can return nullptr), All SSL ciphers (most compatible), 4MB (FS:2MB OTA:~1019KB), 2, v2 Lower Memory, Disabled, None, Sketch + WiFi Settings, 115200"

G:\資優班\科展\程式碼\Front_End\Main_Code_V5\Main_Server\Main_Server.ino: In function 'void loop()':

Main_Server:84:5: error: 'else' without a previous 'if'

exit status 1 'else' without a previous 'if'

This report would have more information with "Show verbose output during compilation" option enabled in File -> Preferences.

Here's the code:

#include <SPI.h>
#include <ESP8266WiFi.h>
#define MAX_SRV_CLIENTS 2
#define led LED_BUILTIN
WiFiServer server(80);
WiFiClient serverClients[MAX_SRV_CLIENTS];
IPAddress ip(10, 241, 241, 27);
IPAddress mask(255, 255, 255, 0);
IPAddress gateway(10, 241, 241, 254);
char ssid[] = "AP";
char pass[] = "Science_Fair";
void setup() {
 Serial.begin(9600);
 WiFi.config(ip, gateway, mask);
 WiFi.begin(ssid, pass);
 while (WiFi.status() != WL_CONNECTED) {
 yield();
 }
 server.begin();
 pinMode(led, OUTPUT);
 digitalWrite(led, LOW);
}
void loop() {
 uint8_t i;
 Serial.setTimeout(10);
 for (i = 0; i < MAX_SRV_CLIENTS; i++) {
 Serial.println("Pre 1");
 if (server.hasClient()) {
 Serial.println("Pre 2");
 if (!serverClients[i] || !serverClients[i].connected()) {
 Serial.println("Pre 3");
 if (serverClients[i]) {
 Serial.println("Pre 4");
 serverClients[i].stop();
 Serial.println("Pre 5");
 }
 Serial.println("Pre 6");
 serverClients[i] = server.available();
 Serial.println("Pre 7");
 serverClients[i].setTimeout(10);
 Serial.println("Pre 8");
 Serial.print("New client: ");
 Serial.println("Pre 9");
 Serial.println(i);
 Serial.println("Pre 10");
 break;
 }
 }
 yield();
 }
 for (i = 0; i < MAX_SRV_CLIENTS; i++) {
 String fromClient = serverClients[i].readStringUntil('e');
 Serial.println("P1");
 yield();
 if (fromClient == "LR") {
 Serial.println("LR");
 String reply = Serial.readStringUntil('e');
 Serial.println("P2");
 yield();
 if (reply == "Warn_B") {
 yield();
 Serial.println("Confirm");
 Serial.println("P3");
 for (i = 0; i < MAX_SRV_CLIENTS; i++) {
 serverClients[i].print("Warn_B");
 Serial.println("P4");
 yield();
 }
 }
 }
 yield(); // this is the yield I need to add
 else if (fromClient == "RR") {
 Serial.println("RR");
 Serial.println("P5");
 String reply = Serial.readStringUntil('e');
 Serial.println("P6");
 yield();
 if (reply == "Warn_A") {
 Serial.println("P7");
 Serial.println("Confirm");
 }
 for (i = 0; i < MAX_SRV_CLIENTS; i++) {
 serverClients[i].print("Warn_A");
 Serial.println("P8");
 yield();
 }
 }
 yield();
 }
}

I need the yield() there but I don't know how to let it still do the else if. Please help me guys, thanks!

default

AltStyle によって変換されたページ (->オリジナル) /