Sine Waves

Hi, I am trying to programme an series of LEDS on and off without using the delay() function, i have already created projects that do this using sine waves and the mills() function however i now need a very specific sine wave creating and I’m unsure of how to do it.
For my previous project i have used:

masterint = 127.00;
delaytime = (millis() / 5000);
value1 = masterint + masterint * sin( delaytime * 2.0 * PI  );

This creates a continuous sine wave but keeps all the number positive (imagine the mcdonalds logo continuously).

what i now require is a sine wave that last for one increase and one decrease. Like the one shown in the attached picture (imagine an n).

This will allow me to fade a value up and down and then stop.

Any help would be greatly appreciated :slight_smile:
Thanks

sinewave.tiff (39.3 KB)

masterint = 127.00;

Why on earth would you use int in the name of a variable that appears to hold a float?

Masterint ..........Master intensity

I was shortening it, the line above is

float masterint;

If you only want to do something once, do it in setup().

I need to do it once but it is to be triggered from a sensor so needs to be in the loop

I need to do it once but it is to be triggered from a sensor so needs to be in the loop

boolean beenThereDoneThat = false;

void loop()
{
   if(!beenThereDoneThat)
   {
      if(checkSensor())
      {
         doSineWaveThing();
         beenThereDoneThat = true;
      }
   }
}

I understand how to use the triggers etc i just need help with the sine wave.

Feel free to post your code.

Just make sure you go only 2*PI radians and then stop.

ok so far i have this, which just produces a normal sine wave from 0 - 255 both in the positive and negative, keithRB could you expand on what you've said, do you have any pointers on how i would code this?

//------------------------
//    Sine Wave Values
//------------------------
int value1;
float delaytime1;
//-------------------------
//     Time Offsets
//-------------------------
long wait1 = 0;      

void setup(){
  
  Serial.begin(9600);
}

void loop(){
//---------------------------  
//   Define Time Variables
//---------------------------

 delaytime1 = millis()/5000.0; 

   if (millis() >= wait1) {

      value1 = 255 * sin ( delaytime1 * PI);
 }

//---------------------------
//    Serial Print Data
//---------------------------
delay (10);

Serial.print ("v1 = ");
Serial.println ( value1); 


}

What in that code waits for a sensor to return some value?

What are the input values to the sin function? What set of inputs generates one hump?

Stop calling sin() when you get to the end of that set of values.

Thanks to KeithB i have an answer

//------------------------
//    Sine Wave Values
//------------------------
int value1;
float delaytime1;
float pie;
//-------------------------
//     Time Offsets
//-------------------------
long wait1 = 0;      

void setup(){
  
  Serial.begin(9600);
}

void loop(){
//---------------------------  
//   Define Time Variables
//---------------------------

 delaytime1 = millis()/5000.0; 
 
 pie = 2 * PI;

   if (millis() >= wait1) {

     
      value1 = 255 * sin ( delaytime1);
 }

//---------------------------
//    Serial Print Data
//---------------------------
if (delaytime1 < (pie / 2)) {
delay (10);

Serial.print ("v1 = ");
Serial.println ( value1); 

}
}

PaulS i think you were missing the point, this sketch is a little out take of a much bigger project, all i needed was to be able to create a sine wave which went from 0-255-0. Although technically i’ve not achieved this i am able to print the values and tell it when to stop printing the value which gives me what i need.

joshbailz:

      value1 = 255 * sin ( delaytime1);

sin(x) is going to have a value between -1.0 and 1.0. So 255*sin(x) is going to be between -255.0 and +255.0.

sin(x) * 127.5 + 127.5 will give you 0..255.