Multiple float in, out functions at once

I've not tested your code, but I'm pretty surprised if it works, since "sin()" takes radians as argument. A full cyclus of radians is 0..PI as far as I remember. Where did you find the values 4.712 and 10.99557?

So you should start to get your math right before you try to arrange all the LED's on a time line :slight_smile:

@septillion: He didn't get a working cut'n paste sketch the first time, so he tries again. Apparently.. :confused:

Danois90:
I've not tested your code, but I'm pretty surprised if it works, since "sin()" takes radians as argument. A full cyclus of radians is 0..PI as far as I remember. Where did you find the values 4.712 and 10.99557?

Full period of a sinusoid runs from 0 to 2*PI.
4.712 ~ 1.5 * PI
10.99557 ~ 3.5 * PI

I guess OP is attempting to set the phase.

@gfvalvo: Nice! I just remember something about 3D programming, sine-tables and that 0..PI will cover all values since the second half is just a "negative mirror" of the first half. Optimize, optimize, optimize :wink:

But, OP should not use "sin()" at all for the task and since he does anyway, it makes it sound a lot like home work :wink:

Danois90:
I've not tested your code, but I'm pretty surprised if it works, since "sin()" takes radians as argument. A full cyclus of radians is 0..PI as far as I remember. Where did you find the values 4.712 and 10.99557?

So you should start to get your math right before you try to arrange all the LED's on a time line :slight_smile:

@septillion: He didn't get a working cut'n paste sketch the first time, so he tries again. Apparently.. :confused:

The math is right as stated by gfvalvo, I've never used an Arduino before this project and after doing some tutorials I found an article explaining the float function along with the appropriate periods of a Sine wave.

And also, I'm not looking for a cut/paste because I want/need to be able to understand the code.

septillion:
What was wrong with what I told you here?

For three:

#include <FadeLed.h>

//91 step sine table 0 = 0 degree, 90 = 90 degree
const flvar_t SineTable[91] PROGMEM = {
  0,  4,  9,  13,  18,  22,  27,  31,  35,  40,
44,  49,  53,  57,  62,  66,  70,  75,  79,  83,
87,  91,  96, 100, 104, 108, 112, 116, 120, 124,
127, 131, 135, 139, 143, 146, 150, 153, 157, 160,
164, 167, 171, 174, 177, 180, 183, 186, 190, 192,
195, 198, 201, 204, 206, 209, 211, 214, 216, 219,
221, 223, 225, 227, 229, 231, 233, 235, 236, 238,
240, 241, 243, 244, 245, 246, 247, 248, 249, 250,
251, 252, 253, 253, 254, 254, 254, 255, 255, 255,
255};

//Make FadeLed object with a sine lookup table of 91 (0 to 90 including)
FadeLed sines[3] = {{9, SineTable, 90}, {10, SineTable, 90}, {11, SineTable, 90}};
const byte NrSines = sizeof(sines)/sizeof(sines[0]);

void setup(){
  for(byte i = 0; i < NrSines; i++){
    sines[i].setTime(1000); //halve period, 0% to 90% or 90% to 0%
  }
 
  //start the first
  sines[0].on();
}

void loop(){
  FadeLed::update();
 
  //if first is >50% (45 degree) and second is not fading and off
  if((sines[0].get() >= 45) && !sines[1].done() && (sines[1].get() == 0)){
    sines[1].on();
  }
 
  //if first is >50% (45 degree) and second is not fading and off
  if((sines[1].get() >= 45) && !sines[2].done() && (sines[2].get() == 0)){
    sines[2].on();
  }
 
  //if sin is 100% (90 degree), fade back down
  if(sines[0].done() && sines[0].get()){
    sines[0].off();
  }
 
  //if sin is 100% (90 degree), fade back down
  if(sines[1].done() && sines[1].get()){
    sines[1].off();
  }
 
  //if sin is 100% (90 degree), fade back down
  if(sines[2].done() && sines[2].get()){
    sines[2].off();
  }
}




Or with a handy loop:


#include <FadeLed.h>

