Help w/ 2 sets of LEDs doing differnet things, I'm so close

Hi All, I've been working on this file for a couple of weekends and I think I'm almost there, but I'm stumped right now. I've looked at a ton of tutorials and forums but I haven't found any ideas that lead to a resolution.

This code is for a small project wherein I want to blink 3 LEDs (independently) on 3 pins and pulse 3 separate LEDs on 3 other pins. I have code that blinks 3 leds beautifully, and I have another code file that pulses 3 leds just the way I want, but combining them was not as easy as I thought. In the finished project, I'll be switching between these 2 lighting regimes using a 4-pos rotary switch (off, all on, blink, pulse), so only 3 out of 6 pins on my board will be powered at a time. Let me make a point of saying that my project will only have 3 LEDs; only the lighting pattern will change.

I'm using an ItsyBitsy 32u4 5v from adafruit: Adafruit-ItsyBitsy-32u4-PCB/Itsy Bitsy 32u4 5V Pinout.pdf at master · adafruit/Adafruit-ItsyBitsy-32u4-PCB · GitHub

Here is my code:

//Pulse code,////////////////////
float in, out;
const int LED1 = 5;
const int LED2 = 9;
const int LED3 = 10;

unsigned long Mils = millis();
byte multiplier = 1;

//Blink code////////////////////
const int LED4 = 7;
const int LED5 = 11;
const int LED6 = 13;

const long onDuration4 = 1200UL;// OFF time for LED4
const long offDuration4 = 1200UL;// ON time for LED4
const long onDuration5 = 1300UL;// OFF time for LED5
const long offDuration5 = 1300UL;// ON time for LED5
const long onDuration6 = 1400UL;// OFF time for LED6
const long offDuration6 = 1400UL;// ON time for LED6

int LEDState4 =255;// initial state of LED
int LEDState5 =0;
int LEDState6 =255;

unsigned long rememberTime4=0;// this is used by the code
unsigned long rememberTime5=0;
unsigned long rememberTime6=0;

void setup() {
  
}

void loop() 
{

////blinking LED4/////////////////////
  if( LEDState4 == 255 ) {
    if( (millis()- rememberTime4) >= onDuration4){   
    LEDState4 = 0;// change the state of LED
    rememberTime4=millis();// remember Current millis() time
    }
  }
  else  {   
    if( (millis()- rememberTime4) >= offDuration4){     
    LEDState4 =255;// change the state of LED
    rememberTime4=millis();// remember Current millis() time
    }
  }
  analogWrite(LED4,LEDState4);// turn the LED ON or OFF

//blinking LED5/////////////////////////
  if( LEDState5 == 255 ) {
    if( (millis()- rememberTime5) >= onDuration5){   
    LEDState5 = 0;// change the state of LED
    rememberTime5=millis();// remember Current millis() time
    }
  }
  else  {   
    if( (millis()- rememberTime5) >= offDuration5){     
    LEDState5 =255;// change the state of LED
    rememberTime5=millis();// remember Current millis() time
    }
  }
  analogWrite(LED5,LEDState5);// turn the LED ON or OFF

//blinking LED6//////////////////////////////
  if( LEDState6 == 255 ) {
    if( (millis()- rememberTime6) >= onDuration6){   
    LEDState6 = 0;// change the state of LED
    rememberTime6=millis();// remember Current millis() time
    }
  }
  else  {   
    if( (millis()- rememberTime6) >= offDuration6){     
    LEDState6 =255;// change the state of LED
    rememberTime6=millis();// remember Current millis() time
    }
  }
  analogWrite(LED6,LEDState6);// turn the LED ON or OFF

  //Pulse Code for LED1-3//////////////////////////////////////////////
  
  for (in = 0; in < 6.283; in += (multiplier * 0.0007))
  {
    while ((millis() - Mils) < 1) {};
    Mils += 1;
    out = sin(in) * 127.5 + 127.5;
    analogWrite(LED1,out);
    out = sin(in + 2.094) * 127.5 + 127.5;
    analogWrite(LED2,out);
    out = sin(in + 4.188) * 127.5 + 127.5;
    analogWrite(LED3,out);
  }

  
}

In my proto setup, I'm using the ItsyBitsy with resistors and 3 filament LEDs (Adafruit-ItsyBitsy-32u4-PCB/Itsy Bitsy 32u4 5V Pinout.pdf at master · adafruit/Adafruit-ItsyBitsy-32u4-PCB · GitHub). I am simulating the rotary switch by just having 3 pins connected to the leds and plugging/unplugging in to the correct pins on the board.

What I want to happen is to have the 3 leds blinking when I plug in pins 7, 11, 13 and have the leds pulse when I plug in pins 5, 9, 10. Instead, what I'm getting is that while the leds ARE pulsing when the pulse pins are connected, connecting the blink pins doesn't make the leds behave as the blink code does in a separate file. Instead, the leds turn on and off in accordance with the pulse code (see above). Basically they are pulsing but since they are not pwm-enabled pins, they just turn on and off on with the same timing as the pulse code specifies. Somehow there is a connecting between the blinking and pulsing parts, but I just don't see it.

I've re-distinguished my variables, went from using digitalWrite to analogWrite for the blinkers, rewritten not using timers from various other libraries (Moba tools)... What's missing?

It's bedtime here, so I'll check in tomorrow.

Hello blinkertime

I´ve read your post and I´m depply confused.

How many LEDs and different modes of blinking and flashing do you will have?

Take some time and describe the desired function of the sketch in simple words and this very simple.

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

p.s. coding this project in C++ would be a good approach.

blink 3 less indepently on Arduino at DuckDuckGo

