#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;
//------------------------------
//---- led2: Green led -----------
bool led2State=LOW;
unsigned long previousMillisLed2;
const int LED2_T_ON=200;
const int LED2_T_OFF=800;
//------------------------------
//---- led3: Green led -----------
bool led3State=LOW;
unsigned long previousMillisLed3;
const int LED3_T_ON=300;
const int LED3_T_OFF=700;
//------------------------------
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:green led: ON-----------------
display.setCursor(62,30);
display.print("G");
display.drawCircle(64, 16, 10, WHITE);
display.fillCircle(64, 16, 7, WHITE);
//------------------------------------
//----led3:green led: ON-----------------
display.setCursor(105,30);
display.print("B");
display.drawCircle(107, 16, 10, WHITE);
display.fillCircle(107, 16, 7, WHITE);
//------------------------------------
// update display with all of the above graphics
display.display();
}
void loop() {
currentMillis=millis();
Led1Handle();
Led2Handle();
Led3Handle();
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(64, 16, 7, BLACK);
}
}
else
{
if((currentMillis-previousMillisLed2)>=LED2_T_OFF)
{
previousMillisLed2=currentMillis;
led2State=HIGH;
display.fillCircle(64, 16, 7, WHITE);
}
}
}
void Led3Handle()
{
if(led3State==HIGH)
{
if((currentMillis-previousMillisLed3)>=LED3_T_ON)
{
previousMillisLed3=currentMillis;
led3State=LOW;
display.fillCircle(107, 16, 7, BLACK);
}
}
else
{
if((currentMillis-previousMillisLed3)>=LED3_T_OFF)
{
previousMillisLed3=currentMillis;
led3State=HIGH;
display.fillCircle(107, 16, 7, WHITE);
}
}
}