Hello community,
I 'm working from few month on a LED lighting project controled by a spark for my reef tank.
I have 2 issues that i can’t solve. Maybe you are an expert in C and you can help me
first issue :
when i call my sunrise or sunset function via the webAPI, i always have a timeout message . My function can take long time to execute, but is it possible to send a return code when the function start his job ?
second issue :
the sunrise and sunset function are not smooth. Sunrise has some flash ( led are flashing one time before starting fade in ) and Sunset stop all light, but don’t fade .
the code :
// Light Reef Tank project
/* remote commands
// make sunrise
curl -k https://api.spark.io/v1/devices/50ff6xxxxxx/sunlight \
-d access_token=xxx \
-d "args=sunrise"
// make sunset
curl -k https://api.spark.io/v1/devices/50ff6xxxxxx/sunlight \
-d access_token=xxx \
-d "args=sunset"
// get last command
curl -k "https://api.spark.io/v1/devices/50ff6xxxxxx/lastcommand?access_token=xxx"
*/
// pin of white lines and pwm value
int8_t white[3][2]={{A5, 0}, {A6, 0}, {A7, 0}};
// pin of blue lines and pwm value
int8_t blue[3][2]={{A0, 0}, {A1, 0}, {A4, 0}};
// time for sunrise/sunset in minutes
unsigned int fadetime = 1;
// maximum Sun intensity in percent
unsigned int sunmaxintensity = 100;
// maximum Moon intensity in percent
unsigned int moonintensity = 1;
// moon line (blue[1])
unsigned int moonline = 1;
// String to hold last command issued
char lastCommand[64] = "NONE";
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//declare global if you don't want it reset every time loop runs
//elapsedMillis timeElapsed = 0;
elapsedMillis timeElapsed2 = 0;
// nb white lines
unsigned int nbwhite = 3; //sizeof(white);
// nb blue lines
unsigned int nbblue = 3; //sizeof(blue);
// maximum intensity from 0 to 255
unsigned int maxint = ( sunmaxintensity * 255 / 100 );
// delay in milliseconds between two fade value
unsigned int btwfadeval = ( fadetime * 60 * 1000 ) / ( ( nbwhite + nbblue ) * maxint );
// This routine runs only once upon reset
void setup()
{
// Initialize white pins as an output
for (int i = 0; i <= ( nbwhite - 1 ); i++){
pinMode(white[i][0], OUTPUT);
}
// Initialize blue pins as an output
for (int i = 0; i <= ( nbblue - 1 ); i++){
pinMode(blue[i][0], OUTPUT);
}
// Expose Sun() to the cloud
Spark.function("sunlight", SunLight);
// Expose lastCommand to the cloud
Spark.variable("lastcommand", &lastCommand, STRING);
}
// This routine loops forever
void loop()
{
// nothing to do
}
int8_t fadin(uint8_t line, uint8_t startval, uint8_t stopval, uint8_t atimebtwfade)
{
int8_t fadeValue;
for(fadeValue = startval ; fadeValue <= stopval ; fadeValue++) {
// sets the value (range from 0 to 255):
analogWrite(line, fadeValue);
// wait for 3 seconds to see the dimming effect
delay(atimebtwfade);
}
return fadeValue;
}
int8_t fadout(uint8_t line, uint8_t startval, uint8_t stopval, uint8_t atimebtwfade)
{
int8_t fadeValue;
for(fadeValue = startval ; fadeValue >= stopval ; fadeValue--) {
// sets the value (range from 255 to 0):
analogWrite(line, fadeValue);
// wait for 3 seconds to see the dimming effect
delay(atimebtwfade);
}
return fadeValue;
}
void sunrise()
{
//sunrise
for (int i = 0; i <= ( nbblue - 1 ); i++){
blue[i][1]=fadin(blue[i][0], blue[i][1], maxint, btwfadeval);
}
for (int i = 0; i <= ( nbwhite - 1 ); i++){
white[i][1]=fadin(white[i][0], white[i][1], maxint, btwfadeval);
}
}
void sunset()
{
// sunset
for (int i = ( nbwhite - 1 ); i >= 0; i--){
white[i][1]=fadout(white[i][0], white[i][1], 0, btwfadeval);
}
for (int i = ( nbblue - 1 ); i >= 0; i--){
blue[i][1]=fadout(blue[i][0], blue[i][1], 0, btwfadeval);
}
}
void moon()
{
// sunset
for (int i = ( nbwhite - 1 ); i >= 0; i--){
white[i][1]=fadout(white[i][0], white[i][1], 0, btwfadeval);
}
for (int i = ( nbblue - 1 ); i >= 0; i--){
blue[i][1]=fadout(blue[i][0], blue[i][1], 0, btwfadeval);
}
// moon
blue[moonline][1]=fadin(blue[moonline][0], blue[moonline][1], moonintensity, 2);
}
void cloud()
{
while (lastCommand == "CLOUD") {
//do the cloud
}
}
void storm()
{
while (lastCommand == "STORM") {
//do the storm
}
}
int SunLight(String command) {
// Trim extra spaces
command.trim();
// Convert it to upper-case for easier matching
command.toUpperCase();
// Copy command argument to lastCommand
command.toCharArray(lastCommand, 64);
// "Route" the commands to the corresponding function
if(command.equals("SUNRISE")) {
sunrise();
return 1;
}
else if(command.equals("SUNSET")) {
sunset();
return 1;
}
else if(command.equals("MOON")) {
moon();
return 1;
}
else if(command.equals("CLOUD")) {
//cloud();
return 2;
}
else if(command.equals("STORM")) {
//storm();
return 2;
}
else {
return -1;
}
}
If you see errors or the possibility to optimize the code, please telle me .
My fish and corals will thank you !
Maybe you are thinking i’m not English, and you are right ! (sorry for mistakes)