Setting values not working as expected - noob needs help

Hi all,

I need your help again.

This code works for me and adresses the PWM correctly. Also for big numbers like 2000.

SYSTEM_MODE ( MANUAL );

#include "libraries/WebServer/WebServer.h"
#include "libraries/PCA/Adafruit_PCA9685.h"
#include "libraries/Timer/SparkCorePolledTimer.h"
#include <stdio.h>

#define PREFIX "/box"

#define Red 3
#define Green 0
#define Blue 4
#define Infrared 1
#define White 2
//#define buffer 128


Adafruit_PCA9685 LedDriver = Adafruit_PCA9685 ( 0x40, true );

WebServer webserver ( PREFIX, 80 );

SparkCorePolledTimer updateTimer_0 ( 10000 );
SparkCorePolledTimer updateTimer_1 ( 10000 );
SparkCorePolledTimer updateTimer_2 ( 10000 );
SparkCorePolledTimer updateTimer_3 ( 10000 );

void OnTimer_0 ( void );
void OnTimer_1 ( void );
void OnTimer_2 ( void );
void OnTimer_3 ( void );

int i = 0;

int enable = 0;

int repeat_0 = 0;

int timing_0 = 0;
int timing_1 = 0;
int timing_2 = 0;
int timing_3 = 0;

int red_0 = 0;
int red_h_0 = 0;

int red_1 = 0;
int red_h_1 = 0;

int red_2 = 0;
int red_h_2 = 0;

int red_3 = 0;
int red_h_3 = 0;

int blue_0 = 0;
int blue_h_0 = 0;

int blue_1 = 0;
int blue_h_1 = 0;

int blue_2 = 0;
int blue_h_2 = 0;

int blue_3 = 0;
int blue_h_3 = 0;

int green_0 = 0;
int green_h_0 = 0;

int green_1 = 0;
int green_h_1 = 0;

int green_2 = 0;
int green_h_2 = 0;

int green_3 = 0;
int green_h_3 = 0;

int infrared_0 = 0;
int infrared_h_0 = 0;

int infrared_1 = 0;
int infrared_h_1 = 0;

int infrared_2 = 0;
int infrared_h_2 = 0;

int infrared_3 = 0;
int infrared_h_3 = 0;

int white_0 = 0;
int white_h_0 = 0;

int white_1 = 0;
int white_h_1 = 0;

int white_2 = 0;
int white_h_2 = 0;

int white_3 = 0;
int white_h_3 = 0;

int timer_0 = 0;
int timer_1 = 0;
int timer_2 = 0;
int timer_3 = 0;