//91 step sine table 0 = 0 degree, 90 = 90 degree
const flvar_t SineTable[91] PROGMEM = {
  0,  4,  9,  13,  18,  22,  27,  31,  35,  40,
44,  49,  53,  57,  62,  66,  70,  75,  79,  83,
87,  91,  96, 100, 104, 108, 112, 116, 120, 124,
127, 131, 135, 139, 143, 146, 150, 153, 157, 160,
164, 167, 171, 174, 177, 180, 183, 186, 190, 192,
195, 198, 201, 204, 206, 209, 211, 214, 216, 219,
221, 223, 225, 227, 229, 231, 233, 235, 236, 238,
240, 241, 243, 244, 245, 246, 247, 248, 249, 250,
251, 252, 253, 253, 254, 254, 254, 255, 255, 255,
255};

//Make FadeLed object with a sine lookup table of 91 (0 to 90 including)
FadeLed sines[3] = {{9, SineTable, 90}, {10, SineTable, 90}, {11, SineTable, 90}};
const byte NrSines = sizeof(sines)/sizeof(sines[0]);

void setup(){
  for(byte i = 0; i < NrSines; i++){
    sines[i].setTime(1000); //halve period, 0% to 90% or 90% to 0%
  }
 
  //start the first
  sines[0].on();
}

void loop(){
  FadeLed::update();
 
  for(byte i = 0; i < NrSines; i++){
    //for all but last (who has no next)
    if(i < (NrSines - 1)){
      //if >50% (45 degree) and next is not fading and is off
      if((sines[i].get() >= 45) && !sines[i + 1].done() && (sines[i + 1].get() == 0)){
        sines[i + 1].on();
      }
    }
   
    //if sin is 100% (90 degree), fade back down
    if(sines[i].done() && sines[i].get()){
      sines[i].off();
    }
  }
}




Although I do apologize for not updating the release :/ But the nightly should be fine :)

Sorry Septillion, but I found that a lot of what I needed wasn't documented in your library. Ill try again with the code you have given me though. Thanks.

It is in the code, only the nightly has not had a run of Doxygen yet to make the HTML doc to go with it :slight_smile:

I'll update it tonight or tomorrow and probably add a example sketch as well. Probably I'll use you're sine table :smiley:

But in the basics it's not that hard. You pre-calculate the values of a sine for an 8-bit (max = 255) output and put them into an array (in PROGMEM). I did it for 0 to and including 90 to correspond with 0 to 90 degree. You can do this in Excel, Python or even by hand. Formula is just:
y = 255 x sin(x)
with y rounded to an integer value and the degree version of sin(). In radians it's just:
y = 255 x sin(x / 180 * pi)

I gave an Python example before, here an Excel example. But how you make the table does not matter. All it matters is that the library now uses that to drive the output :slight_smile: You can now input degrees and get the right sine level on the PWM. So if you do:
sines[0].set(45) it will fade to the level of 45 degrees aka 180/255.

Rest of the functions are documented pretty good I would say :slight_smile:

septillion:
It is in the code, only the nightly has not had a run of Doxygen yet to make the HTML doc to go with it :slight_smile:

I'll update it tonight or tomorrow and probably add a example sketch as well. Probably I'll use you're sine table :smiley:

But in the basics it's not that hard. You pre-calculate the values of a sine for an 8-bit (max = 255) output and put them into an array (in PROGMEM). I did it for 0 to and including 90 to correspond with 0 to 90 degree. You can do this in Excel, Python or even by hand. Formula is just:
y = 255 x sin(x)
with y rounded to an integer value and the degree version of sin(). In radians it's just:
y = 255 x sin(x / 180 * pi)

I gave an Python example before, here an Excel example. But how you make the table does not matter. All it matters is that the library now uses that to drive the output :slight_smile: You can now input degrees and get the right sine level on the PWM. So if you do:
sines[0].set(45) it will fade to the level of 45 degrees aka 180/255.

Rest of the functions are documented pretty good I would say :slight_smile:

Thank you for the detailed response. I have been messing around with it but in your example LED 1 phases in and out and then LED 2 does nothing. It does not fade in when LED2 is at 50% (45 degrees)

Which code did you try? First or second?

Could be there's an error. I wrote it dry.

But the first led (pin 9) fades in and out? I only start the fade once in the code for the first led with

  //start the first
  sines[0].on();

Are you sure you uploaded the new code?

septillion:
Which code did you try? First or second?

Could be there's an error. I wrote it dry.

But the first led (pin 9) fades in and out? I only start the fade once in the code for the first led with

  //start the first

sines[0].on();



Are you sure you uploaded the new code?

I changed the pins from 9,10,11 to 3,5,9 and its the first code I'm trying.
First pin fades in and out but the others don't do anything.

Threads merged.

Sorry if my previous post was offensive, lots of lazy ppl use the forum to get their (home) work done. Q: Do you HAVE to use a sinus wave to get the result you want? There are other methods which are much simpler and better..

If you going to use solenoids, do note the frequency of pin 5 and 6 is different from the rest and not easy changeable.

But I will look into it tomorrow. I will need to dig up an Arduino ::slight_smile: Still unpacking boxes after moving...

Can you try the second piece as well?

septillion:
If you going to use solenoids, do note the frequency of pin 5 and 6 is different from the rest and not easy changeable.

But I will look into it tomorrow. I will need to dig up an Arduino ::slight_smile: Still unpacking boxes after moving...

Can you try the second piece as well?

So this is pretty much the extent of what happens with both pieces of code.

The exact thing happens with the second code. One LED phases in then out. No loops and no signs of any other LEDs.

I also moved the pins to 9,10,11 like your code has, but no avail.

But if I see correctly, it only fades once? (And only a second time with an Arduino reset?)

septillion:
But if I see correctly, it only fades once? (And only a second time with an Arduino reset?)

Correct

Can you run one with a bit of debug Serial?

#include <FadeLed.h>

//91 step sine table 0 = 0 degree, 90 = 90 degree
const flvar_t SineTable[91] PROGMEM = {
  0,   4,   9,  13,  18,  22,  27,  31,  35,  40,
 44,  49,  53,  57,  62,  66,  70,  75,  79,  83,
 87,  91,  96, 100, 104, 108, 112, 116, 120, 124,
127, 131, 135, 139, 143, 146, 150, 153, 157, 160,
164, 167, 171, 174, 177, 180, 183, 186, 190, 192,
195, 198, 201, 204, 206, 209, 211, 214, 216, 219,
221, 223, 225, 227, 229, 231, 233, 235, 236, 238,
240, 241, 243, 244, 245, 246, 247, 248, 249, 250,
251, 252, 253, 253, 254, 254, 254, 255, 255, 255,
255};

//Make FadeLed object with a sine lookup table of 91 (0 to 90 including)
FadeLed sines[3] = {{3, SineTable, 90}, {5, SineTable, 90}, {9, SineTable, 90}};
const byte NrSines = sizeof(sines)/sizeof(sines[0]);

void setup(){
  Serial.begin(115200);
  
  for(byte i = 0; i < NrSines; i++){
    sines[i].setTime(1000); //halve period, 0% to 90% or 90% to 0%
  }
 
  //start the first
  sines[0].on();
  
  Serial.println(NrSines);
  Serial.println();
  
  for(byte i =0; i < NrSines; i++){
    Serial.println(sines[i].get())
    Serial.println(sines[i].getCurrent());
    Serial.println(sines[i].done());
    Serial.println();
  }
}

void loop(){
  FadeLed::update();
 
  for(byte i = 0; i < NrSines; i++){
    //for all but last (who has no next)
    if(i < (NrSines - 1)){
      //if >50% (45 degree) and next is not fading and is off
      if((sines[i].get() >= 45) && !sines[i + 1].done() && (sines[i + 1].get() == 0)){
        sines[i + 1].on();
      }
    }
   
    //if sin is 100% (90 degree), fade back down
    if(sines[i].done() && sines[i].get()){
      sines[i].off();
    }
  }
}

And post the serial output (in text please) here.

septillion:
Can you run one with a bit of debug Serial?

#include <FadeLed.h>

