This circuit is designed to control a set of DC motors using an Arduino Uno R3 microcontroller, interfaced with an L298N DC motor driver. The system can be controlled wirelessly via an HC-05 Bluetooth module. The circuit also includes two red LEDs for indication purposes and a 0.96" OLED display for visual output. The entire circuit is powered by a 9V battery.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// Configuration for OLED
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Variables for running text
String text1 = "SMKN 26 Jakarta";
String text2 = "XII TTL 3";
int textPosition = SCREEN_WIDTH; // Start from the right of the screen
unsigned long previousMillis = 0;
const long interval = 30; // Speed of running text (smaller is faster)
bool showText1 = true;
char t;
void setup() {
// Initialize motor and LED pins
pinMode(13, OUTPUT); // Left motor forward
pinMode(12, OUTPUT); // Left motor reverse
pinMode(11, OUTPUT); // Right motor forward
pinMode(10, OUTPUT); // Right motor reverse
pinMode(9, OUTPUT); // LED
Serial.begin(9600); // Start serial communication for Bluetooth
// Initialize OLED
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.clearDisplay();
}
void loop() {
// Control motors and LED based on input from Bluetooth
if (Serial.available()) {
t = Serial.read();
Serial.println(t); // Display received character in Serial Monitor (for debugging)
}
// Motor and LED control logic based on received character
if (t == 'F') { // Forward
digitalWrite(13, HIGH);
digitalWrite(11, HIGH);
digitalWrite(12, LOW);
digitalWrite(10, LOW);
} else if (t == 'B') { // Reverse
digitalWrite(12, HIGH);
digitalWrite(10, HIGH);
digitalWrite(13, LOW);
digitalWrite(11, LOW);
} else if (t == 'L') { // Turn right
digitalWrite(11, HIGH);
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(10, LOW);
} else if (t == 'R') { // Turn left
digitalWrite(13, HIGH);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(10, LOW);
} else if (t == 'W') { // LED ON
digitalWrite(9, HIGH);
} else if (t == 'w') { // LED OFF
digitalWrite(9, LOW);
} else if (t == 'S') { // STOP
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
}
// Running text display logic
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
if (showText1) {
display.setCursor(textPosition, 0);
display.print(text1);
display.display();
// Move text position to the left
textPosition--;
// Reset text position after it leaves the screen
if (textPosition < -display.width()) {
textPosition = SCREEN_WIDTH; // Back to the right of the screen
showText1 = false; // Show text2 after text1
}
} else {
// Display text2 (static)
display.setCursor((SCREEN_WIDTH - (text2.length() * 6)) / 2, 0); // Center text2
display.print(text2);
display.display();
// Show text1 again after some time
if (currentMillis - previousMillis >= 3000) { // Change text after 3 seconds
previousMillis = currentMillis;
textPosition = SCREEN_WIDTH; // Reset position for text1
showText1 = true; // Back to running text text1
}
}
}
}
This code is responsible for controlling the motors and LEDs based on Bluetooth input, as well as managing the display of running text on the OLED screen. The code includes setup for the OLED display and motor control pins, and the main loop handles Bluetooth communication and motor/LED control logic. The running text feature alternates between two strings, with one scrolling across the display and the other displayed statically in the center.