void boxCmd ( WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete ) {

  if ( type == WebServer::POST ) {

    bool repeat;
    char name [ 16 ], value [ 16 ];

    do {

      repeat = server.readPOSTparam ( name, 16, value, 16 );

      if ( strcmp ( name, "enable" ) == 0 ) {

        enable = strtoul ( value, NULL, 10 );
      }

      if ( strcmp ( name, "red_0" ) == 0 ) {

        red_0 = strtoul ( value, NULL, 10 );

        red_h_0= red_0;

        red_0 = ( int ) ( ( red_0 + 0.979 ) / 1.087 );
      }

      if ( strcmp ( name, "green_0" ) == 0 ) {

        green_0 = strtoul ( value, NULL, 10 );

        green_h_0 = green_0;

        green_0 = ( int ) ( ( green_0 - 0.239 ) / 1.130 );
      }

      if ( strcmp ( name, "blue_0" ) == 0 ) {

        blue_0 = strtoul ( value, NULL, 10 );

        blue_h_0 = blue_0;

        blue_0 = ( int ) ( ( blue_0 + 1.308 ) / 2.713 );
      }

      if ( strcmp ( name, "infrared_0" ) == 0 ) {

        infrared_0 = strtoul ( value, NULL, 10 );

        infrared_h_0 = infrared_0;

        infrared_0 = ( int ) ( ( infrared_0 - 0.617 ) / 2.609 );
      }

      if ( strcmp ( name, "white_0" ) == 0 ) {

        white_0 = strtoul ( value, NULL, 10 );

        white_h_0 = white_0;

        white_0 = ( int ) ( ( white_0 + 6.701 ) / 13.771 );
      }

      if ( strcmp ( name, "timer_0" ) == 0 ) {

        timer_0 = strtoul ( value, NULL, 10 );
      }

      if ( strcmp ( name, "red_1" ) == 0 ) {

        red_1 = strtoul ( value, NULL, 10 );

        red_h_1 = red_1;

        red_1 = ( int ) ( ( red_1 + 0.979 ) / 1.087 );
      }

      if ( strcmp ( name, "green_1" ) == 0 ) {

        green_1 = strtoul ( value, NULL, 10 );

        green_h_1 = green_1;

        green_1 = ( int ) ( ( green_1 - 0.239 ) / 1.130 );
      }

      if ( strcmp ( name, "blue_1" ) == 0 ) {

        blue_1 = strtoul ( value, NULL, 10 );

        blue_h_1 = blue_1;

        blue_1 = ( int ) ( ( blue_1 + 1.308 ) / 2.713 );
      }

      if ( strcmp ( name, "infrared_1" ) == 0 ) {

        infrared_1 = strtoul ( value, NULL, 10 );

        infrared_h_1 = infrared_1;

        infrared_1 = ( int ) ( ( infrared_1 - 0.617 ) / 2.609 );
      }

      if ( strcmp ( name, "white_1" ) == 0 ) {

        white_1 = strtoul ( value, NULL, 10 );

        white_h_1 = white_1;

        white_1 = ( int ) ( ( white_1 + 6.701 ) / 13.771 );
      }

      if ( strcmp ( name, "timer_1" ) == 0 ) {

        timer_1 = strtoul ( value, NULL, 10 );
      }

      if ( strcmp ( name, "red_2" ) == 0 ) {

        red_2 = strtoul ( value, NULL, 10 );

        red_h_2 = red_2;

        red_2 = ( int ) ( ( red_2 + 0.979 ) / 1.087 );
      }

      if ( strcmp ( name, "green_2" ) == 0 ) {

        green_2 = strtoul ( value, NULL, 10 );

        green_h_2 = green_2;

        green_2 = ( int ) ( ( green_2 - 0.239 ) / 1.130 );
      }

      if ( strcmp ( name, "blue_2" ) == 0 ) {

        blue_2 = strtoul ( value, NULL, 10 );

        blue_h_2 = blue_2;

        blue_2 = ( int ) ( ( blue_2 + 1.308 ) / 2.713 );
      }

      if ( strcmp ( name, "infrared_2" ) == 0 ) {

        infrared_2 = strtoul ( value, NULL, 10 );

        infrared_h_2 = infrared_2;

        infrared_2 = ( int ) ( ( infrared_2 - 0.617 ) / 2.609 );
      }

      if ( strcmp ( name, "white_2" ) == 0 ) {

        white_2 = strtoul ( value, NULL, 10 );

        white_h_2 = white_2;

        white_2 = ( int ) ( ( white_2 + 6.701 ) / 13.771 );
      }

      if ( strcmp ( name, "timer_2" ) == 0 ) {

        timer_2 = strtoul ( value, NULL, 10 );
      }

      if ( strcmp ( name, "red_3" ) == 0 ) {

        red_3 = strtoul ( value, NULL, 10 );

        red_h_3 = red_3;

        red_3 = ( int ) ( ( red_3 + 0.979 ) / 1.087 );
      }

      if ( strcmp ( name, "green_3" ) == 0 ) {

        green_3 = strtoul ( value, NULL, 10 );

        green_h_3 = green_3;

        green_3 = ( int ) ( ( green_3 - 0.239 ) / 1.130 );
      }

      if ( strcmp ( name, "blue_3" ) == 0 ) {

        blue_3 = strtoul ( value, NULL, 10 );

        blue_h_3 = blue_3;

        blue_3 = ( int ) ( ( blue_3 + 1.308 ) / 2.713 );
      }

      if ( strcmp ( name, "infrared_3" ) == 0 ) {

        infrared_3 = strtoul ( value, NULL, 10 );

        infrared_h_3 = infrared_3;

        infrared_3 = ( int ) ( ( infrared_3 - 0.617 ) / 2.609 );
      }

      if ( strcmp ( name, "white_3" ) == 0 ) {

        white_3 = strtoul ( value, NULL, 10 );

        white_h_3 = white_3;

        white_3 = ( int ) ( ( white_3 + 6.701 ) / 13.771 );
      }

      if ( strcmp ( name, "timer_3" ) == 0 ) {

        timer_3 = strtoul ( value, NULL, 10 );
      }

      if ( strcmp ( name, "repeat_0" ) == 0 ) {

        repeat_0 = strtoul ( value, NULL, 10 );
      }

    } while ( repeat );

    server.httpSeeOther ( PREFIX );
    return;
  }

  server.httpSuccess ( );

  if ( type == WebServer::GET ) {
    #define BUFFER_SIZE 3000
    char buffer[BUFFER_SIZE];


    //P ( message ) =
//#define buffer "<p> Red in µm photons/(m*s): <input type='number' name='red_0' min='0' max='2000' value='%i'>" <script> var r = document.getElementById("Rot"); var b = document.getElementById("Blau"); </script>

//server.write((uint8_t*)buffer,strlen(buffer));

//char js[512];
//snprintf(js, sizeof(js), "<script>r.value=%d;b.value=%d;</script></html>", red_0, blue_0);
//server.write((uint8_t*)js, strlen(js));



    snprintf(buffer, BUFFER_SIZE, "<html><head><title>IAB Lightbox Control-Panel</title>"
    "<body>"
    "<h1>IAB Lightbox 5 Control-Panel</h1>"
    "<form action='/box' method='POST'>"
    "<p> Red in µm photons/(m²*s): <input type='number' name='red_0' min='0' max='2000' value='%i'>"
    "Green in µm photons/(m²*s): <input type='number' name='green_0' min='0' max='2000'value='%i'>"
    "Blue in µm photons/(m²*s): <input type='number' name='blue_0' min='0' max='2000' value='%i'>"
    "Infrared in µm photons/(m²*s): <input type='number' name='infrared_0' min='0' max='2000' value='%i'>"
    "White in µm photons/(m²*s): <input type='number' name='white_0' min='0' max='2000' value='%i'>"
    "Timer in min: <input type='number' name='timer_0' min='0' max='600000' value='%i'> </p>"
    "<p> Red in µm photons/(m²*s): <input type='number' name='red_1' min='0' max='2000' value='%i'>"
    "Green in µm photons/(m²*s): <input type='number' name='green_1' min='0' max='2000' value='%i'>"
    "Blue in µm photons/(m²*s): <input type='number' name='blue_1' min='0' max='2000' value='%i'>"
    "Infrared in µm photons/(m²*s): <input type='number' name='infrared_1' min='0' max='2000' value='%i'>"
    "White in µm photons/(m²*s): <input type='number' name='white_1' min='0' max='2000' value='%i'>"
    "Timer in min: <input type='number' name='timer_1' min='0' max='600000' value='%i'></p>"
    "<p> Red in µm photons/(m²*s): <input type='number' name='red_2' min='0' max='2000' value='%i'>"
    "Green in µm photons/(m²*s): <input type='number' name='green_2' min='0' max='2000' value='%i'>"
    "Blue in µm photons/(m²*s): <input type='number' name='blue_2' min='0' max='2000' value='%i'>"
    "Infrared in µm photons/(m²*s): <input type='number' name='infrared_2' min='0' max='2000' value='%i'>"
    "White in µm photons/(m²*s): <input type='number' name='white_2' min='0' max='2000' value='%i'>"
    "Timer in min: <input type='number' name='timer_2' min='0' max='600000' value='%i'> </p>"
    "<p> Red in µm photons/(m²*s): <input type='number' name='red_3' min='0' max='2000' value='%i'>"
    "Green in µm photons/(m²*s): <input type='number' name='green_3' min='0' max='2000' value='%i'>"
    "Blue in µm photons/(m²*s): <input type='number' name='blue_3' min='0' max='2000' value='%i'>"
    "Infrared in µm photons/(m²*s): <input type='number' name='infrared_3' min='0' max='2000' value='%i'>"
    "White in µm photons/(m²*s): <input type='number' name='white_3' min='0' max='2000' value='%i'>"
    "Timer in min: <input type='number' name='timer_3' min='0' max='600000' value='%i'> </p> "
    "<p> <button name='enable' value='0' onclick='window.location.reload();'>Reset</button>"
    "<button name='enable' value='1' onclick='window.location.reload();'>Start</button> </p>"
    "Repeat: <input type='number' name='repeat_0' min='0' max='1' value='%i'>"
    "<p> Christian Streng & Christian Karle &copy; 2019 </p>"
    "</form></body></html>", red_h_0, green_h_0, blue_h_0, infrared_h_0, white_h_0, timer_0, red_h_1, green_h_1, blue_h_1, infrared_h_1, white_h_1, timer_1, red_h_2, green_h_2, blue_h_2, infrared_h_2, white_h_2, timer_2, red_h_3, green_h_3, blue_h_3, infrared_h_3, white_h_3, timer_3, repeat_0 ) ;
    server.write((uint8_t *)buffer, strlen(buffer));


    //server.printP ( message );
  }
}

void setup ( ) {

  WiFi.off ( );
  IPAddress myAddress ( 192, 168, 5, 204 );
  IPAddress gateway ( 192, 168, 5, 100 );
  IPAddress netmask ( 255, 255, 255, 0 );
  IPAddress dns ( 8, 8, 8, 8 );
  WiFi.setStaticIP ( myAddress, netmask, gateway, dns );
  WiFi.useStaticIP ( );
  WiFi.on ( );
  WiFi.connect ( );
  waitUntil ( WiFi.ready );

  LedDriver.begin ( );
  LedDriver.setPWMFreq ( 300 );

  LedDriver.setVal ( Red, 0 );
  LedDriver.setVal ( Green, 0 );
  LedDriver.setVal ( Blue, 0 );
  LedDriver.setVal ( Infrared, 0 );
  LedDriver.setVal ( White, 0 );

  updateTimer_0.SetCallback ( OnTimer_0 );
  updateTimer_1.SetCallback ( OnTimer_1 );
  updateTimer_2.SetCallback ( OnTimer_2 );
  updateTimer_3.SetCallback ( OnTimer_3 );

  webserver.setDefaultCommand ( &boxCmd );

  webserver.begin ( );
}

void loop ( ) {

  if ( enable == 1 ) {

    while ( i == 0 ) {

      updateTimer_0.SetTimeout ( 1000 * 60 * timer_0 );
      updateTimer_1.SetTimeout ( 1000 * 60 * timer_1 );
      updateTimer_2.SetTimeout ( 1000 * 60 * timer_2 );
      updateTimer_3.SetTimeout ( 1000 * 60 * timer_3 );

      LedDriver.setVal ( Red, red_0 );
      LedDriver.setVal ( Green, green_0 );
      LedDriver.setVal ( Blue, blue_0 );
      LedDriver.setVal ( Infrared, infrared_0 );
      LedDriver.setVal ( White, white_0 );

      updateTimer_0.Reset ( );

      i = 1;

      timing_0 = 1;
    }

    updateTimer_0.Update ( );
    updateTimer_1.Update ( );
    updateTimer_2.Update ( );
    updateTimer_3.Update ( );
  }

  if ( enable == 0 ) {

    i = 0;

    repeat_0 = 0;

    updateTimer_0.Reset ( );
    updateTimer_1.Reset ( );
    updateTimer_2.Reset ( );
    updateTimer_3.Reset ( );

    LedDriver.setVal ( Red, 0 );
    LedDriver.setVal ( Green, 0 );
    LedDriver.setVal ( Blue, 0 );
    LedDriver.setVal ( Infrared, 0 );
    LedDriver.setVal ( White, 0 );

    red_0 = 0;
    red_h_0 = 0;

    red_1 = 0;
    red_h_1 = 0;

    red_2 = 0;
    red_h_2 = 0;

    red_3 = 0;
    red_h_3 = 0;

    blue_0 = 0;
    blue_h_0 = 0;

    blue_1 = 0;
    blue_h_1 = 0;

    blue_2 = 0;
    blue_h_2 = 0;

    blue_3 = 0;
    blue_h_3 = 0;

    green_0 = 0;
    green_h_0 = 0;

    green_1 = 0;
    green_h_1 = 0;

    green_2 = 0;
    green_h_2 = 0;

    green_3 = 0;
    green_h_3 = 0;

    infrared_0 = 0;
    infrared_h_0 = 0;

    infrared_1 = 0;
    infrared_h_1 = 0;

    infrared_2 = 0;
    infrared_h_2 = 0;

    infrared_3 = 0;
    infrared_h_3 = 0;

    white_0 = 0;
    white_h_0 = 0;

    white_1 = 0;
    white_h_1 = 0;

    white_2 = 0;
    white_h_2 = 0;

    white_3 = 0;
    white_h_3 = 0;

    timer_0 = 0;
    timer_1 = 0;
    timer_2 = 0;
    timer_3 = 0;

    timing_0 = 0;
    timing_1 = 0;
    timing_2 = 0;
    timing_3 = 0;
  }

  webserver.processConnection ( );
}

