#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);
#define NUM_PINS 16
const uint8_t pins[NUM_PINS] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, A0, A1, A2, A3}; // Assign digital inputs to pins
void setup() {
Serial.begin(115200); // Start serial communication at 115200 baud rate
// Initialize OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
// SSD1306 allocation failed
for (;;); // Don't proceed, loop forever
}
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.display();
for (uint8_t i = 0; i < NUM_PINS; i++) {
if (pins[i] <= 7) {
DDRD |= (1 << pins[i]); // Set pins 2-7 as output
} else if (pins[i] <= 13) {
DDRB |= (1 << (pins[i] - 8)); // Set pins 8-13 as output
} else {
DDRC |= (1 << (pins[i] - 14)); // Set pins A0-A3 as output
}
}
}
void loop() {
static char inputString[17]; // Buffer for 16 characters plus null terminator
static byte index = 0; // Index for buffer
while (Serial.available() > 0) {
char c = Serial.read();
if (c == '\n') { // Newline character indicates end of input
inputString[index] = '\0'; // Null-terminate the string
if (index == 8) { // Check if the input length is 8 characters
display.clearDisplay();
display.setCursor(0, 0);
display.print(inputString); // Display the string on the OLED
display.display();
} else if (index == 16) { // Check if the input length is 16 characters
for (int i = 0; i < 16; i++) {
if (inputString[i] == '1') {
if (pins[i] <= 7) {
PORTD |= (1 << pins[i]);
} else if (pins[i] <= 13) {
PORTB |= (1 << (pins[i] - 8));
} else {
PORTC |= (1 << (pins[i] - 14));
}
} else {
if (pins[i] <= 7) {
PORTD &= ~(1 << pins[i]);
} else if (pins[i] <= 13) {
PORTB &= ~(1 << (pins[i] - 8));
} else {
PORTC &= ~(1 << (pins[i] - 14));
}
}
}
}
index = 0; // Reset index for next input
} else if (index < 16) {
inputString[index++] = c; // Add character to buffer if within bounds
} else {
index = 0; // Reset index if input length exceeds 16 characters
while (Serial.available() > 0) {
Serial.read(); // Clear the buffer to remove excess characters
}
}
}
}