/********* Pleasedontcode.com **********
Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- Terms and Conditions:
You have a non-exclusive, revocable, worldwide, royalty-free license
for personal and commercial use. Attribution is optional; modifications
are allowed, but you're responsible for code maintenance. We're not
liable for any loss or damage. For full terms,
please visit pleasedontcode.com/termsandconditions.
- Project: "Prime Counter"
- Source Code compiled for: Arduino Uno
- Source Code created on: 2023-12-13 11:00:10
********* Pleasedontcode.com **********/
/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
/* if push button is pressed a number of times that */
/* is a prime number, so display on LCD the prime */
/* number. */
/****** END SYSTEM REQUIREMENTS *****/
/****** DEFINITION OF LIBRARIES *****/
#include
#include
#include
#include
/****** FUNCTION PROTOTYPES *****/
void setup();
void loop();
/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t button_PushButton_PIN_D2 = 2;
/***** DEFINITION OF I2C PINS *****/
const uint8_t Lcd_LCD1602I2C_I2C_PIN_SDA_A4 = A4;
const uint8_t Lcd_LCD1602I2C_I2C_PIN_SCL_A5 = A5;
const uint8_t Lcd_LCD1602I2C_I2C_SLAVE_ADDRESS = 0x27; // Replace with the correct slave address
/****** DEFINITION OF LIBRARY CLASS INSTANCES *****/
LiquidCrystal_I2C lcd(Lcd_LCD1602I2C_I2C_SLAVE_ADDRESS, 20, 4); // Replace with the correct parameters
EasyButton button1(button_PushButton_PIN_D2);
bool isPrimeNumber(int num)
{
if (num <= 1)
{
return false;
}
for (int i = 2; i <= sqrt(num); i++)
{
if (num % i == 0)
{
return false;
}
}
return true;
}
void setup()
{
// Initialize the LCD with 20 columns and 4 rows
lcd.begin(20, 4);
// Set the backlight off
lcd.setBacklight(LOW);
// Clear the LCD screen
lcd.clear();
// Initialize the button
button1.begin();
}
void loop()
{
// Read the button state
button1.read();
static int pressCount = 0;
if (button1.wasPressed())
{
pressCount++;
// Check if the press count is a prime number
if (isPrimeNumber(pressCount))
{
// Turn on the backlight and display the prime number
lcd.setBacklight(HIGH);
lcd.setCursor(0, 0);
lcd.print("Prime Number: ");
lcd.print(pressCount);
}
else
{
// Turn off the backlight and clear the LCD screen
lcd.setBacklight(LOW);
lcd.clear();
}
}
}