void OnTimer_0 ( void ) {

  if ( timing_0 == 1 ) {

    LedDriver.setVal ( Red, 0 );
    LedDriver.setVal ( Green, 0 );
    LedDriver.setVal ( Blue, 0 );
    LedDriver.setVal ( Infrared, 0 );
    LedDriver.setVal ( White, 0 );

    LedDriver.setVal ( Red, red_1 );
    LedDriver.setVal ( Green, green_1 );
    LedDriver.setVal ( Blue, blue_1 );
    LedDriver.setVal ( Infrared, infrared_1 );
    LedDriver.setVal ( White, white_1 );

    updateTimer_1.Reset ( );

    timing_0 = 0;
    timing_1 = 1;
  }
}

void OnTimer_1 ( void ) {

  if ( timing_1 == 1 ) {

    LedDriver.setVal ( Red, 0 );
    LedDriver.setVal ( Green, 0 );
    LedDriver.setVal ( Blue, 0 );
    LedDriver.setVal ( Infrared, 0 );
    LedDriver.setVal ( White, 0 );

    LedDriver.setVal ( Red, red_2 );
    LedDriver.setVal ( Green, green_2 );
    LedDriver.setVal ( Blue, blue_2 );
    LedDriver.setVal ( Infrared, infrared_2 );
    LedDriver.setVal ( White, white_2 );

    updateTimer_2.Reset ( );

    timing_1 = 0;
    timing_2 = 1;
  }
}

