/**************************************************************************
Trafic light without instruction in the loop

  This program was written in the simplest possible way in order to prevent 
  a not excellent knowledge of programming language.  It is possible use it
  HOW IT IS only on the wokwi simulator
    
  For no reason the use of this code make the authors responsible and 
  even less obliges them to provide support and / or any explanation or 
  clarification
  However, if possible, we will try to answer to any questions, 
  without any obligation and / or commitment and / or responsibility
  by  https://www.facebook.com/groups/883620498953478 TEAM
   

      The user of this software will have total, complete 
      and exclusive responsibility for anythings.
      Anyone can modify it and adapt it to own needs,always reporting the 
      present declaration, even after important changes.

  These phrases will always reported in complete form, without this declaration
  there will be a copyright violation                        
  
  Thank you

 **************************************************************************/
//#include <SPI.h> // SPI alternative to i2c
#include <Wire.h>  // i2c speed !!!
#include <Adafruit_GFX.h> // font
#include <Adafruit_SSD1306.h>
#include <stdio.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)

#define led1 	11 
#define led2 	 8 
#define led3   4

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int  start_h=15, start_m=04, clk=53;
char outstr[30];
volatile int state;
 
void clock(void)
{ // clock simulation as process independent from the traffic light 
  // delete old values
  display.setCursor(80, 0);
  display.setTextColor(SSD1306_BLACK);
  sprintf(outstr,"%02d:%02d:%02d",start_h,start_m,clk);
  display.print(outstr);
  
  // TIC and 595923 management
  if(clk==59) 
  { if(start_m==59)
    { if(start_h==23) start_h=0;
      else start_h++;
      start_m=-1;
    }
    start_m++;
    clk=-1;  
  }
  
  clk++;
    
  // new values
  display.setCursor(80, 0);
  display.setTextColor(SSD1306_WHITE);
  sprintf(outstr,"%02d:%02d:%02d",start_h,start_m,clk);
  display.print(outstr);
  display.display();
}  

void setup() {
  //Serial.begin(9600);
  //Wire.begin();
  //Wire.setClock(400000L); // i2c speed

  // Display setup
  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3D)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }
  display.clearDisplay();              // no logo 
  
  pinMode(led1,OUTPUT);
  pinMode(led2,OUTPUT);
  pinMode(led3,OUTPUT);
    
  cli(); // Nointerrupt
  TCCR1A = 0;
  TCCR1B = 0;
  TCCR1B |= (1 << CS11); //(1 << CS11)|(1 << CS10);(1 << CS12)|
  TCCR1B |= (1 << WGM10); 
  //TCCR1B |= (1 << WGM12); //CTC : with CTC led1 don't work
  OCR1A = 37000;
  OCR1B = 26000; 
  TIMSK1 |= (1 << OCIE1B);
  TIMSK1 |= (1 << OCIE1A);
  //TCNT1=0; 
  TIMSK1 |= (1 << TOIE1);
    
  sei(); // ok interrupt

  // START
  digitalWrite(led1, HIGH);
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  delay(300);
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  display.setTextSize(2);              // 2:1 pixel scale
  display.setTextColor(SSD1306_WHITE); // Draw white text
  display.setCursor(36, 22);           // top-left corner
  display.cp437(true);                 // Use full 256 char 'Code Page 437' font
  display.write("START");
  display.display();
  delay(600);
  display.clearDisplay();

  // READY
  display.setTextSize(1);      // Normal 1:1 pixel scale
  display.setCursor(2, 0);     // top-left corner
  display.write("Trafic light");
  display.display();
  digitalWrite(led1,HIGH);     
}

ISR(TIMER1_COMPB_vect) {
    state =2;
    digitalWrite(led1,HIGH); 
    digitalWrite(led2,HIGH); 
    digitalWrite(led3,LOW); 
}

ISR(TIMER1_COMPA_vect) {
    state =3;
    digitalWrite(led1,LOW); 
    digitalWrite(led2,LOW);
    digitalWrite(led3,HIGH);   
}

ISR(TIMER1_OVF_vect) {
    state =1;
    digitalWrite(led1,HIGH); 
    digitalWrite(led2,LOW);
    digitalWrite(led3,LOW);
       
    /* with TCNT1 > 0  timer1 will start from this value e not zero*/
    //TCNT1=15000; 
  }

void loop() {
  // Trafic light plus a simulation of a clock 
  // that work independently from the traffic light 
  // as a second process
  /*
  static long now,last=0;
    
  now=millis(); 
  if( (now-last) > 1000L) 
  { clock(); 
    last=now; 
  }
  */ 
}