Making 3 LEDs fade in/out using sine wave function

Hi all,

I am completely new to Arduino but have spent the last few weeks working through the book and familiarising myself with how it works.

For part of my project, I need to make 3 motors vibrate one after another, and then back again (i.e. A, B, C, B, A etc.). I would like the first one to start vibrating, then the second one to start vibrating before the first one has faded out, then the third one to start vibrating before the second one has faded out (by this point the first one will have faded out) and so on. I am currently fading in/out LEDs to replicate this scenario. I am using Arduino Uno.

Unfortunately, I cannot quite get the timing right and this means the middle LED (B) does not seem to fully fade in and out (and when we used the motors with this code, it meant that the middle motor wasn’t vibrating very much). I have been randomly editing the numbers in an attempt to solve my problem.

I would very much appreciate some help on finding out why my code isn’t working - I might be missing an obvious solution! Please see my code below. Thank you very much in advance! :slight_smile:

// Declaration of variables and matrices

int NumMotors = 3;   
int incomingByte[3];  //for incoming serial data
int Motors[3] = {
  0, 1, 2 };  // Motor numbers that correspond to ProcessingGUI

// Declaration of pin numbers

// Motors
int motorPinA = 9; // the first motor connected to pin 9
int motorPinB = 6; // the first motor connected to pin 6
int motorPinC = 3; // the first motor connected to pin 3

int motorList[3] = {
  motorPinA, motorPinB, motorPinC};

int i = 0;

void setup() {

  Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps

  // Set all motorPins as output

  for (int i=0; i<NumMotors; i++)
     pinMode(motorList[i], OUTPUT);

void loop() {
   float tstart = millis()/1000.0;
   mean[motor] = 127.5;
    //Activate the motors with a sine wave at specified frequency and amplitude. 

   float t = millis()/1000.0;

   analogWrite(motorPinB,127.5 + 127.5 * sin(0.3*2.0*PI*t));
   analogWrite(motorPinC,127.5 + 127.5 * sin(0.3*2.0*PI*t-1.57));
   analogWrite(motorPinB,127.5 + 127.5 * sin(0.3*2.0*PI*t-3.14));
   analogWrite(motorPinA,127.5 + 127.5 * sin(0.3*2.0*PI*t-4.71));


You have a 50mS delay and are using as a time referance millis / 1000.0 ! Does that strike you as right?

Also that code will fade on a sine wave and will not do what your words say you want to do.

So you mean I should keep everything in ms?

Ooh ok, what does that mean exactly? My LEDs seem to be lighting up one after another, except for the middle one which is essentially blinking instead of fading. I assumed that it just did not have enough time to fade all the way up and down before it was 'told' by the code to fade again.

Any reason why you write pin B twice?

So that it loops forwards and then backwards: A, B, C, B, A, B, C, B, A, B, C...etc I presumed that that would be the easiest way of achieving this (not sure if there is a code to run it backwards too!)?

By subtracting pi, you invert the value of sin(....). So the one moment you write e.g. 127.5 + 121 (at t=1) followed 100ms later by 127.5 - 121.

No wonder you see a flicker on B.

Note: 127.5 + 121 exceeds the PWM range (0 to 255).

I am currently fading in/out LEDs to replicate this scenario.

Can you please explain how fast you are trying to fade the leds up/down and the delay or phasing between them?

Ooh ok, what does that mean exactly?

It means the concept of that code is wrong. You generate basically four quadrature signals but you assign the motor B to two quadrature signals 1800 apart. So what else can it do but flash?
As @sterretje has told you.