// led3-pb2-oled.ino https://wokwi.com/projects/407967620210983937
// Written by Wong SS - 2024-09-03
// oled codes reference: https://wokwi.com/projects/344892392214626898 (i2c addr 0x3C)
// All codes (including libraries) are non-blocking. They do not use delay().
#define OLED_IN 1 // 0 if OLED not used
// Files:
// led3-pb2-oled.ino - setup() and loop() ...
// prj.h - hardware connections
// dly-in-out.h - C++ classes SDELAYMS_T, SINPUT_T and SOUTPUT_T definition
// dly-in-out.cpp - C++ classes implementations
// blink.h and blink.cpp - BLINK_T class defined and implemented
// ===========================================================================================
// What this program does:
// Red LED blinking at 300-300ms on-off
// Yellow LED 100-1900ms on-off - system heartbeat
// Green LED 1000-500ms on-off
// Both Red and Green LEDs blinking can be disable/enabled by PB1 & PB2
// or sending 'r' or 'g' to serial monitor
// This program is non-blocking
#include "prj.h" // defines MQTT_IN, WOKWI and MQTT_ROOT_TOPIC
#include "blink.h"
#include "dly-io.h" // C++ classes: SDELAYMS_T, SINPUT_T, SOUTPUT_T
#if OLED_IN
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
extern Adafruit_SSD1306 display;
void oledInit();
void testOLED();
#endif
// ===========================================================================================
// initally disabled and off
// Reason: Initial system setup can be long and it won't be able to blink
BLINK_T blink_R(ledPinR,ledOnVal,300,300,false,false);
BLINK_T blink_Y(ledPinY,ledOnVal,200,1800,false,false);
BLINK_T blink_G(ledPinG,ledOnVal,1000,500,false,false);
// ===========================================================================================
SINPUT_T pb1(PB1,INPUT,200,LOW); // pin, inputType, bounceDuration, PressedIsLOW
SINPUT_T pb2(PB2,INPUT,200,LOW);
// ------------------------------------
void toggle_enable_R() {
if (blink_R.isEnabled()) {
blink_R.disable();
}
else {
blink_R.enable();
}
}
// ------------------------------------
void toggle_enable_G() {
if (blink_G.isEnabled()) {
blink_G.disable();
}
else {
blink_G.enable();
}
}
// ------------------------------------
void chkPBs() {
if (pb1.justPressed()) {
toggle_enable_R();
Serial.print("\nPB1 enable/disable Red LED blinking");
}
if (pb2.justPressed()) {
toggle_enable_G();
Serial.print("\nPB2 enable/disable Green LED blinking");
}
}
// ------------------------------------
#if OLED_IN
void oledMsg() {
display.clearDisplay(); // Show initial text
display.setTextSize(2); // Draw 2X-scale text
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println(F("Demo\n3LEDs 2PBs"));
display.display(); // Show initial text
}
#endif
// ------------------------------------
void setup() {
Serial.begin(115200);
delay(100);
#if OLED_IN
Wire.begin();
oledInit();
testOLED();
oledMsg();
#endif
Serial.print("\nDemo:"
"\nRed & Green LEDs blink enable/disable controlled by PB1 & PB2, or Serial 'r' or 'g' cmds"
"\nYellow LED always pulse at 0.2s on and 1.8s off as system heartbeat");
Serial.print("\nEnd of setup()\n");
blink_R.enable(true);
blink_Y.enable(true);
blink_G.enable(true);
}
// ------------------------------------
void chkSerial() {
if (Serial.available()) {
switch (Serial.read()) {
case 'r': case 'R':
toggle_enable_R();
Serial.print("\nSerial 'r' or 'R' enable/disable Red LED blinking");
break;
case 'g': case 'G':
toggle_enable_G();
Serial.print("\nSerial 'g' or 'G' enable/disable Green LED blinking");
break;
}
}
}
// ------------------------------------
void loop() {
// ALL codes are non-blocking. No delay() used
blink_R.doBlink();
blink_Y.doBlink();
blink_G.doBlink();
chkPBs();
chkSerial();
}
// ------------------------------------
NP SoE IoT Kit
PB-LED pins
LOW on
Press LOW
RLED
p23
YLED
p19
GLED
p18
PB1 p27
PB2 p25