/*
Wokwi | questions
Battleye — 4/2/24 at 10:11 PM
Project Link: https://wokwi.com/projects/394016653175543809?gh=1
https://discord.com/channels/787627282663211009/1224904058750636112/1224904058750636112
My goal is to display the numbers (as they appear in the GIF)
and allow the LED to display those colors (during the three
seconds it takes to switch from red to blue to red).
But even so, I'm having trouble getting the numbers to display
when x = -1 to 8 with the arrow.
Trying to put simultaneously into an Ardunio as well.
*/
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Wire.h>
const int SCREEN_I2C_ADDR = 0x3C; // or 0x3D
const int SCREEN_WIDTH = 128; // OLED display width, in pixels
const int SCREEN_HEIGHT = 64; // OLED display height, in pixels
const int OLED_RST_PIN = -1; // Reset pin (-1 if not available)
const int BLU_LED_PIN = 3;
const int RED_LED_PIN = 5;
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RST_PIN);
void setup() {
Serial.begin(9600);
pinMode(BLU_LED_PIN, OUTPUT);
pinMode(RED_LED_PIN, OUTPUT);
display.setRotation(2); // rotate screen 180 degrees
display.setTextSize(2); // draw 2X-scale text
display.setTextColor(SSD1306_WHITE, SSD1306_BLACK);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_I2C_ADDR)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
// Show initial buffer, the Adafruit splash screen
display.display();
delay(2000); // Pause for 2 seconds, then clear
display.clearDisplay();
display.display();
}
void loop() {
for (int x = -1; x <= 8; x += 1) {
Serial.print("If x = ");
Serial.print(x);
Serial.print(" the roller coaster is going ");
display.setCursor(10, 27);
display.print("X = ");
display.print(x);
display.print(" ");
display.drawLine(100, 14, 100, 50, SSD1306_WHITE);
if (x > 0 && (x < 2 || x > 3 && x < 5)) {
digitalWrite(BLU_LED_PIN, HIGH);
digitalWrite(RED_LED_PIN, LOW);
Serial.println("up.");
display.drawLine(90, 36, 100, 50, SSD1306_BLACK);
display.drawLine(110, 36, 100, 50, SSD1306_BLACK);
display.drawLine(90, 28, 100, 14, SSD1306_WHITE);
display.drawLine(110, 28, 100, 14, SSD1306_WHITE);
} else {
digitalWrite(BLU_LED_PIN, LOW);
digitalWrite(RED_LED_PIN, HIGH);
Serial.println("down.");
display.drawLine(90, 28, 100, 14, SSD1306_BLACK);
display.drawLine(110, 28, 100, 14, SSD1306_BLACK);
display.drawLine(90, 36, 100, 50, SSD1306_WHITE);
display.drawLine(110, 36, 100, 50, SSD1306_WHITE);
}
display.display();
delay(3000);
}
}