Dimming led strip problem

Hi, i have small problem dimming led strip while counting millis. If i dim the led strip with delay(), it dims perfectly with no visible flashing. When dimmed while counting millis (i need arduino to check for button states) it flashes really fast and the flashing is visible. Is there anything i could do, to make it behave like when using delay()?

Here's the code for delay: (just the code in loop, other is code from experiment with millis)

// Declare the pins for the buttons and resistors
int leftBlinkerButton = 5;
int rightBlinkerButton = 6;
int brakeLightButton = 7;
int leftBlinkerTransistor = 2;
int rightBlinkerTransistor = 3;
int brakeTransistor = 4;
int blinkerOn = 300;
int blinkerOff = 500;
int brakeOn = 200;
int brakeHighOff = 0;
int brakeOff = 20;
int brakeLowOn = 10;
long leftBlinkerLast = 0;
long rightBlinkerLast = 0;
long brakeLast = 0;
unsigned long currentMillis = millis();
boolean leftBlinkerState;
boolean rightBlinkerState;
boolean brakeState;
	

void setup() {
	Serial.begin(9600);
	pinMode(leftBlinkerButton, INPUT_PULLUP);
	pinMode(rightBlinkerButton, INPUT_PULLUP);
	pinMode(brakeLightButton, INPUT_PULLUP);
	pinMode(leftBlinkerTransistor, OUTPUT);
	pinMode(rightBlinkerTransistor, OUTPUT);
	pinMode(brakeTransistor, OUTPUT);
}

void loop() {
	currentMillis = millis();
  int brzdaValue = digitalRead(brakeLightButton);
  if (brzdaValue == LOW){
    digitalWrite(brakeTransistor, HIGH);
    Serial.println("Brake ON");
    }
  else {
    digitalWrite(brakeTransistor, LOW);
    delay(10);
    digitalWrite(brakeTransistor, HIGH);
    delay(1);
    Serial.println("Brake OFF");
    }
  }

void LeftBlinker(){
	int buttonValueL = digitalRead(leftBlinkerButton);
	if (buttonValueL == LOW){
		if (currentMillis - leftBlinkerLast > (leftBlinkerState ? blinkerOn : blinkerOff)) {
			digitalWrite(leftBlinkerTransistor, leftBlinkerState = !leftBlinkerState);
			leftBlinkerLast = currentMillis;
			Serial.println("Left Blink");
		}
	}	else {
		digitalWrite(leftBlinkerTransistor, LOW);
	}
}

void RightBlinker(){
	int buttonValueR = digitalRead(rightBlinkerButton);
	if (buttonValueR == LOW){
		if (currentMillis - rightBlinkerLast > (rightBlinkerState ? blinkerOn : blinkerOff)) {
			digitalWrite(rightBlinkerTransistor, rightBlinkerState = !rightBlinkerState);
			rightBlinkerLast = currentMillis;
			Serial.println("Right Blink");
		}
	} else {
		digitalWrite(rightBlinkerTransistor, LOW);
	}
}

void Brake(){
	int brzdaValue = digitalRead(brakeLightButton);
	if (brzdaValue == LOW){
		if (currentMillis - brakeLast > (brakeState)) {
			digitalWrite(brakeTransistor, HIGH);
			brakeLast = currentMillis;
			Serial.println("Brake ON");
		}
	} else {
		if (currentMillis - brakeLast > (brakeState ? brakeLowOn : brakeOff)) {
			digitalWrite(brakeTransistor, brakeState = !brakeState);
			brakeLast = currentMillis;
			Serial.println("Brake OFF");
		}
	}
}

heres for millis:

// Declare the pins for the buttons and resistors
int leftBlinkerButton = 5;
int rightBlinkerButton = 6;
int brakeLightButton = 7;
int leftBlinkerTransistor = 2;
int rightBlinkerTransistor = 3;
int brakeTransistor = 4;
int blinkerOn = 300;
int blinkerOff = 500;
int brakeOn = 200;
int brakeHighOff = 0;
int brakeOff = 20;
int brakeLowOn = 10;
long leftBlinkerLast = 0;
long rightBlinkerLast = 0;
long brakeLast = 0;
unsigned long currentMillis = millis();
boolean leftBlinkerState;
boolean rightBlinkerState;
boolean brakeState;
	

