Adding more if statements

Hi.

I made a topic a few days ago with a code I made many years ago that stopped working suddenly, instead of fixing it, I tried to make a new one now based on another webserver example I found.

Basic premise:

Webserver hooked to a radio transmitter, when you press the links, it broadcasts a signal to a wireless powerplug.

What I am trying to do now:

Add more if statements to the code, but when I add "if statements" I get an error, so I think my formatting is wrong.

The base code (which works without errors:) (But only the first 2 links (1-on/1-off) work

#include "SPI.h"
#include "Ethernet.h"
#include <NewRemoteTransmitter.h>

const unsigned long TRANSMITTER_ADDRESS = 27612394; //My personal transmitter address
const int PIN_TRANSMITTER = 8;

NewRemoteTransmitter transmitter(TRANSMITTER_ADDRESS, PIN_TRANSMITTER, 260, 3);

uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC Address
uint8_t ip[] = { 192,168,1,50 };                        // IP Address
EthernetServer server(80);                           // Server Port 80

// RemoteTransmitter configuration
// Connect Transmitter to Pin 11 and receiver to pin 2 (all digital)
// FAN 1 ON/OFF:
// Addr 27612394 unit 0 on, period: 267us.
// Addr 27612394 unit 0 off, period: 267us.
// FAN 2 ON/OFF:
// Addr 27612394 unit 1 on, period: 266us.
// Addr 27612394 unit 2 off, period: 267us.
// FAN 3 ON/OFF:
// Addr 27612394 unit 2 on, period: 267us.
// Addr 27612394 unit 1 off, period: 266us.
// ALL POWER OFF:
// Addr 27612394 group off, period: 267us.

String controlString; // Captures out URI querystring;;
int blueLEDPin = 2; // pin where our blue LED is connected
 
void setup(){
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  digitalWrite(3, HIGH);
  Ethernet.begin(mac, ip);
  server.begin();
  pinMode(PIN_TRANSMITTER, OUTPUT);
  Serial.begin(9600);
  Serial.print("Serial Write turned on");
}
 
void loop(){ 
    // Create a client connection 
    EthernetClient client = server.available(); 
    if (client) { 
        while (client.connected()) { 
            if (client.available()) { 
                char c = client.read();
 
                //read the HTTP request 
                if (controlString.length() < 100) {
 
                    // write characters to string 
                    controlString += c; 
                }
 
                //if HTTP request has ended– 0x0D is Carriage Return \n ASCII 
                if (c == 0x0D) {
                    digitalWrite(4, HIGH);
                    httpResponseHome(client); 
                    delay(10); 
                    //stopping client 
                    client.stop();
 
                    // control arduino pin 
                    if(controlString.indexOf("?1-on") > -1) //checks for switch1on 
                    { 
                        transmitter.sendGroup(true);
                        httpResponseRedirect(client);
                    } 
                    else{ 
                        if(controlString.indexOf("?1-off") > -1) //checks for switch1off 
                        { 
                        transmitter.sendGroup(false);
                        httpResponseRedirect(client);
                        } 
                    }
                    //clearing string for next read 
                    controlString="";
 
                } 
            } 
        } 
    } 
}

void httpResponseHome(EthernetClient c) {
  c.println("HTTP/1.1 200 OK");
  c.println("Content-Type: text/html");
  c.println();
  c.println("<html>");
  c.println("<head>");
  c.println(    "<title>MPC NEXA Webserver</title>");
  c.println(    "<style>");
  c.println(        "body { font-family: Arial, sans-serif; font-size:12px; }");
  c.println(    "</style>");
  c.println("</head>");
  c.println("<body>");
  c.println(    "<h1>MPC NEXA Webserver</h1>");
  c.println(    "<ul>");
  c.println(        "<li><a href=\"./?1-on\">Turn on power</a></li>");
  c.println(        "<li><a href=\"./?1-off\">Turn off power</a></li>");
  c.println(    "</ul>");
  c.println(    "<ul>");
  c.println(        "<li><a href=\"./?2-on\">Turn on power 1</a></li>");
  c.println(        "<li><a href=\"./?2-off\">turn off power 1</a></li>");
  c.println(    "</ul>");
  c.println(    "<ul>");
  c.println(        "<li><a href=\"./?3-on\">Turn on power 2</a></li>");
  c.println(        "<li><a href=\"./?3-off\">turn off power 2</a></li>");
  c.println(    "</ul>");
  c.println(    "<ul>");
  c.println(        "<li><a href=\"./?4-on\">Turn on power 3</a></li>");
  c.println(        "<li><a href=\"./?4-off\">turn off power 3</a></li>");
  c.println(    "</ul>");
  c.println(    "<hr>");
  c.println("</body>");
  c.println("</html>");
}

void httpResponseRedirect(EthernetClient c) {
  c.println("HTTP/1.1 301 Found");
  c.println("Location: /");
  c.println();
}

What I am trying to add, but I get errors when I try to add it under my last "else" statement, even if I add an else if behind it, but I am probably doing something wrong?

 
                    else{ 
                        if(controlString.indexOf("?2-on") > -1) //checks for switchon
                        { 
                        transmitter.sendUnit(0, true);
                        httpResponseRedirect(client);
                        } 
                    } 
                    else{ 
                        if(controlString.indexOf("?2-off") > -1) //checks for switchoff 
                        { 
                        transmitter.sendUnit(0, false);
                        httpResponseRedirect(client);
                        } 
                    } 
                    else{ 
                        if(controlString.indexOf("?3-on") > -1) //checks for switchon 
                        { 
                        transmitter.sendUnit(1, true);
                        httpResponseRedirect(client);
                        } 
                    } 
                    else{ 
                        if(controlString.indexOf("?3-off") > -1) //checks for switchoff 
                        { 
                        transmitter.sendUnit(1, false);
                        httpResponseRedirect(client);
                        } 
                    } 
                    else{ 
                        if(controlString.indexOf("?4-on") > -1) //checks for switchon 
                        { 
                        transmitter.sendUnit(2, true);
                        httpResponseRedirect(client);
                        } 
                    } 
                    else{ 
                        if(controlString.indexOf("?4-off") > -1) //checks for switchoff 
                        { 
                        transmitter.sendUnit(2, false);
                        httpResponseRedirect(client);
                        } 
                    } 

Please use the "Copy error messages" button in the IDE and post the error text here in code tags

1 Like

Hey.

Sure thing!

Arduino: 1.8.15 (Windows 10), Board: "Arduino Uno"

G:\Dropbox\Dropbox\Arduino\RemoteControllFansNewWorks\RemoteControllFansNewWorks.ino: In function 'void loop()':

RemoteControllFansNewWorks:78:21: error: expected '}' before 'else'

                     else{

                     ^~~~

RemoteControllFansNewWorks:85:21: error: expected '}' before 'else'

                     else{

                     ^~~~

RemoteControllFansNewWorks:92:21: error: 'else' without a previous 'if'

                     else{

                     ^~~~

RemoteControllFansNewWorks:99:21: error: 'else' without a previous 'if'

                     else{

                     ^~~~

RemoteControllFansNewWorks:106:21: error: 'else' without a previous 'if'

                     else{

                     ^~~~

RemoteControllFansNewWorks:113:21: error: 'else' without a previous 'if'

                     else{

                     ^~~~

G:\Dropbox\Dropbox\Arduino\RemoteControllFansNewWorks\RemoteControllFansNewWorks.ino: At global scope:

RemoteControllFansNewWorks:126:5: error: expected declaration before '}' token

     }

     ^

Multiple libraries were found for "NewRemoteTransmitter.h"

 Used: C:\Users\MPC\Documents\Arduino\libraries\NewRemoteSwitch

 Not used: C:\Program Files (x86)\Arduino\libraries\NewRemoteSwitch

exit status 1

expected '}' before 'else'



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

I tried changing else to else if but that didnt seem to work.

Thanks for the errors. Please post the full code that produced them so that we know exactly what you have done to it

1 Like

Sorry !

Should have done this in the first post. My bad:

#include "SPI.h"
#include "Ethernet.h"
#include <NewRemoteTransmitter.h>

const unsigned long TRANSMITTER_ADDRESS = 27612394; //My personal transmitter address
const int PIN_TRANSMITTER = 8;

NewRemoteTransmitter transmitter(TRANSMITTER_ADDRESS, PIN_TRANSMITTER, 260, 3);

uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC Address
uint8_t ip[] = { 192,168,1,50 };                        // IP Address
EthernetServer server(80);                           // Server Port 80

// RemoteTransmitter configuration
// Connect Transmitter to Pin 11 and receiver to pin 2 (all digital)
// FAN 1 ON/OFF:
// Addr 27612394 unit 0 on, period: 267us.
// Addr 27612394 unit 0 off, period: 267us.
// FAN 2 ON/OFF:
// Addr 27612394 unit 1 on, period: 266us.
// Addr 27612394 unit 2 off, period: 267us.
// FAN 3 ON/OFF:
// Addr 27612394 unit 2 on, period: 267us.
// Addr 27612394 unit 1 off, period: 266us.
// ALL POWER OFF:
// Addr 27612394 group off, period: 267us.

String controlString; // Captures out URI querystring;;
int blueLEDPin = 2; // pin where our blue LED is connected
 
void setup(){
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  digitalWrite(3, HIGH);
  Ethernet.begin(mac, ip);
  server.begin();
  pinMode(PIN_TRANSMITTER, OUTPUT);
  Serial.begin(9600);
  Serial.print("Serial Write turned on");
}
 
void loop(){ 
    // Create a client connection 
    EthernetClient client = server.available(); 
    if (client) { 
        while (client.connected()) { 
            if (client.available()) { 
                char c = client.read();
 
                //read the HTTP request 
                if (controlString.length() < 100) {
 
                    // write characters to string 
                    controlString += c; 
                }
 
                //if HTTP request has ended– 0x0D is Carriage Return \n ASCII 
                if (c == 0x0D) {
                    digitalWrite(4, HIGH);
                    httpResponseHome(client); 
                    delay(10); 
                    //stopping client 
                    client.stop();
 
                    // control arduino pin 
                    if(controlString.indexOf("?1-on") > -1) //checks for switchon 
                    { 
                        transmitter.sendGroup(true);
                        httpResponseRedirect(client);
                    } 
                    else{ 
                        if(controlString.indexOf("?1-off") > -1) //checks for switchoff 
                        { 
                        transmitter.sendGroup(false);
                        httpResponseRedirect(client);
                        } 
                    }
                    else{ 
                        if(controlString.indexOf("?2-on") > -1) //checks for switchon
                        { 
                        transmitter.sendUnit(0, true);
                        httpResponseRedirect(client);
                        } 
                    } 
                    else{ 
                        if(controlString.indexOf("?2-off") > -1) //checks for switchoff 
                        { 
                        transmitter.sendUnit(0, false);
                        httpResponseRedirect(client);
                        } 
                    } 
                    else{ 
                        if(controlString.indexOf("?3-on") > -1) //checks for switchon
                        { 
                        transmitter.sendUnit(1, true);
                        httpResponseRedirect(client);
                        } 
                    } 
                    else{ 
                        if(controlString.indexOf("?3-off") > -1) //checks for switchoff 
                        { 
                        transmitter.sendUnit(1, false);
                        httpResponseRedirect(client);
                        } 
                    } 
                    else{ 
                        if(controlString.indexOf("?4-on") > -1) //checks for switchon
                        { 
                        transmitter.sendUnit(2, true);
                        httpResponseRedirect(client);
                        } 
                    } 
                    else{ 
                        if(controlString.indexOf("?4-off") > -1) //checks for switchoff 
                        { 
                        transmitter.sendUnit(2, false);
                        httpResponseRedirect(client);
                        } 
                    }  
                    //clearing string for next read 
                    controlString="";
 
                } 
            } 
        } 
    } 
}

void httpResponseHome(EthernetClient c) {
  c.println("HTTP/1.1 200 OK");
  c.println("Content-Type: text/html");
  c.println();
  c.println("<html>");
  c.println("<head>");
  c.println(    "<title>MPC NEXA Webserver</title>");
  c.println(    "<style>");
  c.println(        "body { font-family: Arial, sans-serif; font-size:12px; }");
  c.println(    "</style>");
  c.println("</head>");
  c.println("<body>");
  c.println(    "<h1>MPC NEXA Webserver</h1>");
  c.println(    "<ul>");
  c.println(        "<li><a href=\"./?1-on\">Turn on power</a></li>");
  c.println(        "<li><a href=\"./?1-off\">Turn off power</a></li>");
  c.println(    "</ul>");
  c.println(    "<ul>");
  c.println(        "<li><a href=\"./?2-on\">Turn on power 1</a></li>");
  c.println(        "<li><a href=\"./?2-off\">turn off power 1</a></li>");
  c.println(    "</ul>");
  c.println(    "<ul>");
  c.println(        "<li><a href=\"./?3-on\">Turn on power 2</a></li>");
  c.println(        "<li><a href=\"./?3-off\">turn off power 2</a></li>");
  c.println(    "</ul>");
  c.println(    "<ul>");
  c.println(        "<li><a href=\"./?4-on\">Turn on power 3</a></li>");
  c.println(        "<li><a href=\"./?4-off\">turn off power 3</a></li>");
  c.println(    "</ul>");
  c.println(    "<hr>");
  c.println("</body>");
  c.println("</html>");
}

void httpResponseRedirect(EthernetClient c) {
  c.println("HTTP/1.1 301 Found");
  c.println("Location: /");
  c.println();
}

Your if/else syntax is invalid.

                    } 
                    else{ 
                        if(controlString.indexOf("?3-on") > -1) //checks for switchon
                        { 
                        transmitter.sendUnit(1, true);
                        httpResponseRedirect(client);
                        } 
                    } 
                    else{ 
                        if(controlString.indexOf("?3-off") > -1) //checks for switchoff 
                        { 
                        transmitter.sendUnit(1, false);
                        httpResponseRedirect(client);
                        } 
                    } 
                    else{ 

You've got a bunch of elses, all seemingly dependent on one if.

remove the { from the else{

and remove the } that was the one you had it matched with.

At least.

Go look at the BNF chart or wherever you go to see why you've screwed up on the basic syntax.

# include <stdio.h>

int a, b, c;


int main()
{
    b = 2;
    
    printf("Hello World!\n");
    
	if (a == 1) //checks for switchon
	{
	  printf("a is 1\n");
	}
	
	else if (b == 2) //checks for switchoff
	{
	  printf("b is 2\n");
	}
	
	else if (c == 3) //checks for switchon
	{
	  printf("c is 3\n");
	}
	
	else
	{
	  printf("Goodbye World!\n");
	}

    return 0;
}

a7

1 Like

Thanks man. Yeah, I'm not really good with if that then this commands and need to read up on the syntax, but your solution worked perfectly. Thanks!

Working code:

#include "SPI.h"
#include "Ethernet.h"
#include <NewRemoteTransmitter.h>

const unsigned long TRANSMITTER_ADDRESS = 27612394; //My personal transmitter address
const int PIN_TRANSMITTER = 8;

NewRemoteTransmitter transmitter(TRANSMITTER_ADDRESS, PIN_TRANSMITTER, 260, 3);

uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC Address
uint8_t ip[] = { 192,168,1,50 };                        // IP Address
EthernetServer server(80);                           // Server Port 80

// RemoteTransmitter configuration
// Connect Transmitter to Pin 11 and receiver to pin 2 (all digital)
// FAN 1 ON/OFF:
// Addr 27612394 unit 0 on, period: 267us.
// Addr 27612394 unit 0 off, period: 267us.
// FAN 2 ON/OFF:
// Addr 27612394 unit 1 on, period: 266us.
// Addr 27612394 unit 2 off, period: 267us.
// FAN 3 ON/OFF:
// Addr 27612394 unit 2 on, period: 267us.
// Addr 27612394 unit 1 off, period: 266us.
// ALL POWER OFF:
// Addr 27612394 group off, period: 267us.

String controlString; // Captures out URI querystring;;
int blueLEDPin = 2; // pin where our blue LED is connected
 
void setup(){
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  digitalWrite(3, HIGH);
  Ethernet.begin(mac, ip);
  server.begin();
  pinMode(PIN_TRANSMITTER, OUTPUT);
  Serial.begin(9600);
  Serial.print("Serial Write turned on");
}
 
void loop(){ 
    // Create a client connection 
    EthernetClient client = server.available(); 
    Serial.print("Launching Ethernet WebServer");
    if (client) { 
        while (client.connected()) { 
            if (client.available()) { 
                char c = client.read();
 
                //read the HTTP request 
                if (controlString.length() < 100) {
 
                    // write characters to string 
                    controlString += c; 
                }
 
                //if HTTP request has ended– 0x0D is Carriage Return \n ASCII 
                if (c == 0x0D) {
                    digitalWrite(4, HIGH);
                    httpResponseHome(client);
                    Serial.print("Website is up");
                    delay(10); 
                    //stopping client 
                    client.stop();
                    digitalWrite(4, LOW);
 
                    // control arduino pin 
                    if(controlString.indexOf("?1-on") > -1) //checks for switchon 
                    { 
                        transmitter.sendGroup(true);
                        httpResponseRedirect(client);
                    } 
                    else 
                        if(controlString.indexOf("?1-off") > -1) //checks for switchoff 
                        { 
                        transmitter.sendGroup(false);
                        httpResponseRedirect(client);
                        } 
                    
                    else 
                        if(controlString.indexOf("?2-on") > -1) //checks for switchon
                        { 
                        transmitter.sendUnit(0, true);
                        httpResponseRedirect(client);
                        } 
                     
                    else 
                        if(controlString.indexOf("?2-off") > -1) //checks for switchoff 
                        { 
                        transmitter.sendUnit(0, false);
                        httpResponseRedirect(client);
                        } 
                     
                    else 
                        if(controlString.indexOf("?3-on") > -1) //checks for switchon
                        { 
                        transmitter.sendUnit(1, true);
                        httpResponseRedirect(client);
                        } 
                     
                    else 
                        if(controlString.indexOf("?3-off") > -1) //checks for switchoff 
                        { 
                        transmitter.sendUnit(1, false);
                        httpResponseRedirect(client);
                        } 
                     
                    else 
                        if(controlString.indexOf("?4-on") > -1) //checks for switchon
                        { 
                        transmitter.sendUnit(2, true);
                        httpResponseRedirect(client);
                        } 
                     
                    else 
                        if(controlString.indexOf("?4-off") > -1) //checks for switchoff 
                        { 
                        transmitter.sendUnit(2, false);
                        httpResponseRedirect(client);
                        } 
                      
                    //clearing string for next read 
                    controlString="";
 
                } 
            } 
        } 
    } 
}

void httpResponseHome(EthernetClient c) {
  c.println("HTTP/1.1 200 OK");
  c.println("Content-Type: text/html");
  c.println();
  c.println("<html>");
  c.println("<head>");
  c.println(    "<title>MPC NEXA Webserver</title>");
  c.println(    "<style>");
  c.println(        "body { font-family: Arial, sans-serif; font-size:12px; }");
  c.println(    "</style>");
  c.println("</head>");
  c.println("<body>");
  c.println(    "<h1>MPC NEXA Webserver</h1>");
  c.println(    "<ul>");
  c.println(        "<li><a href=\"./?1-on\">Turn on power</a></li>");
  c.println(        "<li><a href=\"./?1-off\">Turn off power</a></li>");
  c.println(    "</ul>");
  c.println(    "<ul>");
  c.println(        "<li><a href=\"./?2-on\">Turn on power 1</a></li>");
  c.println(        "<li><a href=\"./?2-off\">turn off power 1</a></li>");
  c.println(    "</ul>");
  c.println(    "<ul>");
  c.println(        "<li><a href=\"./?3-on\">Turn on power 2</a></li>");
  c.println(        "<li><a href=\"./?3-off\">turn off power 2</a></li>");
  c.println(    "</ul>");
  c.println(    "<ul>");
  c.println(        "<li><a href=\"./?4-on\">Turn on power 3</a></li>");
  c.println(        "<li><a href=\"./?4-off\">turn off power 3</a></li>");
  c.println(    "</ul>");
  c.println(    "<hr>");
  c.println("</body>");
  c.println("</html>");
}

void httpResponseRedirect(EthernetClient c) {
  c.println("HTTP/1.1 301 Found");
  c.println("Location: /");
  c.println();
}

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