Fade 50% -100% with millis problems

Hello,
I'm trying to use millis() to create a fade (blinking works fine).
In this project, I'm going to use some of my taillights to turn on the signal via Bluetooth.
The rear lights work normally on 50 diodes power.
Right now all my turn signals works from 0 to 255 so its fine for my front turn signal lights, but on rear i want to setup them to work from 50 to 255.
What should i change / fix?

#include <SoftwareSerial.h>

int turnSignal = 5;
int stateLed = 50;
unsigned long currentTime = 0;
unsigned long savedTime1 = 0;
boolean turnSignalState = false;

SoftwareSerial Bluetooth(0, 1);
char Data;
void sendData(String transmitData){
Bluetooth.println(transmitData);}

void setup(){
  Bluetooth.begin(9600);
  pinMode(turnSignal,OUTPUT);
}

void loop(){

  currentTime = millis();
  
  if (turnSignalState && currentTime - savedTime1 >= 300UL) {
    savedTime1 = currentTime;
    digitalWrite(turnSignal , !digitalRead(turnSignal ));
  }

  if(Bluetooth.available()){
    Data=Bluetooth.read();

    if(Data==('g')){ // ON
      turnSignalState = true;
    }
    if(Data==('h')){ // OFF
      turnSignalState = false;
      analogWrite(turnSignal,stateLed);
    }
  }
}

Welcome to the forum

Not the answer to the question that you asked, but using pins 0 and 1 for SoftwareSerial on many Arduinos is not a good idea as they are used by the hardware Serial interface for the Serial monitor and to upload code

Which Arduino board do you have ?

There is a function called analogWrite()
that does this.

best regards Stefan

OP uses it already

but stateLed is set at 50 and never changes

Ooops Yes indeed.

So then the OP has to describe in normal words but in detail what she/he wants the LEDs to look like

One single speculation
@arduraz
do you want the LEDs to start at "half brightness = your 50%" and then increase to 100% over a certain time instead of immidiately switching to full brightness = your 100%?

if this is what you want in which timeintervall shall the transition from 50% to 100% be finished?

yes I'm confused too on what the end result needs to be as s/he mentions front and rear lights but there is only one turn signal pin

do you see any declaration of port adresses used?

Note: 50 is not 50%. 50 is 50/256ths brightness. If you want 50% brightness, use 128.

void loop() {

  currentTime = millis();

  if (turnSignalState && currentTime - savedTime1 >= 300UL) {
    savedTime1 = currentTime;

    // CHANGED from digitalWrite()
    if (stateLed == 50)
      stateLed = 255;
    else
      stateLed = 50;
    analogWrite(turnSignal, stateLed);
  }

  if (Bluetooth.available()) {
    Data = Bluetooth.read();

    if (Data == ('g')) {  // ON
      turnSignalState = true;
    }
    if (Data == ('h')) {  // OFF
      turnSignalState = false;
      stateLed = 50;  // ADDED
      analogWrite(turnSignal, stateLed);
    }
  }
}

Hello arduraz

Check, test, modify and add this "dimmed flasher" sketch to your project.

constexpr byte LedPin {9};
constexpr byte ButtonPin {A0};

void setup() 
{
  pinMode (LedPin,OUTPUT);
  pinMode (ButtonPin,INPUT_PULLUP);
}
void loop()
{
  static unsigned long stamp=millis();

  if (millis()-stamp >= 2 and !digitalRead(ButtonPin)) 
  {
    stamp=millis(); 
    static bool upDown=false;
    static int dimValue=50;
    upDown?dimValue--:dimValue++;
    if (dimValue==50 or dimValue==255) upDown=!upDown;
    analogWrite(LedPin,dimValue);
  }
}

Have a nice day and enjoy coding in C++.

or something like this

const byte turnSignalPin = 5;
bool turnSignalIsOn = false;
bool ledIsOn = false;
unsigned long lastBlinkTime;
int currentFadeValue;

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

void loop() {
  switch (Serial.read()) {
    case 'g':
      digitalWrite(turnSignalPin, HIGH);
      turnSignalIsOn = true;
      lastBlinkTime = millis();
      ledIsOn = true;
      currentFadeValue = 255;
      break;
    case 'h':
      digitalWrite(turnSignalPin, LOW);
      turnSignalIsOn = false;
      ledIsOn = false;

      break;
    default: break;
  }

  if (turnSignalIsOn)
    if (millis() - lastBlinkTime >= 300) {
      if (ledIsOn) {
        digitalWrite(turnSignalPin, LOW);
        currentFadeValue -= 10;
        if (currentFadeValue <= 50)  turnSignalIsOn = false;
      } else {
        Serial.println(currentFadeValue);
        analogWrite(turnSignalPin, currentFadeValue);
      }
      ledIsOn = ! ledIsOn;
      lastBlinkTime = millis();
    }
}

The eye won't perceive a linear change in brightness.
Perhaps just use a lower value here, i.e. int stateLed = 30;

Here, we test 0, 6, 12, 25, 50 and 100% brightness (linear) ...
https://wokwi.com/projects/351396862761108056