void OnTimer_2 ( void ) {

  if ( timing_2 == 1 ) {

    LedDriver.setVal ( Red, 0 );
    LedDriver.setVal ( Green, 0 );
    LedDriver.setVal ( Blue, 0 );
    LedDriver.setVal ( Infrared, 0 );
    LedDriver.setVal ( White, 0 );

    LedDriver.setVal ( Red, red_3 );
    LedDriver.setVal ( Green, green_3 );
    LedDriver.setVal ( Blue, blue_3 );
    LedDriver.setVal ( Infrared, infrared_3 );
    LedDriver.setVal ( White, white_3 );

    updateTimer_3.Reset ( );

    timing_2 = 0;
    timing_3 = 1;
  }
}

void OnTimer_3 ( void ) {
  if ( timing_3 == 1 ) {

    LedDriver.setVal ( Red, 0 );
    LedDriver.setVal ( Green, 0 );
    LedDriver.setVal ( Blue, 0 );
    LedDriver.setVal ( Infrared, 0 );
    LedDriver.setVal ( White, 0 );

    timing_3 = 0;

    if ( repeat_0 == 1 ) {

      i = 0;
    }
  }
}

If i change the code to by changing the equation:

      if ( strcmp ( name, "red_0" ) == 0 ) {

        red_0 = strtoul ( value, NULL, 10 );

        red_h_0= red_0;

        red_0 = ( int ) ( ( red_0 - 1.1121 ) / 0.0086 );
      }

      if ( strcmp ( name, "green_0" ) == 0 ) {

        green_0 = strtoul ( value, NULL, 10 );

        green_h_0 = green_0;

        green_0 = ( int ) ( ( green_0 - 0.293 ) / 0.0099 );
      }

      if ( strcmp ( name, "blue_0" ) == 0 ) {

        blue_0 = strtoul ( value, NULL, 10 );

        blue_h_0 = blue_0;

        blue_0 = ( int ) ( ( blue_0 - 0.1621 ) / 0.0197 );
      }

      if ( strcmp ( name, "infrared_0" ) == 0 ) {

        infrared_0 = strtoul ( value, NULL, 10 );

        infrared_h_0 = infrared_0;

        infrared_0 = ( int ) ( ( infrared_0 - 0.6229 ) / 0.0264 );
      }

      if ( strcmp ( name, "white_0" ) == 0 ) {

        white_0 = strtoul ( value, NULL, 10 );

        white_h_0 = white_0;

        white_0 = ( int ) ( ( white_0 - 4.7611 ) / 0.1155 );
      }

it won’t work anymore. I’m adressing LED lights with it. So in the first version of the code the colors are adressed perfectly fine, but in the second all the colors are lighning up.

Thank you in advance. I hope someone can explain to me, since I’m totally stuck and don’t know why it does not work. Using float was also not effective. Also I didn’t receive any errors in the code.

Cheers,

Silenz

What does that mean?

One thing that springs to mind tho' is that you are adding in the original code and subtracting in your changed code.
Since your xxx_0 values are preset to zero your result will be negative which is probably the reason for your problems.

However, that code is ripe for a major overhaul to remove code redundancies.
I'm pretty sure this code can be shortened by 70%.

2 Likes

Thank you for your reply.

The microcontroler controls differen LED lights on the panel. The color indicates the monochromatic lights beeing red, blue, infrared, white and green.

If I use the HTML interface for the first code and write 2000 for the red_0 value and submit it, then the red LED lights up, which is how I want it to work. If I use the second code and enter for example 10, it will no only adress the red light, but also blue, green, infrared and white.

True, the code is pretty bad. I’m not from the field and just use it as means to have a LED panel for samples.

I set them to zero at first to have all the lights turned off. I thought if I calculate 2000 with code 2 or enter 2000 with code 1 it should be the same, but somehow its not.

You could add some Serial.print() statements to check what is happening with the other values before and after you change one.

Could you also explain what your code is intended to do?
What are the four timers for?
Are they ever meant to run concurrently or only ever one at a time?
What are these hardcoded offsets and factors for?
What are the LEDs supposed to do after you submitted and parsed the HTML form?

This would help streamline your code tremendously.

1 Like

Thank you!

If I understand correctly, serial.print(red_0) before and after calculation would give me the ongoing information?

Could you also explain what your code is intended to do?|

Yes, I have a LED panel with the Photon P1 and different LEDs. The LEDs are all monochromatic and emit a specific wavelength. That's why the colors are named blue, red, infrared, white and green. I want to control these LEDs over a local website IP adress /box in order to set the intensity and which light color is running. With the timer I want to be able to illuminate for example 5 min or 10 min, but also 12h dark 12h light cycles are wanted. These equations and values refer to the intensity of the LED, which we measured after we used raw values before we used code 1. So basically code 0 was no equations. Now after calculating the fit for the raw value and the intensity, we came up with the equations of code 2 . We want to submit for example photonflux of 200 µM photon/m²s and the program should calculate the raw value which is needed to match this flux.

What are the four timers for?

They control block of operation for example 1 min red light with an intensity of 200 µM photns /m²s. Then they step to the next block which might be nothing or maybe something else like green or so. Sadly the timer don't count down in the HTML, but they work for the Photon.

What are these hardcoded offsets and factors for?

For the timer? They were like this in the code I looked up. The *60 was to convert seconds to minutes.

What are the LEDs supposed to do after you submitted and parsed the HTML form?

The Led should emit light in the desired intesity and wavelength/color for the set time. Afterwards when the time ran out it should stop. To reset it there's a button. Saddly the repeat only works with 0 and 1, but I would liked to have a switch in the HTML.

What are these hardcoded offsets and factors for?

Correct, but when your problem is that you intentionally alter red_0 but observe changes on the other variables you may want to print them too.
BTW, if you use Serial.printlnf() you can have multiple values in one statement and format them conveniently (with a line feed at/new line the end).

Actually I was referring to the offsets and factors (resp. divisors) in your colour formulas. But the explanation above clarified that already.

Also what are the four different categories for red_0, _1, _2, _3?
Are these some sort of intensity level over time? Start with _0 for t1 minutes, followed by _1 for t2 minutes, followed by _2 ...?

1 Like

Correct, but when your problem is that you intentionally alter red_0 but observe changes on the other variables you may want to print them too.

Yes, formerly the input values in the HTML would disappear. The red_h_0 helps me to retain the input in the box.

Actually I was referring to the offsets and factors (resp. divisors) in your colour formulas. But the explanation above clarified that already.

My bad sorry, that's just the formula to convert bit to photon flux of the LED

Also what are the four different categories for red_0, _1, _2, _3?

In the HTML I have four rows with boxes to enter my values for the intensity and time. red_0, green_0 etc. are for the first row of the table which are controlled by timer_0. The second row red_1 etc: is controlled by the timer_1 and so on. With this I can tell the box to switch lights for example 1min white 1 min dark would be white_0 = 2000, timer__0=1 and all other 0 except timer_1=1. With repeat on it would cycle infinetely from white light to darkness every minute.

