#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;
bool led2State = LOW;
unsigned long previousMillisLed1;
unsigned long previousMillisLed2;
const int LED1_T_ON=500;
const int LED1_T_OFF=500;
const int LED2_T_ON=200;
const int LED2_T_OFF=800;
//------------------------------
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);
//----led2:gren led: ON-----------------
display.setCursor(70,30);
display.print("G");
display.drawCircle(73, 16, 10, WHITE);
display.fillCircle(73, 16, 7, WHITE);
//------------------------------------
// update display with all of the above graphics
display.display();
}
void loop() {
currentMillis=millis();
Led1Handle();
Led2Handle();
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);
}
}
}
void Led2Handle()
{
if(led2State==HIGH)
{
if((currentMillis-previousMillisLed2)>=LED2_T_ON)
{
previousMillisLed2=currentMillis;
led2State=LOW;
display.fillCircle(73, 16, 7, BLACK);
}
}
else
{
if((currentMillis-previousMillisLed2)>=LED2_T_OFF)
{
previousMillisLed2=currentMillis;
led2State=HIGH;
display.fillCircle(73, 16, 7, WHITE);
}
}
}