I used this tutorial here and decided it to overcomplicate the pattern. For further learning. My code in theory should start the LEDs at a low brightness and fade them in using this code. My expected output is as follows.
All 8 LEDs start out as off.
Fade in slowly.
LEDs turn of one by one from left to right.
LEDs turn on one by one right to left.
Fade out.
Repeat.
This is the loop that fades the LEDs in.
for (byte b = 1; b < 255; b++) {
setBrightnessUp(b); //sets the brightness up function
delay(20);
}
This is the function:
void setBrightnessUp(byte brightness) {// 0 to 255
analogWrite(outputEnablePin, brightness);
}
However at this time it appears to be getting into my loop but not increasing the brightness at all. When the code first runs, the LEDs are on but at a minimum brightness. The rest of the code including the fade out at the end works fine.
Fade out functions:
for (byte b = 255; b > 0; b--) {
setBrightness(b); //sets the brightness function
delay(20);
}
void setBrightness(byte brightness) { // 0 to 255
analogWrite(outputEnablePin, 255 - brightness);
}
Here is the complete code. Excuse the over commenting it helps me learn:
//sets pin out for control pins on shift register
int latchPin = 5;
int clockPin = 6;
int dataPin = 4;
int outputEnablePin = 3; //enables a PWM pin output
//uses byte (8bit binary variable)
byte leds = 0;
void setup() {
// sets the pins to output mode
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode (outputEnablePin, OUTPUT);
}
void loop() {
//attempted to make led fade in from 0
leds = 0; // set led to no output ready for dimming effect
updateShiftRegister(); //calls the function used for output
delay(1000); //troubleshooting delay
for (byte b = 1; b < 255; b++) {
setBrightnessUp(b); //calls the brightnessup function
delay(20);
}
leds = 255; //Sets the byte variable to 255 or 11111111
updateShiftRegister(); //calls the function used for output
delay(500); //waits 1/2 second
//sets a variable i at 8 and runs while i is more than 1
//minus 1 each loop
for (int i = 8; i > -1 ; i--) {
//Bitclear acts on a byte like its an array
//it sets the bit at position i to 0 (or off)
// the left most bit is 7 the right most 0
bitClear(leds, i);
updateShiftRegister(); //calls the display function
delay(250); // waits
}
//sets a veriable i at 0 and runs while is less than 8
//plus 1 each loop
for (int i = 0; i < 8 ; i++) {
//same as bitclear but turns on the bit at position i
bitSet(leds, i);
updateShiftRegister(); //calls the display function
delay(250); //waits
}
//After the 2 other for loops are finished this will then fade
//the LEDs out
//sets the bite to 255 (11111111), while its more that 0
//and minus 1 each loop
for (byte b = 255; b > 0; b--) {
setBrightness(b); //calls the brightness function
delay(20);
}
}
// this function outputs the data to the shifter
void updateShiftRegister() {
digitalWrite(latchPin, LOW);//sets the latch pin to low(off)
shiftOut(dataPin, clockPin, LSBFIRST, leds);
// the first 2 functions set the data and clock pins needed
//to receive data. The LSBFIRST tells it to start at the right
//most bit (MSBFIRST is oposite). LEDs is our binary array (byte)
// EG 0000000 would print no lights. It shifts this into the
//shiftregister at 1 bit at a time from one end or the other
//(determined by LSBFIRST or MSBFIRST)
digitalWrite(latchPin, HIGH); // sets the latch pin to high
//the low to high state change triggers the output
}
//brightness down slowly
void setBrightness(byte brightness) { // 0 to 255
analogWrite(outputEnablePin, 255 - brightness);
}
//supposed to fade the brightness up
void setBrightnessUp(byte brightness) { // 0 to 255
analogWrite(outputEnablePin, brightness);
}
Board layout
-
1I don't see a connection to pin 3...?Majenko– Majenko2017年07月14日 18:29:14 +00:00Commented Jul 14, 2017 at 18:29
-
Yea its not in the diagram cause its part of the additional work exercise but is in my board.PInoob– PInoob2017年07月14日 18:37:35 +00:00Commented Jul 14, 2017 at 18:37
1 Answer 1
I was mistakenly thinking that setting the brightness variable to 255 would mean that the LED is full on. This is wrong, 255 is actually off.
So with a little change in my code.
for (byte b = 255; b > 0; b--) {
setBrightnessUp(b); //calls the brightnessup function
delay(30);
}
The loop now works. I am still not sure why 255 is off and not on though.
-
1Because the output enable on the shift register is active low. A low turns it on and a high turns it off. Newbies tend to want high to always turn something on and low to be off, but in the real world it is more often the other way round.Delta_G– Delta_G2018年01月11日 00:39:58 +00:00Commented Jan 11, 2018 at 0:39