How to randomly numbered string?

Hi all

I have a motion sensor which turns lights on and off.

I want it to randomly select colours from a few presets rather than show the same colour all the time.

It is called by

light_control(hue_on1);

and I want it to select from the folllowing:

String hue_on1 = "{\"on\":true, \"bri\":255, \"xy\":[0.3052,0.3305]}";
String hue_on2 = "{\"on\":true, \"bri\":150, \"xy\":[0.1450,0.01520]}";
String hue_on3 = "{\"on\":true, \"bri\":255, \"xy\":[0.1052,0.2205]}";

light_control is the prefix to hue_on:

bool light_control(String command)
{
  int retval = 0;     // return value
  WiFiClient client;  // WiFiClient class to create TCP connections
  
  if (!client.connect(bridge_ip, port))
  {
    Serial.println("ERR>> light_control - Connection failed");
    return -1;
  }
  
  // This will send PUT request to the server
  client.println("PUT /api/" + user + "/lights/" + light + "/state  HTTP/1.1");
  client.println("Host: " + String(bridge_ip) + ":" + String(port));
  client.println("User-Agent: ESP8266/1.0");
  client.println("Connection: keep-alive");
  client.println("Content-type: text/xml; charset=\"utf-8\"");
  client.print("Content-Length: ");
  client.println(command.length()); 
  client.println();
  client.println(command); 


  // Wait 10 seconds for server to respond
  unsigned long timeout = millis();
  while (client.available() == 0)
  {
    if (millis() - timeout > 10000)
    {
      Serial.println("ERR>> light_control - Client timeout");
      client.stop();
      return -1;
    }
  }

I have tried the following with no success:

int randNumber;

in the main loop I have added:

randNumber = random(1,3);

and I am calling it with the following:

light_control(hue_on(randNumber));

With that I get the following error:

esp8266Huemotion.ino: In function 'void loop()':

esp8266Huemotion:365: error: no match for call to '(String) (int&)'

           light_control2(hue_on(randNumber));

                                           ^

exit status 1
no match for call to '(String) (int&)'

Code anyone give me some pointers please? Whilst I'm asking for pointers, what are some good resources to learn programming arduinos?

motion.ino (10.6 KB)

Holy crap I'm an idiot. Straight after I posted this thread I added a plus sign to the light_control command

light_control(hue_on+(randNumber));

It compiles but I can't test for a few days until I get another esp8266

Please post your entire sketch, not just snippets.

http://snippets-r-us.com/

odometer:
Please post your entire sketch, not just snippets.

http://snippets-r-us.com/

I tried but it goes over the 9000 character limit on this forum. I've added it here instead:

It turns on 2 lights but only checks to see if one of them is switched on or not because they are next to each other so no need for 2 checks. It turns the lights off after 25 seconds.

Light 14 I want to stay white and light 26 I want to change to random preset colours.

It's not my base code and I'm still learning. Frankly I'm amazed I managed to add another light just by jigging the code a bit. There are probably a lot cleaner and efficient ways of doing this.

I tried but it goes over the 9000 character limit on this forum

So attach the code to a post

Delta_G:
Why would you come on a forum and start posting without reading this:

How to use this forum - please read

Did you think snubbing the forum etiquette would make friends for you?

Could you highlight the exact forum etiquette I have snubbed please ?

The only bit I can think of is this: Post a complete sketch (program code)! If you don't you waste time while people ask you to do that. However, with coding problems, if possible post a "minimal" sketch that demonstrates the problem - not hundreds of lines of code. If the problem goes away in the minimal sketch, it wasn't where you thought it was.

I have explained why I didn't and tried to paraphrase the code due to the forum limitation. If I have done anything wrong then I apologise.

99% of forums I have been on send you a welcome message telling you where the rules are. Am I supposed to search through Arduino Forum > Using Arduino > Installation & Troubleshooting to find the rules thread? It's not exactly the easiest thread to find.

Am I supposed to search through Arduino Forum > Using Arduino > Installation & Troubleshooting to find the rules thread? It's not exactly the easiest thread to find.

The same post is at the top of EVERY section of the forum.

That post tells you what to do if your code is too large to post inline.

PaulS:
The same post is at the top of EVERY section of the forum.

That post tells you what to do if your code is too large to post inline.

OK admittedly I didn't notice it at the top so didn't read it. Regardless, I have provided the full code via a pastebin link.

Before posting I read other threads without the full code posted and no one batted an eyelid.

Delta_G:
Ok. Maybe someone at pastebin will help you. Most folks here will just go to another thread before they'll go offsite to hunt down your code.

If you feel you have been treated unfairly then you should ask the forum for your money back. Oh wait, it's free.

Look you can bitch about it or you can realize that folks volunteered to help you but they don't have to. You can insist on it being your way, but I would encourage you to hunt down some other threads where someone took that tactic. They almost always are just abandoned. It's entirely up to you. Play along or complain and see which gets more people to help you.

At the moment you are the only person who has helped and for that I am grateful. I do feel a welcome message for new members would help though.