#include <avr/io.h>
#include <avr/interrupt.h>
const int LED_pin = 12;
const int LED_pin3 = 10;
int eventSwitch;
volatile byte count;
volatile byte count2;
byte reload = 0x9C;
/* analog globabls */
int ledPin = 11; // LED connected to digital pin 9
int analogPin = 3; // potentiometer connected to analog pin 3
int val = 0; // variable to store the read value
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
ISR(TIMER2_COMPA_vect)
{
count++;
count2++;
OCR2A = reload;
}
void setup()
{
Serial.begin(115200);
pinMode(LED_pin, OUTPUT);
pinMode(LED_pin3, OUTPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(LED_pin, LOW);
digitalWrite(LED_pin3, LOW);
cli();
TCCR0B = 0;
OCR2A = reload;
TCCR2A = 1<<WGM21;
TCCR2B = (1<<CS22) | (1<<CS21) | (1<<CS20);
TIMSK2 = (1<<OCIE2A);
sei();
Serial.print("OCR2A: ");
Serial.println(OCR2A, HEX);
Serial.print("TCCR2A: ");
Serial.println(TCCR2A, HEX);
Serial.print("TCCR2B: ");
Serial.println(TCCR2B, HEX);
Serial.print("TIMSK2: ");
Serial.println(TIMSK2, HEX);
Serial.println("TIMER2 Setup Finished.");
}
void loop()
{
static int i;
int numOfLeds = 60;
static int events =0;
static int PWM_State=0;
static int multiplier = 1;
timer(multiplier);
switch(events){
case 0 :
multiplier = 10;
if(i>numOfLeds)i=0;
if (count2 == (multiplier*10 )){
i++;
flash();
if(eventSwitch){
events = eventSwitch;
break;
}
}
break;
case 1 :
multiplier = 1;
if(i>numOfLeds)i=0;
if (count2 == (multiplier*10 )){
i++;
flash();
if(eventSwitch){
events = eventSwitch;
break;
}
}
break;
case 2 :
multiplier = 1;
if(i>numOfLeds)i=0;
if (count2 == (multiplier*10 )){
i++;
flash();
if(eventSwitch){
events = eventSwitch;
break;
}
}
break;
case 3 :
multiplier = 1;
if(i>numOfLeds)i=0;
if (count2 == (multiplier*10 )){
i++;
flash();
if(eventSwitch){
events = eventSwitch;
break;
}
}
break;
case 4 :
multiplier = 1;
if(i>numOfLeds)i=0;
if (count2 == (multiplier*10 )){
i++;
flash();
if(eventSwitch){
//Serial.println("break0");
events = eventSwitch;
break;
}
}
break;
case 5 :
multiplier = 1;
if(i>numOfLeds)i=0;
if (count2 == (multiplier*10 )){
i++;
flash();
if(eventSwitch){
//Serial.println("break0");
events = eventSwitch;
break;
}
}
break;
case 6 :
multiplier = 1;
if(i>numOfLeds)i=0;
if (count2 == (multiplier*10 )){
i++;
flash();
if(eventSwitch){
//Serial.println("break0");
events = eventSwitch;
break;
}
}
break;
case 7 :
multiplier = 1;
if(i>numOfLeds)i=0;
if (count2 == (multiplier*10 )){
i++;
flash();
if(eventSwitch){
//Serial.println("break0");
events = eventSwitch;
break;
}
}
break;
case 8 :
multiplier = 1;
if(i>numOfLeds)i=0;
if (count2 == (multiplier*10 )){
i++;
flash();
if(eventSwitch){
//Serial.println("break0");
events = eventSwitch;
break;
}
}
break;
case 9 :
multiplier = 1;
if(i>numOfLeds)i=0;
if (count2 == (multiplier*10 )){
i++;
flash();
if(eventSwitch){
//Serial.println("break0");
events = eventSwitch;
break;
}
}
break;
case 10 :
multiplier = 1;
if(i>numOfLeds)i=0;
if (count2 == (multiplier*10 )){
i++;
flash();
if(eventSwitch){
//Serial.println("break0");
events = eventSwitch;
break;
}
}
break;
} //switchcase
}
void timer(int multiplier){
static int sequenceState = 0;
static int sequenceState2 = 0;
static int sequenceState3 = 0;
static int sequenceState4 = 0;
static int millisecond = 0;
const int timerValue=10;
static int seconds=0;
static int tenseconds=0;
switch(sequenceState4)
{
case 0: //2nd timer.
if (count2 == (timerValue*30 )){
sequenceState4 = 1;
}
break;
case 1:
if (count2 == ((timerValue+1)*30)){
sequenceState4 = 0;
count2 = 0;
}
break;
}
switch(sequenceState)
{
case 0: //flashing every 10 milliseconds
if (count == (timerValue*10 )){
sequenceState = 1;
seconds++;
}
break;
case 1:
if (count == ((timerValue+1)*10)){
sequenceState = 0;
count = 0;
}
break;
}
switch(sequenceState3)
{
case 0:
if(tenseconds > 9)tenseconds = 0;
if (seconds == 10){
sequenceState3 = 1;
tenseconds++;
flash3();
eventSwitch++;
if(eventSwitch>10)eventSwitch=0;
// Serial.println(eventSwitch);
}
break;
case 1:
sequenceState3 = 0;
seconds = 0;
flash3();
break;
}
}
void flash()
{
static boolean output = HIGH;
digitalWrite(LED_pin, output);
output = !output;
}
void flash3()
{
static boolean output = HIGH;
digitalWrite(LED_pin3, output);
output = !output;
}