1

I have an arduino webserver that uses the following code:

client.println(F("HTTP/1.1 200 OK"));
client.println(F("Content-Type: text/html"));
client.println();
client.println(F("<HTML><HEAD>"));
...
if (fan1Toggle == 0)
    client.print(F("Fan1<font color=\"red\">&#8226;</font><a href=\"/?fan1on\">On</a><br>"));
else
    client.print(F("Fan1<font color=\"green\">&#8226;</font><a href=\"/?fan1off\">Off</a><br>"));

if (fan2Toggle == 0)
    client.print(F("Fan2<font color=\"red\">&#8226;</font><a href=\"/?fan2on\">On</a><br>"));
else
    client.print(F("Fan2<font color=\"green\">&#8226;</font><a href=\"/?fan2off\">Off</a><br>"));
...

The page is pretty long, allowing the user to toggle over a dozen different switches. Some of the code checks to see the state of a certain switch, and then displays a green or red bullet ("&#8226", like •) on the page if the switch is on or off.

Since the code for the colored bullet is repeating, I wanted to make a function for it. Then I would just call (for example) "displayBullet(fan1Toggle);", and it would determine if fan1 was on or off, and display the correct color depending on the state.

I tried to create the following function:

void displayBullet(int toggle) {
if (toggle == 0)     //switch is off, so display red bullet
    client.print(F("<font color=\"red\">&#8226;</font>"));
else    //switch is on, so display green bullet
    client.print(F("<font color=\"green\">&#8226;</font>"));
}

But then I got an error saying that "'client' is not declared in this scope".

Is there any way to work around that error, so that I can get the displayBullet() function I want? Or is this something that can't be shortened like this, because the "client" function has to be inside the main loop?

Thanks!

2

You can pass client to the function by reference, like this:

void displayBullet(EthernetClient & client, int toggle) 
  {
  if (toggle == 0)     //switch is off, so display red bullet
    client.print(F("<font color=\"red\">&#8226;</font>"));
  else    //switch is on, so display green bullet
    client.print(F("<font color=\"green\">&#8226;</font>"));
  }

Now it knows about client. Make sure you pass client when you call it:

displayBullet(client, 1);
1
  • Thanks a lot!! That's very useful information to know, and I'm sure it'll help me in the future with other similar problems. – Jerry Aug 5 '15 at 22:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.