Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- OLED 0.96" display test code for Arduino
- Libraries used:
- SPI
- TWI/I2C (for temperature sensor)
- Hardware:
- LM35 temperature sensor, connected to pin A0
- Momentary push button connected to pin D2
- The debounce routine works but it can miss sometimes. I recommend
- using the Bounce2 library for a foolproof debouncing routine.
- */
- #include <SPI.h>
- #include <Wire.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_SSD1306.h>
- #define TEMP_PIN 0 // Pin A0 for LM35
- #define TEMP_CONVERSION 0.48828125
- #define TEMP_F(X) (X*9)/5 + 32
- #define OLED_RESET 4 // Reset line for OLED display
- #define BUTTON_PIN 2 // Pin D2 for button
- float tempC = 0.00; // Temperature value
- byte dispMode = 0; // Current display mode
- int buttonState; // variable for reading the pushbutton status
- int lastButtonState = LOW;
- unsigned int flipDisplay = 0;
- // the following variables are long's because the time, measured in miliseconds,
- // will quickly become a bigger number than can be stored in an int.
- unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
- const long PROGMEM debounceDelay = 50; // the debounce time; increase if the output flickers
- unsigned long lastReadTime = 0;
- const long PROGMEM readDelay = 2000; // Read delay amount for sensors
- Adafruit_SSD1306 display(OLED_RESET);
- void setup() {
- dispMode = 0;
- pinMode(BUTTON_PIN, INPUT);
- // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
- display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x64)
- // init done
- // Initialize the display
- display.clearDisplay();
- display.setTextSize(1);
- display.setTextColor(WHITE);
- display.display();
- tempC = analogRead(TEMP_PIN) * TEMP_CONVERSION;
- }
- void loop() {
- // Need to only do this but once every second or so
- if ( (millis() - lastReadTime) > readDelay) {
- lastReadTime = millis();
- flipDisplay = !flipDisplay;
- /* Take readings from sensors */
- tempC = analogRead(TEMP_PIN) * TEMP_CONVERSION;
- }
- int reading = digitalRead(BUTTON_PIN);
- // If the switch changed, due to noise or pressing:
- if (reading != lastButtonState) {
- // reset the debouncing timer
- lastDebounceTime = millis();
- }
- if ( (millis() - lastDebounceTime) > debounceDelay) {
- if ( reading != buttonState) {
- buttonState = reading;
- if ( buttonState == HIGH ) {
- dispMode++;
- if ( dispMode > 4 ) {
- dispMode = 0;
- }
- }
- }
- }
- lastButtonState = reading;
- switch (dispMode) {
- case 0:
- drawMode0();
- break;
- case 1:
- drawMode1();
- break;
- case 2:
- drawMode2();
- break;
- case 3:
- drawMode3();
- break;
- case 4:
- drawMode4();
- break;
- }
- display.display();
- }
- void drawHeader(boolean drawBeacon) {
- display.clearDisplay();
- display.setTextSize(1);
- display.setCursor(20,0);
- display.setTextColor(WHITE);
- display.println( F("USCSS Nostromo") );
- if ( drawBeacon ) {
- display.setTextColor(BLACK, WHITE); // 'inverted' text
- display.setCursor(9,8);
- display.println( F(" Beacon Detected ") );
- }
- }
- void drawTempC() {
- display.print(tempC);
- display.print(" ");
- display.print((char)248);
- display.println(F("C"));
- }
- void drawTempF() {
- display.print(TEMP_F(tempC));
- display.print(" ");
- display.print((char)248);
- display.println(F("F"));
- }
- void drawMode0() {
- drawHeader(false);
- display.setTextSize(1);
- display.setTextColor(WHITE);
- display.setCursor(0,16);
- drawTempC();
- drawTempF();
- display.println(F("Distance: 500m"));
- display.print(F("Time: "));
- display.print(millis()/1000);
- display.print(F("s"));
- //display.println("Signal Strength: LOW");
- }
- /*
- Big temperature in F
- */
- void drawMode1() {
- drawHeader(false);
- display.setTextSize(2);
- display.setTextColor(WHITE);
- display.setCursor(18, 32);
- drawTempF();
- }
- /*
- Big temperature in C
- */
- void drawMode2() {
- drawHeader(false);
- display.setTextSize(2);
- display.setTextColor(WHITE);
- display.setCursor(18, 32);
- drawTempC();
- }
- /*
- Distance
- */
- void drawMode3() {
- drawHeader(false);
- display.setTextSize(2);
- display.setTextColor(WHITE);
- display.setCursor(16, 16);
- display.println(F("Distance"));
- if ( flipDisplay )
- display.setTextColor(BLACK, WHITE);
- display.setCursor(24, 36);
- display.print(F(" 500m "));
- }
- /*
- Display signal bars
- */
- void drawMode4() {
- drawHeader(true);
- display.setCursor(16,18);
- display.setTextSize(1);
- display.setTextColor(WHITE);
- display.println(F("Signal Strength"));
- display.fillRect(48, 40, 2, 8, WHITE);
- display.fillRect(52, 40, 2, 8, WHITE);
- display.fillRect(56, 36, 2, 12, WHITE);
- display.fillRect(60, 32, 2, 16, WHITE);
- display.drawFastHLine(48, 50, 14, WHITE);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement