Skip to content

Instantly share code, notes, and snippets.

@fakufaku
Created December 1, 2011 16:56
Show Gist options
  • Save fakufaku/1418174 to your computer and use it in GitHub Desktop.
Save fakufaku/1418174 to your computer and use it in GitHub Desktop.
LCDShield v2.0 - Helloworld
/*
Example of using the LCDShield v2.0
2011 (c) Fakufaku
This examples is released in the public domain
The screen backlight is dimmed after 5s. A shake of the display will
restore the backlight, thanks to the tilt sensor.
*/
#define DIM_TIME 5000
#define DIM_LEN 1000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>3
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
// pin layout
int backlightPin = 9;
int buzzerPin = 8;
int pinLightSensor = 0; // analog 0 as analog
int pinTiltSensor = 15; // analog 1 as digital
// screen variables
float brightness;
unsigned long dimmerTimer;
int dimmed;
int tilt_pre;
/**************************************************************************/
// Initialize
/**************************************************************************/
void setup()
{
// set pins
pinMode(backlightPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(pinTiltSensor, INPUT);
// set brightness
setBrightness(1.0f);
dimmed = 0;
tilt_pre = 0;
// set up the LCD's number of columns and rows:
lcd.begin(8, 2);
// Print a message to the LCD.
lcd.print("Welcome!");
buzz(4000, 2, 50, 150);
delay(2000);
// Initialize the chibi command line and set the speed to 9600 bps
Serial.begin(9600);
Serial.println("Hello.");
}
/**************************************************************************/
// Loop
/**************************************************************************/
void loop()
{
char line[9] = {0};
// dose rate on first row
lcd.setCursor(0, 0);
sprintf(line, "%s", "Hello ");
lcd.print(line);
// connection info on the 2nd row
lcd.setCursor(0, 1);
sprintf(line, "%s", " World");
lcd.print(line);
controlBrightness();
int tilt = digitalRead(pinTiltSensor);
Serial.print("Tilt: ");
Serial.println(tilt);
}
void buzz(int f, int p, int t_up, int t_dw)
{
if (p == 0)
return;
while (p > 0)
{
tone(buzzerPin, f);
delay(t_up);
noTone(buzzerPin);
p--;
if (p > 0)
delay(t_dw);
}
}
void setBrightness(float c)
{
analogWrite(backlightPin, (int)(c*255));
}
void controlBrightness()
{
float dim_coeff;
int light, tilt;
unsigned long delta_t = millis() - dimmerTimer;
// read light and tilt sensors
light = analogRead(pinLightSensor);
tilt = digitalRead(pinTiltSensor);
// check if dim reset
if (tilt != tilt_pre)
{
dimmerTimer = millis();
dimmed = 0;
}
tilt_pre = tilt;
// set a priori brightness
dim_coeff = light/1023.0;
// now handle dimming
if (!dimmed)
{
if (delta_t > DIM_TIME && delta_t < DIM_TIME + DIM_LEN)
{
dim_coeff *= (1.0 - ((float)delta_t - (float)DIM_TIME)/(float)DIM_LEN);
}
else if (delta_t >= DIM_TIME + DIM_LEN)
{
dim_coeff = 0.0;
dimmed = 1;
}
} else {
dim_coeff = 0.0;
}
setBrightness(dim_coeff);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment