Created
June 2, 2025 23:24
-
-
Save greed9/04f68dd5e17b68c2e1569ceaa19f6d36 to your computer and use it in GitHub Desktop.
Starter Sketch for Light Meter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Based on code Written by Limor Fried/Ladyada for Adafruit Industries, | |
with contributions from the open source community. | |
BSD license, check license.txt for more information | |
All text above, and the splash screen below must be | |
included in any redistribution. | |
**************************************************************************/ | |
#include <SPI.h> | |
#include <Wire.h> | |
#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 | |
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) | |
// The pins for I2C are defined by the Wire-library. | |
// On an arduino UNO: A4(SDA), A5(SCL) | |
// On an arduino MEGA 2560: 20(SDA), 21(SCL) | |
// On an arduino LEONARDO: 2(SDA), 3(SCL), ... | |
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) | |
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32 | |
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); | |
// 'whiteboots', 128x64px | |
const unsigned char epd_bitmap_whiteboots [] PROGMEM = { | |
0xfc, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x00, 0xff, | |
0x14, 0x00, 0x0f, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x9b, 0x00, 0x01, 0xff, | |
0x00, 0x00, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0xfe, 0x80, 0x03, 0xff, | |
0x00, 0x01, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0x80, 0x03, 0xff, | |
0x00, 0x07, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3d, 0xf7, 0xc0, 0x07, 0xff, | |
0x00, 0x07, 0xff, 0xc0, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0x00, 0x0f, 0xff, | |
0x00, 0x17, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x8f, 0xff, 0x80, 0x1f, 0xff, | |
0x00, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x27, 0xbf, 0xf3, 0x80, 0x3f, 0xff, | |
0x07, 0xff, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x09, 0x1f, 0xff, 0xe0, 0x3f, 0xff, | |
0x18, 0x8f, 0x00, 0x00, 0x13, 0x98, 0x80, 0x08, 0x01, 0x00, 0x13, 0xff, 0xff, 0xe0, 0x7f, 0xff, | |
0x25, 0xff, 0x72, 0x03, 0x00, 0xe8, 0x40, 0x00, 0x04, 0x00, 0xa7, 0xff, 0xff, 0xe0, 0xff, 0xff, | |
0x3f, 0xff, 0xc6, 0x00, 0x45, 0x20, 0x80, 0x00, 0x00, 0x86, 0x4e, 0xff, 0xff, 0xe0, 0xff, 0xff, | |
0x7f, 0xff, 0xfb, 0x8c, 0x00, 0x60, 0x08, 0x00, 0x00, 0xf9, 0x0f, 0x7f, 0xff, 0xd1, 0xff, 0xff, | |
0x7f, 0xff, 0xf0, 0x70, 0x00, 0x30, 0x30, 0x00, 0x07, 0xf2, 0x1b, 0xff, 0xff, 0xcf, 0xff, 0xff, | |
0x7f, 0xff, 0xfe, 0xe2, 0x02, 0x10, 0x70, 0x7f, 0xff, 0xc4, 0x03, 0x3f, 0xff, 0xef, 0xff, 0xff, | |
0xff, 0xff, 0xff, 0xf0, 0xdf, 0x98, 0x1f, 0xff, 0xfc, 0x00, 0x2f, 0xff, 0xff, 0xf0, 0xff, 0xf7, | |
0xff, 0xff, 0xff, 0xf2, 0x1f, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x3e, 0x7f, 0xff, 0xfc, 0x9f, 0xff, | |
0xdf, 0xff, 0xff, 0xff, 0x01, 0x37, 0xff, 0xff, 0xf4, 0x08, 0xdf, 0x3f, 0xff, 0xf4, 0x4f, 0xff, | |
0x8f, 0xff, 0xff, 0xfe, 0x00, 0x00, 0xff, 0xff, 0xf8, 0x09, 0xff, 0xff, 0xdb, 0xfc, 0x03, 0xff, | |
0x0f, 0xff, 0xff, 0xff, 0x00, 0x00, 0x7f, 0xff, 0xc8, 0x07, 0xff, 0xff, 0xff, 0xc8, 0x06, 0xff, | |
0x07, 0xff, 0xff, 0xff, 0x74, 0x00, 0x0f, 0xff, 0xe0, 0x02, 0xef, 0xff, 0xf2, 0x84, 0x01, 0xff, | |
0x03, 0xff, 0xff, 0xff, 0xe1, 0xc4, 0x0f, 0xff, 0xf9, 0x25, 0xed, 0xff, 0xf9, 0x88, 0x05, 0xff, | |
0x81, 0xff, 0xff, 0xff, 0xfe, 0x25, 0x2f, 0xff, 0xff, 0xf9, 0xbf, 0x7f, 0xff, 0xff, 0x13, 0xff, | |
0x40, 0xff, 0xff, 0xff, 0xfd, 0x02, 0xff, 0xff, 0xff, 0xc5, 0xfb, 0xfe, 0xfb, 0xff, 0xc7, 0xff, | |
0xee, 0xff, 0xff, 0xff, 0xfe, 0xcb, 0xbf, 0xff, 0xfe, 0xfc, 0x06, 0xd2, 0x55, 0xff, 0xff, 0xff, | |
0xe9, 0xff, 0xff, 0xff, 0xfa, 0xc7, 0x9b, 0xfd, 0xfd, 0xea, 0xff, 0xff, 0xaf, 0xff, 0xff, 0xff, | |
0xf0, 0x7f, 0xff, 0xff, 0xbf, 0xaa, 0x94, 0xfe, 0xbe, 0x60, 0xfd, 0x7f, 0x97, 0xff, 0xff, 0xff, | |
0xf8, 0xff, 0xbf, 0xff, 0xef, 0x3d, 0x38, 0xe2, 0x3c, 0x72, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
0xfc, 0x77, 0xff, 0xfd, 0xda, 0xdf, 0x3c, 0x75, 0x7c, 0x78, 0x7f, 0xff, 0xf3, 0xff, 0xff, 0xff, | |
0xff, 0x58, 0xff, 0xf7, 0xfe, 0xde, 0x8e, 0x1c, 0x1e, 0x78, 0x3f, 0xff, 0xfe, 0xff, 0xfb, 0xff, | |
0xff, 0x80, 0x7f, 0xff, 0xde, 0xff, 0x0e, 0x08, 0x7e, 0x3e, 0x7f, 0xff, 0xfc, 0x6f, 0xfb, 0xfe, | |
0xff, 0xc0, 0x3b, 0xff, 0xff, 0x7f, 0x0f, 0x8c, 0x3c, 0xfe, 0x7f, 0x40, 0x3f, 0xff, 0xf6, 0xfe, | |
0xff, 0xff, 0x7f, 0xc7, 0xff, 0xff, 0x0f, 0x8d, 0xff, 0x58, 0xf8, 0x00, 0x7f, 0xfe, 0xfc, 0xff, | |
0xff, 0xff, 0xff, 0xb7, 0xff, 0xff, 0x87, 0xea, 0xff, 0x83, 0xc0, 0x02, 0x7f, 0xff, 0xf8, 0xbf, | |
0xff, 0xfe, 0xdf, 0xd7, 0xff, 0xef, 0xfc, 0x37, 0xbf, 0x9f, 0x00, 0x06, 0xff, 0xf9, 0xc1, 0x3f, | |
0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x7f, 0x8f, 0xff, 0xbe, 0x3f, 0x05, 0xff, 0xf8, 0x00, 0x1f, | |
0xff, 0xff, 0xff, 0x0d, 0x84, 0x00, 0x0f, 0xcf, 0xff, 0x3e, 0x1f, 0xa7, 0xfb, 0x7b, 0xf2, 0x1f, | |
0xff, 0xff, 0xff, 0xff, 0xce, 0x80, 0xe3, 0xf5, 0xfe, 0x7c, 0x20, 0xff, 0xef, 0xff, 0xf8, 0x17, | |
0xff, 0xff, 0xff, 0xff, 0xe7, 0x41, 0xf5, 0xff, 0xff, 0xbc, 0x1f, 0xff, 0xff, 0xf3, 0xfc, 0x0f, | |
0xff, 0xff, 0xff, 0xff, 0xf9, 0xc1, 0xec, 0xff, 0xff, 0xe8, 0xff, 0xff, 0xfd, 0xe7, 0xe8, 0x0e, | |
0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x98, 0x3f, 0xfb, 0xe0, 0x7f, 0xf7, 0xdf, 0xc7, 0xf0, 0x0e, | |
0xff, 0xff, 0xfd, 0xff, 0x97, 0xff, 0xfa, 0x17, 0xff, 0xe0, 0x0f, 0xfe, 0xff, 0x8f, 0x80, 0x0f, | |
0xff, 0xff, 0xfc, 0xff, 0xff, 0xff, 0xfc, 0x03, 0xff, 0xe0, 0x0f, 0xff, 0x9e, 0x01, 0x80, 0x0e, | |
0xff, 0xff, 0x78, 0xff, 0xdf, 0xff, 0x7e, 0x01, 0xff, 0xe0, 0xff, 0xfd, 0xf0, 0xc5, 0x00, 0x1f, | |
0xff, 0xf7, 0xbb, 0x1f, 0xf2, 0xeb, 0x79, 0xa0, 0xff, 0xe1, 0xff, 0xdd, 0xc9, 0xfe, 0x00, 0x3f, | |
0xff, 0xe4, 0x5f, 0x9f, 0xff, 0xff, 0xff, 0xf0, 0x7f, 0xe1, 0xff, 0x4c, 0xe3, 0xc0, 0x00, 0x3f, | |
0xff, 0xf7, 0x7f, 0xdf, 0xff, 0xff, 0xff, 0xfc, 0x0f, 0x81, 0xfe, 0xf0, 0xb0, 0x01, 0xe1, 0x1f, | |
0xff, 0xf7, 0x5f, 0xf0, 0xff, 0xff, 0xff, 0xfc, 0x3f, 0xe3, 0xfb, 0x7c, 0xe6, 0x3e, 0x00, 0xff, | |
0xff, 0xe3, 0x7f, 0xf8, 0x03, 0xff, 0x7b, 0xff, 0x0e, 0x0f, 0xff, 0xce, 0x78, 0x5f, 0x83, 0x3f, | |
0xff, 0xf6, 0xcf, 0x78, 0x30, 0x9f, 0xff, 0xff, 0xc0, 0x1f, 0xff, 0xff, 0xfb, 0xfc, 0x03, 0x7f, | |
0xff, 0xf1, 0x8e, 0xff, 0xfe, 0xc0, 0x67, 0xff, 0xf8, 0x3f, 0xff, 0x87, 0x07, 0xfd, 0x23, 0xff, | |
0xff, 0xf8, 0x1e, 0x9f, 0xff, 0xdf, 0xff, 0x3f, 0xfe, 0x7f, 0xff, 0xde, 0xff, 0xdf, 0x00, 0xff, | |
0xff, 0xfc, 0x0e, 0x1f, 0xe0, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xfc, 0x7f, 0xff, 0xbe, 0x03, 0xff, | |
0xff, 0xfc, 0x09, 0xce, 0x70, 0x3f, 0xff, 0xff, 0xfe, 0x1f, 0xf7, 0xfb, 0xff, 0xf8, 0x01, 0xff, | |
0xff, 0xff, 0xb7, 0xfc, 0x46, 0x3f, 0xff, 0xff, 0xf0, 0x00, 0x09, 0xef, 0xff, 0x40, 0x07, 0xff, | |
0xff, 0xff, 0x83, 0xfd, 0xff, 0x03, 0xff, 0xff, 0x0f, 0xfc, 0xf8, 0x3b, 0xf8, 0x00, 0x7f, 0xff, | |
0xff, 0xff, 0xc0, 0x5f, 0xff, 0xff, 0xff, 0xfc, 0x3f, 0xff, 0xf8, 0x00, 0x20, 0x01, 0xff, 0xff, | |
0xff, 0xff, 0xfc, 0x2f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, | |
0xff, 0xff, 0xf4, 0xc3, 0xff, 0xff, 0xff, 0xf7, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, | |
0xff, 0xff, 0xf2, 0x46, 0x6f, 0xff, 0xff, 0xea, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, | |
0xff, 0xff, 0xff, 0x24, 0xaf, 0xff, 0xff, 0xe8, 0x00, 0x40, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, | |
0xff, 0xff, 0xff, 0x80, 0x13, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, | |
0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, | |
0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x1f, 0xff, 0xff | |
}; | |
// Array of all bitmaps for convenience. (Total bytes used to store images in PROGMEM = 1040) | |
const int epd_bitmap_allArray_LEN = 1; | |
const unsigned char* epd_bitmap_allArray[1] = { | |
epd_bitmap_whiteboots | |
}; | |
void setup() { | |
// put your setup code here, to run once: | |
Serial.begin(9600); | |
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally | |
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { | |
Serial.println(F("SSD1306 allocation failed")); | |
for(;;); // Don't proceed, loop forever | |
} | |
// Show initial display buffer contents on the screen -- | |
// the library initializes this with an Adafruit splash screen. | |
display.display(); | |
delay(2000); // Pause for 2 seconds | |
// Clear the buffer | |
display.clearDisplay(); | |
// bitmap | |
testDrawBitmap( ) ; | |
display.clearDisplay( ) ; | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
int rawLightLevel = analogRead( A0) ; | |
plotData( rawLightLevel ) ; | |
display.display( ) ; | |
delay( 10 ) ; | |
// Read the A/D to get a raw value | |
// call the plotData( ) function to scale and draw it | |
// call display() to make it show up | |
// kill a few 10th's of a second | |
} | |
// draws the cat face ("White Boots") | |
void testDrawBitmap(void) { | |
display.clearDisplay(); | |
display.drawBitmap( | |
0, | |
0, | |
epd_bitmap_whiteboots,display.width(), display.height(), 1); | |
display.display(); | |
delay(5000); | |
} | |
// scales and plots a tiny line between the current reading | |
// and the previous one | |
void plotData( int value) | |
{ | |
static int x = 0 ; //increments to generate X axis | |
static int prev = 0 ; // Remembers previous y value | |
value = map( value, 0, 1023, 1, 63 ) ; // rescale A/D to fit display | |
display.drawLine( x, prev, x++, value, 1 ) ; // draw a line from last X to new X, incrementing X | |
prev = value ; // save previous Y | |
// Clear and wrap at edge of screen | |
if( x > 128 ) | |
{ | |
display.clearDisplay( ) ; | |
x = 0 ; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment