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.