#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.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            -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

unsigned long currentMillis;

//---- led1: red led -----------
bool led1State=LOW;
unsigned long previousMillisLed1;
const int LED1_T_ON=500;
const int LED1_T_OFF=500;
//------------------------------


void setup() {
  Serial.begin(9600);
  // initialize and clear display
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }

  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  //----led1:red led: ON-----------------
  display.setCursor(18,30);
  display.print("R");
  display.drawCircle(21, 16, 10, WHITE);  
  display.fillCircle(21, 16, 7, WHITE);   
  //------------------------------------
  // update display with all of the above graphics
  display.display();
}

void loop() {
  currentMillis=millis();

  Led1Handle();
  // reflejar los cambios efectuados en Led1Handle()
  display.display();
  
  delay(50);
}

void Led1Handle()
{
  if(led1State==HIGH)
  {
    if((currentMillis-previousMillisLed1)>=LED1_T_ON)
    {
      previousMillisLed1=currentMillis;
      led1State=LOW;
      display.fillCircle(21, 16, 7, BLACK);
    }
  }
  else
  {
    if((currentMillis-previousMillisLed1)>=LED1_T_OFF)
    {
      previousMillisLed1=currentMillis;
      led1State=HIGH;
      display.fillCircle(21, 16, 7, WHITE);
    }
  }
}