const byte LED = 3; // Timer 2 "B" output: OC2B
const long frequency = 12500L; // Hz
void setup()
{
pinMode (LED, OUTPUT);
TCCR2A = _BV (WGM20) | _BV (WGM21) | _BV (COM2B1); // fast PWM, clear OC2B on compare
TCCR2B = _BV (WGM22) | _BV (CS21); // fast PWM, prescaler of 8
OCR2A = ((F_CPU / 8) / frequency) - 1; // zero relative
OCR2B = ((OCR2A + 1) / 2) - 1; // 50% duty cycle
} // end of setup
void loop() { }
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
int WhichScreen =1; // This variable stores the current Screen number
boolean hasChanged = true;
const int buttonPin = 4; // the number of the pushbutton pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup()
{
lcd.init();// initialize the lcd
lcd.backlight();
pinMode(buttonPin, INPUT);
}
void loop()
{
if (hasChanged == true) {
switch(WhichScreen) {
case 1:
{
firstScreen();
}
break;
case 2:
{
secondScreen();
}
break;
case 3:
{
thirdScreen();
}
break;
case 4:
{
fourthScreen();
}
break;
case 5:
{
fifthScreen();
}
break;
case 6:
{
sixthScreen();
}
break;
case 0:
{
}
break;
}
}
//-------------------------------
// BEGIN of the switch debouncing code
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
hasChanged = true;
WhichScreen++;
}
} else {
hasChanged = false;
}
}
lastButtonState = reading;
// END of the switch Debouncing code
// --------------------------------------
if (WhichScreen > 6){
WhichScreen = 1;
}
}
void firstScreen()
{
lcd.clear();
lcd.setCursor(0,0); // Column, line
lcd.print("This is screen 1");
lcd.setCursor(0,1);
lcd.print("On a menu system");
}
void secondScreen()
{
lcd.clear();
lcd.setCursor(0,0); // Column, line
lcd.print(" Second screen");
lcd.setCursor(0,1);
lcd.print("of my menu !!");
}
void thirdScreen()
{
lcd.clear();
lcd.setCursor(0,0); // Column, line
lcd.print("Hello, world!");
lcd.setCursor(0,1);
lcd.print("Third screen (3)");
}
void fourthScreen()
{
lcd.clear();
lcd.setCursor(0,0); // Column, line
lcd.print("This is screen 4");
lcd.setCursor(0,1);
lcd.print("Just press btn");
}
void fifthScreen()
{
lcd.clear();
lcd.setCursor(0,0); // Column, line
lcd.print(" Fifth screen");
lcd.setCursor(0,1);
lcd.print("i2C LCD screen");
}
void sixthScreen()
{
lcd.clear();
lcd.setCursor(0,0); // Column, line
lcd.print("THE last screen");
lcd.setCursor(0,1);
lcd.print(" Sixth and last");
}