Hi PaulPaulson

OK, so I've got a 4-position switch hooked to 3 leds and an ItsyBitsy.
Position 1 if off.
Position 2 is all on (very easy, no need for microcontroller).
Position 3 is blinking leds, which I can do as a standalone code.
Position 4 is pulsing leds, the same 3 leds.

I can get the leds to blink perfectly, running a sketch that only blinks. I can also get them to pulse perfectly, running a sketch that only pulses.

In order for me to use the switch, I figure I have to have a sketch that blinks the leds when pins 1-3 are powered (position 3) and pulses leds when pins 4-6 are powered (position 4). This means my sketch needs to have all blinking and pulsing instances in one file, right? That's what I've pasted up above.

Does this help?

Thanks mrburnette!

It looks like that flasher class might be useful. There are a million ways to blink and maybe half that to pulse, but I was hoping I wouldn't have to find another way to write this again. :sweat_smile:

Hello blinkertime

Post a handdrawn schematic.

vs

so what should happen in the position 4? pulse the same 3 LEDs like used in position 3?
or other pins ?
or do you nee another position 5?

in any case - you must write your pulsing in a way so it doesn't block. Currently your pulsing blocks and destroys the blinking effect.

If you can use a library, I suggest my LED Toolkit. Check the blink effect and the heartbeat:
https://werner.rothschopf.net/microcontroller/202202_tools_led_en.htm

Yeah, I see where I bungled the phrasing.

Yes, position 4 pulses the same leds. I only have 3 leds in the project. The rotary switch just turns on/off lights and changes how they're lit.

Yep, this was my problem, but I couldn't figure out how to unblock it...

UNTIL last night! I removed the For loop from around the pulse code block and that did the trick. I wish I'd posted here a week ago. :grin:

Thanks for the brain exercise, everybody. This is my 1st post here but the forum has been extremely useful in the past. I'll post the correct code below for posterity.

//This sketch controls the lighting pattern of 3 leds (for my uses), or 6 leds if all pins are 
//connected. Connecting leds to pins 5, 9, 10 slowly pulses them. Connecting to pins 7, 11, 13 
//blinks the leds. See [Sparkfun](https://www.sparkfun.com/tutorials/329) topic to get background
// on pulsing method.

//Pulse code,////////////////////
float in, out;
const int LED1 = 5;
const int LED2 = 9;
const int LED3 = 10;

unsigned long Mils = millis();
byte multiplier = 1;

//Blink code////////////////////
const int LED4 = 7;
const int LED5 = 11;
const int LED6 = 13;

const long onDuration4 = 1200UL;// OFF time for LED4
const long offDuration4 = 1200UL;// ON time for LED4
const long onDuration5 = 1300UL;// OFF time for LED5
const long offDuration5 = 1300UL;// ON time for LED5
const long onDuration6 = 1400UL;// OFF time for LED6
const long offDuration6 = 1400UL;// ON time for LED6

int LEDState4 =255;// initial state of LED. Having different inital states makes blinking seem more random
int LEDState5 =0;
int LEDState6 =255;

unsigned long rememberTime4=0;// this is used by the code
unsigned long rememberTime5=0;
unsigned long rememberTime6=0;

void setup() {
}

void loop() 
{

////blinking LED4/////////////////////
  if( LEDState4 == 255 ) {
    if( (millis()- rememberTime4) >= onDuration4){   
    LEDState4 = 0;// change the state of LED
    rememberTime4=millis();// remember Current millis() time
    }
  }
  else  {   
    if( (millis()- rememberTime4) >= offDuration4){     
    LEDState4 =255;// change the state of LED
    rememberTime4=millis();// remember Current millis() time
    }
  }
  analogWrite(LED4,LEDState4);// turn the LED ON or OFF

//blinking LED5/////////////////////////
  if( LEDState5 == 255 ) {
    if( (millis()- rememberTime5) >= onDuration5){   
    LEDState5 = 0;// change the state of LED
    rememberTime5=millis();// remember Current millis() time
    }
  }
  else  {   
    if( (millis()- rememberTime5) >= offDuration5){     
    LEDState5 =255;// change the state of LED
    rememberTime5=millis();// remember Current millis() time
    }
  }
  analogWrite(LED5,LEDState5);// turn the LED ON or OFF

//blinking LED6//////////////////////////////
  if( LEDState6 == 255 ) {
    if( (millis()- rememberTime6) >= onDuration6){   
    LEDState6 = 0;// change the state of LED
    rememberTime6=millis();// remember Current millis() time
    }
  }
  else  {   
    if( (millis()- rememberTime6) >= offDuration6){     
    LEDState6 =255;// change the state of LED
    rememberTime6=millis();// remember Current millis() time
    }
  }
  analogWrite(LED6,LEDState6);// turn the LED ON or OFF

  //Pulse Code/////// //The (in + #) term in out=sin( blah blah blah determines where 
//on the sin wave the brightness of this leds begins. These 3 leds pulse at the same 
//rate but begin at different brightnesses

  static float in = 4.712;
  float out;
 
  in = in + 0.00025; //change this decimal number up to speed up the pulsing
  if (in > 10.995)
    in = 4.712;
  out = sin(in) * 127.5 + 127.5;
  analogWrite(LED1,out);
  out = sin(in + 2.094) * 127.5 + 127.5; 
  analogWrite(LED2,out);
  out = sin(in + 4.188) * 127.5 + 127.5;
  analogWrite(LED3,out);
  in = in + .0005 * out /255; //this is meant to smooth out the brightness steps, not really sure it has a noticeable effect for my leds

}

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