#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const int amplitude = 30; // Amplitude of the sine wave
const float frequency = 0.05; // Frequency of the sine wave
int x = 0; // X coordinate
const int SquareWavefrequency = 2; // Frequency in Hz
unsigned long previousMillis = 0;
bool waveState = false;
const int waveWidth = SCREEN_WIDTH / 4;
void sine_wave();
void drawSquareWave(bool state);
void setup() {
// Initialize the OLED display
display.begin(SSD1306_SWITCHCAPVCC, 0x3c);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.print("Hello, World!");
display.display();
delay(1000);
}
void loop() {
unsigned long currentMillis = millis();
//sine_wave();
delay(1000);
// Check if it's time to change the wave state
if (currentMillis - previousMillis >= 1000 / (SquareWavefrequency * 2)) {
previousMillis = currentMillis;
waveState = !waveState; // Toggle the wave state
}
display.clearDisplay();
// Call the function to draw the square wave
drawSquareWave(waveState);
display.display();
// Small delay for visual clarity
delay(10);
}
void sine_wave()
{
display.clearDisplay();
for (int x = 0; x < SCREEN_WIDTH; x++) {
int y = (amplitude * sin(frequency * (x + millis() / 10.0))) + (SCREEN_HEIGHT / 2);
display.drawPixel(x, y, WHITE);
}
display.display();
delay(30);
}
void drawSquareWave(bool state) {
// Define heights for the square wave
int highY = SCREEN_HEIGHT / 4; // Top position
int lowY = 3 * SCREEN_HEIGHT / 4; // Bottom position
// Fill rectangles for two waves
for (int i = 0; i < 2; i++) { // Loop for two square waves
int xOffset = i * waveWidth; // Offset for each wave
if (state) {
display.fillRect(xOffset, 0, waveWidth, highY, WHITE); // Fill top half
} else {
display.fillRect(xOffset, lowY, waveWidth, SCREEN_HEIGHT - lowY, WHITE); // Fill bottom half
}
}
}