//91 step sine table 0 = 0 degree, 90 = 90 degree
const flvar_t SineTable[91] PROGMEM = {
  0,  4,  9,  13,  18,  22,  27,  31,  35,  40,
44,  49,  53,  57,  62,  66,  70,  75,  79,  83,
87,  91,  96, 100, 104, 108, 112, 116, 120, 124,
127, 131, 135, 139, 143, 146, 150, 153, 157, 160,
164, 167, 171, 174, 177, 180, 183, 186, 190, 192,
195, 198, 201, 204, 206, 209, 211, 214, 216, 219,
221, 223, 225, 227, 229, 231, 233, 235, 236, 238,
240, 241, 243, 244, 245, 246, 247, 248, 249, 250,
251, 252, 253, 253, 254, 254, 254, 255, 255, 255,
255};

//Make FadeLed object with a sine lookup table of 91 (0 to 90 including)
FadeLed sines[3] = {{3, SineTable, 90}, {5, SineTable, 90}, {9, SineTable, 90}};
const byte NrSines = sizeof(sines)/sizeof(sines[0]);

void setup(){
  Serial.begin(115200);
 
  for(byte i = 0; i < NrSines; i++){
    sines[i].setTime(1000); //halve period, 0% to 90% or 90% to 0%
  }

//start the first
  sines[0].on();
 
  Serial.println(NrSines);
  Serial.println();
 
  for(byte i =0; i < NrSines; i++){
    Serial.println(sines[i].get())
    Serial.println(sines[i].getCurrent());
    Serial.println(sines[i].done());
    Serial.println();
  }
}

void loop(){
  FadeLed::update();

for(byte i = 0; i < NrSines; i++){
    //for all but last (who has no next)
    if(i < (NrSines - 1)){
      //if >50% (45 degree) and next is not fading and is off
      if((sines[i].get() >= 45) && !sines[i + 1].done() && (sines[i + 1].get() == 0)){
        sines[i + 1].on();
      }
    }
 
    //if sin is 100% (90 degree), fade back down
    if(sines[i].done() && sines[i].get()){
      sines[i].off();
    }
  }
}




And post the serial output (in text please) here.
3

90
0
0

0
0
1

0
0
1

That looks correct. I did notice an error but for all the world, that would just turn all three on... Can you try:

#include <FadeLed.h>

//91 step sine table 0 = 0 degree, 90 = 90 degree
const flvar_t SineTable[91] PROGMEM = {
  0,   4,   9,  13,  18,  22,  27,  31,  35,  40,
 44,  49,  53,  57,  62,  66,  70,  75,  79,  83,
 87,  91,  96, 100, 104, 108, 112, 116, 120, 124,
127, 131, 135, 139, 143, 146, 150, 153, 157, 160,
164, 167, 171, 174, 177, 180, 183, 186, 190, 192,
195, 198, 201, 204, 206, 209, 211, 214, 216, 219,
221, 223, 225, 227, 229, 231, 233, 235, 236, 238,
240, 241, 243, 244, 245, 246, 247, 248, 249, 250,
251, 252, 253, 253, 254, 254, 254, 255, 255, 255,
255};

//Make FadeLed object with a sine lookup table of 91 (0 to 90 including)
FadeLed sines[3] = {{3, SineTable, 90}, {5, SineTable, 90}, {9, SineTable, 90}};
const byte NrSines = sizeof(sines)/sizeof(sines[0]);

void setup(){
  Serial.begin(115200);
  
  for(byte i = 0; i < NrSines; i++){
    sines[i].setTime(1000); //halve period, 0% to 90% or 90% to 0%
  }
  
  for(byte i = 0; i < NrSines; i++){
    sines[i].beginOn();
    delay(1000);
    sines[i].begin(0);
  }
 
  //start the first
  sines[0].on();
  
  Serial.println(NrSines);
  Serial.println();
  
  for(byte i =0; i < NrSines; i++){
    Serial.println(sines[i].get());
    Serial.println(sines[i].getCurrent());
    Serial.println(sines[i].done());
    Serial.println();
  }
}

