#include <TinyDebug.h>
#include <avr/io.h>
#include <avr/delay.h>
int binaryNumber[8]; // Empty array to store 8-bit (0 - 255) binary number
void Eight_Bit_Binary_Array(int num);
void SetColour (int colour, int intensity);
#define RED_LED PB0
#define GREEN_LED PB1
#define BLUE_LED PB4
const int Red = 0;
const int Green = 1;
const int Blue = 2;
volatile uint8_t* Port[] = {&OCR0A, &OCR0B, &OCR1B};
void setup() {
Debug.begin();
pinMode(RED_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
pinMode(BLUE_LED, OUTPUT);
// - - - Configure counter/timer0 for fast PWM on PB0 and PB1 - - -
// Set OC0A/OC0B on Compare Match, clear OC0A/OC0B at BOTTOM (inverting mode)
// Compare Output Mode, Fast PWM Mode
TCCR0A = 1 << COM0A1 | 1 << COM0A0 | 1 << COM0B1 | 1 << COM0B0 | 1 << WGM01 | 1 << WGM00;
Debug.print("TCCR0A "); Eight_Bit_Binary_Array(TCCR0A);
TCCR0B = 1 << CS01 | 1 << CS00; // 64 prescaler
Debug.print("TCCR0B "); Eight_Bit_Binary_Array(TCCR0B);
// - - - Configure counter/timer1 for fast PWM on PB4 - - -
GTCCR = 1 << PWM1B | 1 << COM1B1 | 1 << COM1B0; // 01110000
Debug.print("GTCCR "); Eight_Bit_Binary_Array(GTCCR);
TCCR1 = 1 << COM1A1 | 1 << COM1A0 | 1 << CS12 | 1 << CS11 | 1 << CS10; // 00110111
Debug.print("TCCR1 "); Eight_Bit_Binary_Array(TCCR1);
}
void loop() {
for (int colour = 0; colour < 3; colour++) {
SetColour((colour + 2) % 3, 0);
Debug.print("colour "); Debug.println(colour);
for (int i = 0; i <= 255; i++) {
SetColour((colour + 1) % 3, i);
Debug.print("colour+: "); Debug.print((colour + 1) % 3); Debug.print(" i:"); Debug.println(i);
SetColour(colour, 255 - i);
Debug.print("colour: "); Debug.print(colour); Debug.print(" -i:"); Debug.println(i);
delay(25);
}
}
}
// - - - - - Functions - - - - -
void Eight_Bit_Binary_Array(int num) {
// Convert decimal to binary and store the bits in an array
for (int i = 7; i >= 0; i--) {
binaryNumber[i] = num & 1; // Extract the least significant bit
num >>= 1; // Right-shift the decimal number to get the next bit
}
Debug.print("binary = ");
for (int i = 0; i < 8; i++) {
Debug.print(binaryNumber[i]); // Print the binary number only of the array
}
Debug.print("\n");
}
// Sets colour Red = 0 Green = 1 Blue = 2 to specified intensity 0 (off) to 255 (max)
void SetColour (int colour, int intensity) {
*Port[colour] = 255 - intensity;
}