Hi all,
Recently got into the arduino, most problems up until now I managed to figure out by reading through the wealth of info here, alas I have hit a wall.

My project is trying to breathe new life into a slot machine topper (little statue on top). The statue has eyes which consist of a pair of green, red and blue LEDs.

The goal is to have the ‘eyes’ fade on and off, changing colour between each fade.
Unfortunately, the ‘eyes’ have a common positive and depending on which of the 3 grounds is active, determines the colour.

I will aim to have a total of 4 LEDs. 1 constantly fading in and out, then the other three turning on and off in succession each time the first LED does 1 fade cycle.

So, say 4 LEDs, 1 yellow, 1 red, 1 blue and 1 green.
LED yellow will constantly fade on and off. As it fades, only one of the other three LEDs will be on at a time.

LED yellow starts at Low LED yellow fades to High LED yellow fades to Low LED Yellow fades to high

etc
LED Red is at high LED Red is at high LED Red is at high LED Red is Low
LED Blue is at low LED Blue is at low LED Blue is at low LED Blue is at high
LED Green is at low LED Green is at low LED Green is at low LED Green is at low

So far I have the fade code set (an example from on here) and I am working on just the one colour. The aim of the below code is to have green LED fade on and off, be off for the period of a fade, then back to fade (estimated the time to fade in and out is 3000 millisec)

``````/*

This example shows how to fade an LED on pin 9
using the analogWrite() function.

This example code is in the public domain.
*/

int led = 9;           // the pin that the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by
unsigned long timeGreenChanged = millis();
unsigned long period = 1000;
boolean greenOn = true;
const byte greenPin = 2;

// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);
pinMode(greenPin, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
// set the brightness of pin 9:
analogWrite(led, brightness);

// change the brightness for next time through the loop:

// reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness == 255) {
}
// wait for 30 milliseconds to see the dimming effect
delay(30);

if (millis() - timeGreenChanged >= period)
{
greenOn = !greenOn;
timeGreenChanged = millis();
digitalWrite(greenPin, greenOn);
if (greenOn)
{
period = 3000;
}
else
{
period = 3000;
}
}
}
``````

The issue is, it is turning on while the fade is at its brightest, not when its at its least.

it goes;
Brightest > fade off > fade on until brightest > off for 3000mill > etc.

Any help would be greatly appreciated, especially confirming the period of a full fade is 3000millisec and how to add in code for the ‘blink’ for the transistors for the other 2 colours.

Cheers,

There is no need to use a transistor at all. You should simply subtract your fade value from 255 before sending it to analogWrite.

Why do you not always use analogWrite for all your LEDs?

Adding a transistor inverts the signal so you must invert the commands, that applies to the digital stuff as well.

I see what you mean about the transistors, I'll remove them.

Ok so instead I will aim to have a total of 4 LEDs. 1 constantly fading in and out, then the other three turning on and off in succession each time the first LED does 1 fade cycle.

So, say 4 LEDs, 1 yellow, 1 red, 1 blue and 1 green.
LED yellow will constantly fade on and off. As it fades, only one of the other three LEDs will be on at a time.

LED yellow starts at Low LED yellow fades to High LED yellow fades to Low LED Yellow fades to high

etc
LED Red is at high LED Red is at high LED Red is at high LED Red is Low
LED Blue is at low LED Blue is at low LED Blue is at low LED Blue is at high
LED Green is at low LED Green is at low LED Green is at low LED Green is at low

``````#define fadeSpeed 5
#define offDelay 3000

const uint8_t fadeLed = 9;            // the pin that the fading LED is attached to (needs to be a PWM pin)
const uint8_t rgbPins[] = {2,3,4};    // Pins the R, G & B
uint8_t ledCounter = 0;

void setup() {
for (uint8_t x = 0; x < sizeof(rgbPins); x++){
pinMode(rgbPins[x], OUTPUT);
}
}

void loop() {
digitalWrite(rgbPins[ledCounter],HIGH);
for (uint16_t x = 0; x < 256; x++){