void setup() {
	Serial.begin(9600);
	pinMode(leftBlinkerButton, INPUT_PULLUP);
	pinMode(rightBlinkerButton, INPUT_PULLUP);
	pinMode(brakeLightButton, INPUT_PULLUP);
	pinMode(leftBlinkerTransistor, OUTPUT);
	pinMode(rightBlinkerTransistor, OUTPUT);
	pinMode(brakeTransistor, OUTPUT);
}

void loop() {
	currentMillis = micros();
	LeftBlinker();
	RightBlinker();
	Brake();
}

void LeftBlinker(){
	int buttonValueL = digitalRead(leftBlinkerButton);
	if (buttonValueL == LOW){
		if (currentMillis - leftBlinkerLast > (leftBlinkerState ? blinkerOn : blinkerOff)) {
			digitalWrite(leftBlinkerTransistor, leftBlinkerState = !leftBlinkerState);
			leftBlinkerLast = currentMillis;
			Serial.println("Left Blink");
		}
	}	else {
		digitalWrite(leftBlinkerTransistor, LOW);
	}
}

void RightBlinker(){
	int buttonValueR = digitalRead(rightBlinkerButton);
	if (buttonValueR == LOW){
		if (currentMillis - rightBlinkerLast > (rightBlinkerState ? blinkerOn : blinkerOff)) {
			digitalWrite(rightBlinkerTransistor, rightBlinkerState = !rightBlinkerState);
			rightBlinkerLast = currentMillis;
			Serial.println("Right Blink");
		}
	} else {
		digitalWrite(rightBlinkerTransistor, LOW);
	}
}

void Brake(){
	int brzdaValue = digitalRead(brakeLightButton);
	if (brzdaValue == LOW){
		if (currentMillis - brakeLast > (brakeState)) {
			digitalWrite(brakeTransistor, HIGH);
			brakeLast = currentMillis;
			Serial.println("Brake ON");
		}
	} else {
		if (currentMillis - brakeLast > (brakeState ? brakeLowOn : brakeOff)) {
			digitalWrite(brakeTransistor, brakeState = !brakeState);
			brakeLast = currentMillis;
			Serial.println("Brake OFF");
		}
	}
}

Many thanks for help:)

Err....

Sorry, I know it's supposed to be millis, that was just for a test and forgot to change it back

You are doing a LOT of Serial output. I suspect your Serial buffer is filling up and causing delays. You could start by changing the baud rate to something less lethargic, like 115200. If that improves the blinking but doesn't cure it, make the serial output less frequent or make the serial output optional:

#define DEBUGGING 0 // Set to 1 for debugging
.
.
.
#if DEBUGGING
  Serial.println("Left Blink");
#endif

Note: Some of your variable types are wrong. Time values should be "unsigned long". Everything that shouldn't change should be marked "const".

// Declare the pins for the buttons and resistors
const int leftBlinkerTransistor = 2;
const int rightBlinkerTransistor = 3;
const int brakeTransistor = 4;
const int leftBlinkerButton = 5;
const int rightBlinkerButton = 6;
const int brakeLightButton = 7;

const unsigned blinkerOn = 300;
const unsigned blinkerOff = 500;
const unsigned brakeOn = 200;
const int brakeHighOff = 0;
const unsigned brakeOff = 20;
const int brakeLowOn = 10;
unsigned long leftBlinkerLast = 0;
unsigned long rightBlinkerLast = 0;
unsigned long brakeLast = 0;
unsigned long currentMillis;

boolean leftBlinkerState;
boolean rightBlinkerState;
boolean brakeState;

Note: Embedding an assignment inside an expression makes the code harder to read. I would change:
digitalWrite(rightBlinkerTransistor, rightBlinkerState = !rightBlinkerState);
to

      rightBlinkerState = !rightBlinkerState;
      digitalWrite(rightBlinkerTransistor, rightBlinkerState);

Note: There is no need to store a function result in a variable if it is only going to be used once. I would change:

  int buttonValueR = digitalRead(rightBlinkerButton);
  if (buttonValueR == LOW)
  {

to

  if (digitalRead(rightBlinkerButton) == LOW)
  {

disabling serial print fixed it, thanks!
I will learn from and implement the other changes too, many thanks:)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.