void loop(){
  FadeLed::update();
 
  for(byte i = 0; i < NrSines; i++){
    //for all but last (who has no next)
    if(i < (NrSines - 1)){
      //if >50% (45 degree) and next is not fading and is off
      if((sines[i].get() >= 45) && !sines[i + 1].done() && (sines[i + 1].get() == 0)){
        sines[i + 1].on();
      }
    }
   
    //if sin is 100% (90 degree), fade back down
    if(sines[i].done() && sines[i].get()){
      sines[i].off();
    }
  }
}

That should turn on one by one the leds at first. Do all three leds light up in order?

septillion:
That looks correct. I did notice an error but for all the world, that would just turn all three on... Can you try:

#include <FadeLed.h>

//91 step sine table 0 = 0 degree, 90 = 90 degree
const flvar_t SineTable[91] PROGMEM = {
  0,  4,  9,  13,  18,  22,  27,  31,  35,  40,
44,  49,  53,  57,  62,  66,  70,  75,  79,  83,
87,  91,  96, 100, 104, 108, 112, 116, 120, 124,
127, 131, 135, 139, 143, 146, 150, 153, 157, 160,
164, 167, 171, 174, 177, 180, 183, 186, 190, 192,
195, 198, 201, 204, 206, 209, 211, 214, 216, 219,
221, 223, 225, 227, 229, 231, 233, 235, 236, 238,
240, 241, 243, 244, 245, 246, 247, 248, 249, 250,
251, 252, 253, 253, 254, 254, 254, 255, 255, 255,
255};

//Make FadeLed object with a sine lookup table of 91 (0 to 90 including)
FadeLed sines[3] = {{3, SineTable, 90}, {5, SineTable, 90}, {9, SineTable, 90}};
const byte NrSines = sizeof(sines)/sizeof(sines[0]);

void setup(){
  Serial.begin(115200);
 
  for(byte i = 0; i < NrSines; i++){
    sines[i].setTime(1000); //halve period, 0% to 90% or 90% to 0%
  }
 
  for(byte i = 0; i < NrSines; i++){
    sines[i].beginOn();
    delay(1000);
    sines[i].begin(0);
  }

//start the first
  sines[0].on();
 
  Serial.println(NrSines);
  Serial.println();
 
  for(byte i =0; i < NrSines; i++){
    Serial.println(sines[i].get());
    Serial.println(sines[i].getCurrent());
    Serial.println(sines[i].done());
    Serial.println();
  }
}

void loop(){
  FadeLed::update();

for(byte i = 0; i < NrSines; i++){
    //for all but last (who has no next)
    if(i < (NrSines - 1)){
      //if >50% (45 degree) and next is not fading and is off
      if((sines[i].get() >= 45) && !sines[i + 1].done() && (sines[i + 1].get() == 0)){
        sines[i + 1].on();
      }
    }
 
    //if sin is 100% (90 degree), fade back down
    if(sines[i].done() && sines[i].get()){
      sines[i].off();
    }
  }
}




That should turn on one by one the leds at first. Do all three leds light up in order?

This causes LED1 to blink, then LED2 to blink, then LED3. One by one.

DOHHHHHHH

My comment was right: "and next is not fading"
but you don't do that with !sines[i + 1].done() :confused: Spot the sneaky error? maybe I should have said "and next is done fading" 8)

