I am making a house lighting automation sketch and circuit for modelrailroad scale model houses. Create a arduino nano project for mimicking daily living in the house from the lights that turn on and off during a typical day, we want to add a tv simulated light that flickers like a tv. One in the living room for evening viewing and one in the bedroom for morning news and night viewing, we like to go to bed with the bedroom tv on for two hours, but lights off.
However since it is a model railroad, we have to speed time up to 6:1 with the option to change that ratio.
We want to show a clock with time sped up to match the ratio
I want to be able to turn the night time code off (reduce to 1hr )
Daily simulation continues till the pico is turned off.
We want to use a 0.96 oled screen to show the time and event name.
Tell me how to implement with step by step instructions.
Assistant
Arduino Nano Project for Model Railroad House Lighting Automation
This project outlines the steps to create a realistic house lighting simulation for your model railroad using an Arduino Nano, a 0.96" OLED screen, and LEDs for house lights and TV simulation.
Materials:
Arduino Nano
0.96" I2C OLED Display
LEDs (for house lights and TVs)
Resistors (appropriate for your LEDs)
Connecting wires
Breadboard (optional for prototyping)
Step 1: Setting up the Arduino IDE
Install the Arduino IDE: Download and install the Arduino IDE from the official website (arduino.cc).
Install Libraries:
Adafruit SSD1306: For controlling the OLED display. Go to Sketch -> Include Library -> Manage Libraries... and search for "Adafruit SSD1306". Install the library by Adafruit.
Adafruit GFX Library: This is a dependency for the SSD1306 library. Install it the same way as above.
Step 2: Wiring the Circuit
OLED Display: Connect the OLED display to the Arduino Nano using I2C communication:
VCC: to Arduino Nano 5V
GND: to Arduino Nano GND
SCL: to Arduino Nano A5
SDA: to Arduino Nano A4
LEDs: Connect each LED to a digital pin on the Arduino Nano through a current limiting resistor. Note down the pin numbers for each LED for programming.
Step 3: Writing the Arduino Code
Here's a sample code incorporating the features you requested:
cpp
#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
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Define LED pins
#define LIVING_ROOM_LIGHT 7
#define BEDROOM_LIGHT 8
#define LIVING_ROOM_TV 9
#define BEDROOM_TV 10
// Time speed up ratio
int timeRatio = 6;
// Flag for disabling night time code
bool nightTimeEnabled = true;
void setup() {
// Initialize the OLED display
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.clearDisplay();
display.display();
// Initialize LED pins as outputs
pinMode(LIVING_ROOM_LIGHT, OUTPUT);
pinMode(BEDROOM_LIGHT, OUTPUT);
pinMode(LIVING_ROOM_TV, OUTPUT);
pinMode(BEDROOM_TV, OUTPUT);
}
void loop() {
// Simulated time
unsigned long currentMillis = millis();
unsigned long simulatedMillis = currentMillis * timeRatio;
int simulatedSeconds = (simulatedMillis / 1000) % 60;
int simulatedMinutes = (simulatedMillis / (1000 * 60)) % 60;
int simulatedHours = (simulatedMillis / (1000 * 60 * 60)) % 24;
// Display time and event on OLED
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.print(simulatedHours);
display.print(":");
display.print(simulatedMinutes);
display.print(":");
display.print(simulatedSeconds);
display.setCursor(0, 30);
// Lighting logic based on simulated time
if (simulatedHours >= 7 && simulatedHours < 9) { // Morning
digitalWrite(BEDROOM_LIGHT, HIGH);
digitalWrite(BEDROOM_TV, HIGH);
display.println("Morning News");
} else if (simulatedHours >= 9 && simulatedHours < 18) { // Daytime
digitalWrite(BEDROOM_LIGHT, LOW);
digitalWrite(BEDROOM_TV, LOW);
digitalWrite(LIVING_ROOM_LIGHT, LOW);
digitalWrite(LIVING_ROOM_TV, LOW);
display.println("Daytime");
} else if (simulatedHours >= 18 && simulatedHours < 22) { // Evening
digitalWrite(LIVING_ROOM_LIGHT, HIGH);
digitalWrite(LIVING_ROOM_TV, HIGH);
display.println("Evening TV");
tvFlicker(LIVING_ROOM_TV);
} else if (simulatedHours >= 22 && simulatedHours < 24) { // Night
digitalWrite(LIVING_ROOM_LIGHT, LOW);
digitalWrite(LIVING_ROOM_TV, LOW);
digitalWrite(BEDROOM_LIGHT, HIGH);
digitalWrite(BEDROOM_TV, HIGH);
display.println("Night TV");
tvFlicker(BEDROOM_TV);
} else if (simulatedHours >= 0 && simulatedHours < 2) { // Late Night
digitalWrite(BEDROOM_LIGHT, LOW);
if (nightTimeEnabled) {
digitalWrite(BEDROOM_TV, HIGH);
display.println("Late Night TV");
tvFlicker(BEDROOM_TV);
} else {
digitalWrite(BEDROOM_TV, LOW);
display.println("Sleeping");
}
} else { // Sleeping
digitalWrite(BEDROOM_LIGHT, LOW);
digitalWrite(BEDROOM_TV, LOW);
display.println("Sleeping");
}
display.display();
delay(100); // Update every 100ms
}
// Simulate TV flickering
void tvFlicker(int tvPin) {
if (random(10) == 0) { // 10% chance of flicker
digitalWrite(tvPin, LOW);
delay(random(5, 50)); // Flicker duration
digitalWrite(tvPin, HIGH);
}
}
Step 4: Uploading and Testing
Upload the Code: Connect your Arduino Nano to your computer and upload the code using the Arduino IDE.
Test the Circuit: Observe the LEDs and the OLED display to see if the lighting simulation is working as expected.
Customization and Further Development:
Adjust Time Ratio: Modify the timeRatio variable to change the speed-up factor.
Night Time Control: Implement a button or switch to control the nightTimeEnabled flag for turning off the late-night TV.
More Realistic Flickering: Experiment with the tvFlicker() function to achieve a more realistic TV flickering effect.
Adding More Lights and Events: Expand the code to include more LEDs and events based on your desired simulation.
This detailed guide provides a solid foundation for your model railroad house lighting automation project. Remember to adjust the code and wiring based on your specific setup and preferences. Good luck!