Hello,
Firstly I am only new to Arduino so please be kind...
I am trying a simple project of hooking up 8x red LEDs to a shift register and having being able to control each of their intensities in an animation type function.
I am hoping this will pave the way to my understanding of how to incorporate different levels of BAM into my future projects (led interactive table, rgb cube etc etc).
I understand the concept of BAM as well as the shift register. My issue is, I cannot for the life of me understand how to shift out the correct bytes to display even a fading effect on ANY of the LEDs.
I am after that light bulb moment where something clicks in my brain lol. Will someone please provide me of such information??????
I will attach my code (don't laugh as I am very tired and it probably doesn't even make much sense!!)
Any help is much appreciated,
Dave
int latchPin = 8; // Latch pin connected to pin 12 RCLK on the 74HC595
int clockPin = 12; // Clock pin connected to pin 11 SRCLK on the 74HC595
int dataPin = 11; // Data pin connected to pin 14 SER on the 74HC595
byte BAM_Counter = 0; //Counter used in BAM_Handler function
byte brightness; //4-Bit desired brightness for leds
byte shift_out; //Byte shifted out to shift registers
byte red[8];
byte which_led = 4;
unsigned long current_time;
unsigned long last_updated_time;
/*------------------------------------------------------------*/
void setup() {
pinMode(latchPin,OUTPUT);
pinMode(clockPin,OUTPUT);
pinMode(dataPin,OUTPUT);
}
/*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*/
void loop() {
current_time = millis();
last_updated_time = 0;
if(brightness > B1111) { brightness == B0001; }
if((current_time - last_updated_time) > 49)
{
last_updated_time = current_time;
brightness++;
}
BAM_Handler();
}
/*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*/
void BAM_Handler() {
if(BAM_Counter > B1111) { BAM_Counter = B0001; } //Once counter reaches 16, it resets back to 1
if(BAM_Counter == B0001) { Update_leds(0); } //4-Bit binary code modulation handler.
else if(BAM_Counter == B0010) { Update_leds(1); }
else if(BAM_Counter == B0100) { Update_leds(2); }
else if(BAM_Counter == B1000) { Update_leds(3); }
BAM_Counter++;
}
/*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*/
void Update_leds(byte bitPosition) {
red[8] = bitWrite(red[8], which_led, bitRead(brightness, bitPosition));
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, red[which_led]);
digitalWrite(latchPin, HIGH);
}