Are these some sort of intensity level over time? Start with _0 for t1 minutes, followed by _1 for t2 minutes, followed by _2 …?

The idea is to have a hirachie for the timers. First the first one counts down then the second one and so on while each timer has its values for the light intensities.

Thank you so much for your help! I'll try the Serial.printlnf()

I tried to recreate your code’s behaviour in a somewhat more concise manner :wink:

Give this a spin and see whether this does what it should
https://go.particle.io/shared_apps/60a56c26de87cd0016df0811

1 Like

Thank you! That looks like a huge load of work for you. I’m very grateful for the help!

I’ll read on the details of the code you wrote and try to learn from that.

Problem now is, that the LED breathes white, which is new to me. Formerly it was breathing green. If I type 192.168.5.204 with or without the suffix /box in the browser it won’t connect. What did I do wrong? Do I need to put it to listen mode and reconfigure the WIFI?

It might be that my STARTUP() code does not execute as intended.
Since I’m not using static IP on my Photons I had disabled that part of the code for my tests.
Breathing white means the WiFi modem is not even switched on.

This is the probable solution
change

STARTUP(wifiSetup);

to

STARTUP(wifiSetup());

:blush:

1 Like

Thank you that was the trick for the WIFI on.

If I set the timer to 1 or another number, it will not turn on the light. Instead it shows 0 as value after start. For the light colors the numbers stay the same as they should be after start.

I’d have to test this a bit more, but adding some Serial.print() statements in strategically useful places may help you do it from your end too :wink:

1 Like

I'm trying, but it always gives me timer is not declared. Yet in the html we have timer_1 or timer in the code. Which variable do I need to print?

Thank you so much! Sorry for beeing so unexperienced :see_no_evil:

All xxxx_# variables have been substituted for much more flexible array fields and I also moved the states up by one to have state 0 as a “disabled” state while previous _3 would now be _4 but in an array.
Consequently, if you are looking for what used to be timer_1 you’d now use ledStates[2].duration.
Simliarly what used to be the base colour red_h_3 would now be ledStates[4].base[cRED].


I found some bugs in my code :blush:
This should be working now.
https://go.particle.io/shared_apps/60a6e65ade87cd0008df1719

I left in my test setup to add local feedback on the onboard RGB LED to show what the current RGB values should be.

1 Like

Thank you!

I see, I didn’t undestand the array, but then it’s clear to me how to readit.

After I reset the status LED blinks green like it wants to connect to the WIFI, but afterwards there is no reaction anymore. The HTML site will also not load.

Interestingly over Windows CMD ping 195.168.5.204 I can reach it.

Oh drat! I left the waitUntil(Particle.connected) call in setup() as I was testing with a non-static IP and hence wanted to local IP exposed as Particle.variable().

Since there is no Particle.connect() call, this will wait indefinetly - I was testing with SYSTEM_MODE(AUTOMATIC) :man_facepalming:

1 Like

Thank you, I deleted the waitUntil(Particle.connected) and now all the lights work!

Now only one minor problem. The values are retained in the box as they should be, but after reset it should be 0 for all the values. Now it shows the values still while LEDs are off.The reason behind this is that one should be able to load the page and see if the LED are turned off or which LED are one with the indicated value.

Would it be possible to have instead for repeat a on and zero something like a butto to switch in the HTML?

I'd actually do that on the browser side.

However, you can achieve this on the device by simply changing

      else if (!strcmp(name, "enable")) {
        curState = atoi(value);
      }

to

      else if (!strcmp(name, "enable")) {
        if (!(curState = atoi(value)))                              // if "enable" is false
          memset(ledStates, 0, sizeof(ledStates));                  //   reset the letStates to 0 
      }

But to achive this ...

you'd also need some feedback from the device which state the device currently is in and also how far it is into that state's period.

Once you have that you could also mark the HTML block tied to the current state as active (e.g. by high lighting the background).

1 Like

Ah it’s already perfect! It works nice!

Since the code or better to say the hole panel is used in scientific experiments (biology), I would like to cite you in the paper. If that is fine for you?

1 Like

Yup, that’s perfectly fine :+1:

I’ll give the web page a little polish and submit the code here in the post rather than just a link which may break some time from now.

1 Like