Try: (left the debug in but I'll guess you know how to remove that :smiley:

#include <FadeLed.h>

//91 step sine table 0 = 0 degree, 90 = 90 degree
const flvar_t SineTable[91] PROGMEM = {
  0,   4,   9,  13,  18,  22,  27,  31,  35,  40,
 44,  49,  53,  57,  62,  66,  70,  75,  79,  83,
 87,  91,  96, 100, 104, 108, 112, 116, 120, 124,
127, 131, 135, 139, 143, 146, 150, 153, 157, 160,
164, 167, 171, 174, 177, 180, 183, 186, 190, 192,
195, 198, 201, 204, 206, 209, 211, 214, 216, 219,
221, 223, 225, 227, 229, 231, 233, 235, 236, 238,
240, 241, 243, 244, 245, 246, 247, 248, 249, 250,
251, 252, 253, 253, 254, 254, 254, 255, 255, 255,
255};

//Make FadeLed object with a sine lookup table of 91 (0 to 90 including)
FadeLed sines[3] = {{3, SineTable, 90}, {5, SineTable, 90}, {9, SineTable, 90}};
const byte NrSines = sizeof(sines)/sizeof(sines[0]);

void setup(){
  Serial.begin(115200);
  
  for(byte i = 0; i < NrSines; i++){
    sines[i].setTime(1000); //halve period, 0% to 90% or 90% to 0%
  }
  
  for(byte i = 0; i < NrSines; i++){
    sines[i].beginOn();
    delay(1000);
    sines[i].begin(0);
  }
 
  //start the first
  sines[0].on();
  
  Serial.println(NrSines);
  Serial.println();
  
  for(byte i =0; i < NrSines; i++){
    Serial.println(sines[i].get());
    Serial.println(sines[i].getCurrent());
    Serial.println(sines[i].done());
    Serial.println();
  }
}

void loop(){
  FadeLed::update();
 
  for(byte i = 0; i < NrSines; i++){
    //for all but last (who has no next)
    if(i < (NrSines - 1)){
      //if >50% (45 degree) and next is not fading and is off
      if((sines[i].getCurrent() >= 45) && sines[i + 1].done() && (sines[i + 1].get() == 0)){
        sines[i + 1].on();
      }
    }
   
    //if sin is 100% (90 degree), fade back down
    if(sines[i].done() && sines[i].get()){
      sines[i].off();
    }
  }
}

septillion:
DOHHHHHHH

My comment was right: "and next is not fading"
but you don't do that with !sines[i + 1].done() :confused: Spot the sneaky error? maybe I should have said "and next is done fading" 8)

Try: (left the debug in but I'll guess you know how to remove that :smiley:

#include <FadeLed.h>

//91 step sine table 0 = 0 degree, 90 = 90 degree
const flvar_t SineTable[91] PROGMEM = {
  0,  4,  9,  13,  18,  22,  27,  31,  35,  40,
44,  49,  53,  57,  62,  66,  70,  75,  79,  83,
87,  91,  96, 100, 104, 108, 112, 116, 120, 124,
127, 131, 135, 139, 143, 146, 150, 153, 157, 160,
164, 167, 171, 174, 177, 180, 183, 186, 190, 192,
195, 198, 201, 204, 206, 209, 211, 214, 216, 219,
221, 223, 225, 227, 229, 231, 233, 235, 236, 238,
240, 241, 243, 244, 245, 246, 247, 248, 249, 250,
251, 252, 253, 253, 254, 254, 254, 255, 255, 255,
255};

//Make FadeLed object with a sine lookup table of 91 (0 to 90 including)
FadeLed sines[3] = {{3, SineTable, 90}, {5, SineTable, 90}, {9, SineTable, 90}};
const byte NrSines = sizeof(sines)/sizeof(sines[0]);

void setup(){
  Serial.begin(115200);
 
  for(byte i = 0; i < NrSines; i++){
    sines[i].setTime(1000); //halve period, 0% to 90% or 90% to 0%
  }
 
  for(byte i = 0; i < NrSines; i++){
    sines[i].beginOn();
    delay(1000);
    sines[i].begin(0);
  }

//start the first
  sines[0].on();
 
  Serial.println(NrSines);
  Serial.println();
 
  for(byte i =0; i < NrSines; i++){
    Serial.println(sines[i].get());
    Serial.println(sines[i].getCurrent());
    Serial.println(sines[i].done());
    Serial.println();
  }
}

void loop(){
  FadeLed::update();

for(byte i = 0; i < NrSines; i++){
    //for all but last (who has no next)
    if(i < (NrSines - 1)){
      //if >50% (45 degree) and next is not fading and is off
      if((sines[i].getCurrent() >= 45) && sines[i + 1].done() && (sines[i + 1].get() == 0)){
        sines[i + 1].on();
      }
    }
 
    //if sin is 100% (90 degree), fade back down
    if(sines[i].done() && sines[i].get()){
      sines[i].off();
    }
  }
}

This does the same as the last code. They blink one by one. Not as a sine wave but just a blink.