Hello m8,

I used an Arduino Nano. My project is much bigger than this code, I have no other pins to connect my HC-05 :frowning:

Have you used all of the A* pins ?

Then don't use SoftwareSerial, just use Serial

Thank you for help, its work!
But after all i have some new problems...
In this project i have front and rear turn signal diodes, front one are on digital pins (they ar blinked from 0 to 100% and its ok) and rear are fade from 50 to 100%.
Now the problem are:
a) If I off left or right turn signal diodes, is 50% chance to frontdiodes stay ON,
b) hazzard lights are not synhronized (special if I used turn signal before), + theay state ar 100% when i stop it.
How to fix these problems?

Fixed Code:

#include <SoftwareSerial.h>
int turnSignalRearL = 5; //PWM 2x diode turn signal rear-left
int turnSignalRearR = 6; //PWM 2x diode turn signal rear-right
int turnSignalFrontL = 7; //2x diode turn signal front-left
int turnSignalFrontR = 8; //2x diode turn signal front-right

const int constStateLed = 50;
int stateLed1 = 50;
int stateLed2 = 50;

//BLINK / FADE
unsigned long currentTime1 = 0;
unsigned long savedTime1 = 0;
unsigned long savedTime2 = 0;
boolean turnSigLeft = false;
boolean turnSigRight = false;

SoftwareSerial Bluetooth(0, 1);
char Data;
void sendData(String transmitData){
Bluetooth.println(transmitData);}

void setup(){
  Bluetooth.begin(9600);    
  pinMode(turnSignalRearL,OUTPUT);
  pinMode(turnSignalRearR,OUTPUT);    
  pinMode(turnSignalFrontL,OUTPUT);
  pinMode(turnSignalFrontR,OUTPUT);
}

void loop(){
  currentTime1 = millis();
  
  //=== LEFT turn signal ===
  if (turnSigLeft && currentTime1 - savedTime1 >= 300UL) {
    savedTime1 = currentTime1;
    digitalWrite(turnSignalFrontL, !digitalRead(turnSignalFrontL));
    if (stateLed1 == 50)
      stateLed1 = 255;
    else
      stateLed1 = 50;
    analogWrite(turnSignalRearL, stateLed1);
  }

  //=== RIGHT turn signal ===
  if (turnSigRight && currentTime1 - savedTime2 >= 300UL) {
    savedTime2 = currentTime1;
    digitalWrite(turnSignalFrontR, !digitalRead(turnSignalFrontR));
    if (stateLed2 == 50)
      stateLed2 = 255;
    else
      stateLed2 = 50;
    analogWrite(turnSignalRearR, stateLed2);
  }

  //=== BLINKING / FADING ===  
  if(Bluetooth.available()){
    Data=Bluetooth.read();
    
    //Left ON
    if(Data==('g')){
      turnSigRight = false;
      analogWrite(turnSignalRearR,constStateLed);
      turnSigLeft = true;
      sendData("turn signal left ON");
    }
    //Left OFF
    if(Data==('h')){
      turnSigLeft = false;
      analogWrite(turnSignalRearL,constStateLed);
      digitalWrite(turnSignalFrontL,0);;
      sendData("turn signal left OFF");
    }

    //Right ON
    if(Data==('i')){
      turnSigLeft = false;
      analogWrite(turnSignalRearL,constStateLed);
      turnSigRight = true;
      sendData("turn signal right ON");
    }
    //Right OFF
    if(Data==('j')){
      turnSigRight = false;
      analogWrite(turnSignalRearR,constStateLed);
      digitalWrite(turnSignalFrontR,0);
      sendData("turn signal right OFF");
    }
    
    //Hazzard lights OM
    if(Data==('9')){      
      turnSigLeft = true;
      turnSigRight = true;
      sendData("HAZARD lights ON");
    }
    //Hazzard lights OFF
    if(Data==('0')){
      turnSigLeft = false;
      turnSigRight = false;
      digitalWrite(turnSignalFrontL,0);
      digitalWrite(turnSignalFrontR,0);
      analogWrite(turnSignalRearL,constStateLed);
      analogWrite(turnSignalRearR,constStateLed);
      sendData("HAZARD lights OFF");
    }
  }
}

Just 3 left and my project are not done yet...

a state machine approach to this would probably help you get a better architectured code

The first thing to fix the problems is to describe in more words what is happening.

And to post a schematic (a schematic is a NON-fritzing hand-drawn plan how things are wired together)

and then post your complete sketch. How shall anybody make a suggestion that works if
you don't provide your full code and a complete schematic?

best regards Stefan

The first problem was solved by me.
The second problem is not hardware (schematic etc.), but software(desynchronised).
I modified my code (Fixed Code) to easy to understand.
The problem is the synchronization of the flashing of the direction indicators when using the hazard lights, and when I turn off one of the direction indicators while the LEDs are on.
The front turn signals and the rear turn signals are desynchronised.
I cant do schematic for you right now, but I`m 100% sure its ok.
I'll do it schematic tomorrow if u want.

this still applies