// Learn about the ESP32 WiFi simulation in
// https://docs.wokwi.com/guides/esp32-wifi
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address (refer to the datasheet for your specific module)
LiquidCrystal_I2C lcd(0x27, 20, 4);
int counter = 0;
// External LED circuit to test code
int ledPin = 4;
const int channel = 0;
const int frequency = 1000;
const int resolution = 8;
// constants won't change. Used here to set pin numbers:
const int ledPin2 = 16; // the number of the LED pin
// Variables will change:
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000; // interval at which to blink (milliseconds)
void setup() {
// set the digital pin as output:
pinMode(ledPin2, OUTPUT);
ledcSetup(channel, frequency, resolution);
ledcAttachPin(ledPin, channel);
// put your setup code here, to run once:
lcd.init(); // Initialize the LCD - for WOKWI, this must be lcd.init()instead. For Arduino IDE, use lcd.begin.
lcd.backlight(); // Turn on the LCD backlight
lcd.setCursor(0, 0); // Set the cursor to the first column of the first row
lcd.print("Ken loves Lynda!!"); // Print a message
lcd.setCursor(0, 1); // Set the cursor to the first column of the first row
lcd.print("He thinks about her"); // Print a message
lcd.setCursor(0, 2); // Set the cursor to the first column of the first row
lcd.print("this many times/day:"); // Print a message
}
void loop() {
// put your main code here, to run repeatedly:
lcd.setCursor(0, 3);
lcd.print("Count:");
lcd.print(counter);
delay(10);
//lcd.clear();
counter++;
for (int fadeValue = 0; fadeValue <= 255; fadeValue += 5) {
ledcWrite(channel, fadeValue);
Serial.print("Fade Value:");
Serial.println(fadeValue);
delay(30);
}
for (int fadeValue = 255; fadeValue >= 0; fadeValue -= 5) {
ledcWrite(channel, fadeValue);
Serial.print("Fade Value:");
Serial.println(fadeValue);
delay(30);
}
// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin2, ledState);
}
}