//  Attiny85 code for Timer alarm 
//  https://americantech1.blogspot.com/2022/01/attiny85-code-for-timer-alarm.html
//  https://www.youtube.com/watch?v=QhGhzi8NowE&t=89s

#include <TinyWireM.h>
#include <Tiny4kOLED.h>
#include "font16x32digits.h"

// Define screen size 
uint8_t width = 128;
uint8_t height = 32;

int minutes=2;
int seconds=0;
int fase=0;
int chosen=0;
int times[]={2,5,10,15,20,25,30,40};
int ani=14;

int debounce4=0;
int debounce1=0;
//---------------------------------------------------------------------
void setup() {
	pinMode(4,INPUT_PULLUP); // select time button
	pinMode(1,INPUT_PULLUP); // Execute the time 
	pinMode(3,OUTPUT);
	oled.begin(width, height, sizeof(tiny4koled_init_128x32br), tiny4koled_init_128x32br);

	oled.clear();
	oled.on();

	// Switch the half of RAM that we are writing to, to be the half that is non currently displayed
	oled.switchRenderFrame();

	oled.setFont(FONT6X8P);
	oled.setCursor(5, 3);
	oled.print("American");
	delay(3000);
	oled.clear();
}
//---------------------------------------------------------------------
void loop() {
	if(fase==0 || fase==3){
		if(digitalRead(4)==0){
			if(debounce4==0){
				debounce4=1;
				chosen++;
				if(chosen>7) chosen=0;
				if(fase==3) fase=0;
			}
		} else {debounce4=0;}
		if(digitalRead(1)==0){
			if(debounce1==0) {
				debounce1=1;
				minutes=times[chosen];
				fase=1;
				if(fase==3) fase=0;
			}
		} else {debounce1=0;}
	}

	if(fase==1 || fase==2){
		delay(1000);
		seconds--;
		if(seconds<=0){
			seconds=59;
			minutes--;
		}
	}
	if(fase==3){
		digitalWrite(3,1);
		delay(500);
		digitalWrite(3,0);
		delay(500);
	}

	if(minutes==0) fase=2;
	if(minutes==-1) fase=3;

	updateDisplay();
}
//---------------------------------------------------------------------
void updateDisplay() {
  oled.clear();
  if(fase==0)
     {
      oled.setFont(FONT6X8P);
      oled.setCursor(0, 0);
      oled.print(" SET TIME:");
      oled.setCursor(0, 1);
      oled.print("----------------------");
      oled.setCursor(0, 2);
      for(int i=0;i<8;i++) {
		oled.print(" ");
		oled.print(times[i]);
	}
	if(chosen == 2) oled.setCursor(32,3 );
	else if(chosen == 3) oled.setCursor(50,3);
	else if(chosen>3) oled.setCursor(chosen*17, 3);
	else
		oled.setCursor(6+chosen*12, 3);
		oled.print("^");
	}

	if(fase==1){  // print the remaining time 
		oled.setFont(FONT6X8P);
		oled.setCursor(8, 0);
		oled.print("TIME:");
		oled.setFont(FONT8X16P);
		oled.setCursor(8, 1);
		if(minutes<10)
			oled.print(0);
			oled.print(minutes);
			oled.print(" : ");
		if(seconds<10)
			oled.print(0);
			oled.print(seconds);
			oled.setFont(FONT6X8P);
			oled.setCursor(8, 3);
		for(int i=ani;i>0;i--) {
			oled.print(".");
		}
		ani--;
		if(ani==0)
		ani=14;
	}

	if(fase==2) {  /// to preint the big number seconds 
		oled.setFont(FONT16X32DIGITS);
		oled.setCursor(8, 0);
		oled.print(seconds);
	}
	oled.switchFrame();
} 
ATTINY8520PU