Skip to content

Instantly share code, notes, and snippets.

@embeddeduser
Last active May 1, 2017 00:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save embeddeduser/feab7144aceeabb6a9aa4e5862b92c75 to your computer and use it in GitHub Desktop.
Save embeddeduser/feab7144aceeabb6a9aa4e5862b92c75 to your computer and use it in GitHub Desktop.
EmbeddedUser Arduino Light Meter
// Light meter
// 12K resistor connected between A0 pin and GND
// Photoresistor connected between A0 and VCC 3.3V
#include "U8glib.h"
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NO_ACK); // Display which does not send AC
//U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0); // I2C / TWI
//U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0|U8G_I2C_OPT_NO_ACK|U8G_I2C_OPT_FAST); // Fast I2C / TWI
//U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NO_ACK); // Display which does not send AC
int sensorValue = 0;
unsigned int curOutputValue = 0;
unsigned int lastOutputValue;
void setup(void)
{
Serial.begin(9600);
lastOutputValue = ~curOutputValue;
// assign default color value
if (u8g.getMode() == U8G_MODE_R3G3B2)
{
u8g.setColorIndex(255); // white
}
else if (u8g.getMode() == U8G_MODE_GRAY2BIT)
{
u8g.setColorIndex(3); // max intensity
}
else if (u8g.getMode() == U8G_MODE_BW)
{
u8g.setColorIndex(1); // pixel on
}
else if (u8g.getMode() == U8G_MODE_HICOLOR)
{
u8g.setHiColorByRGB(255, 255, 255);
}
}
void LightMeter(uint16_t percentLight)
{
u8g.setFont(u8g_font_fub17);
u8g.setFontRefHeightExtendedText();
u8g.setDefaultForegroundColor();
u8g.setFontPosTop();
u8g.drawStr(4, 0, "Light %");
u8g.drawStr(4, 20, "Meter");
u8g.drawFrame(4, 40, 122, 24);
u8g.drawBox(8, 44, (114 * percentLight / 100), 16);
//
if (percentLight > 99)
{
u8g.setPrintPos(90, 15);
}
else
{
u8g.setPrintPos(100, 15);
}
u8g.print(percentLight);
}
void loop(void)
{
sensorValue = analogRead(A0);
Serial.print("input :");
Serial.println(sensorValue);
// map input values between 0 t0 100%
curOutputValue = map(sensorValue, 0, 690, 0, 100);
if (curOutputValue > 100)
{
curOutputValue = 100;
}
if (lastOutputValue != curOutputValue)
{
lastOutputValue = curOutputValue;
u8g.firstPage();
do
{
LightMeter(curOutputValue);
}
while (u8g.nextPage());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment