Advertisement
pleasedontcode

"Prime Counter" rev_13

Dec 13th, 2023
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "Prime Counter"
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2023-12-13 11:00:10
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* if push button is pressed a number of times that */
  21.     /* is a prime number, so display on LCD the prime */
  22.     /* number. */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <Wire.h>
  27. #include <LiquidCrystal_I2C.h>
  28. #include <EasyButton.h>
  29. #include <math.h>
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup();
  33. void loop();
  34.  
  35. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  36. const uint8_t button_PushButton_PIN_D2 = 2;
  37.  
  38. /***** DEFINITION OF I2C PINS *****/
  39. const uint8_t Lcd_LCD1602I2C_I2C_PIN_SDA_A4 = A4;
  40. const uint8_t Lcd_LCD1602I2C_I2C_PIN_SCL_A5 = A5;
  41. const uint8_t Lcd_LCD1602I2C_I2C_SLAVE_ADDRESS = 0x27; // Replace with the correct slave address
  42.  
  43. /****** DEFINITION OF LIBRARY CLASS INSTANCES *****/
  44. LiquidCrystal_I2C lcd(Lcd_LCD1602I2C_I2C_SLAVE_ADDRESS, 20, 4); // Replace with the correct parameters
  45. EasyButton button1(button_PushButton_PIN_D2);
  46.  
  47. bool isPrimeNumber(int num)
  48. {
  49.   if (num <= 1)
  50.   {
  51.     return false;
  52.   }
  53.  
  54.   for (int i = 2; i <= sqrt(num); i++)
  55.   {
  56.     if (num % i == 0)
  57.     {
  58.       return false;
  59.     }
  60.   }
  61.  
  62.   return true;
  63. }
  64.  
  65. void setup()
  66. {
  67.   // Initialize the LCD with 20 columns and 4 rows
  68.   lcd.begin(20, 4);
  69.   // Set the backlight off
  70.   lcd.setBacklight(LOW);
  71.   // Clear the LCD screen
  72.   lcd.clear();
  73.  
  74.   // Initialize the button
  75.   button1.begin();
  76. }
  77.  
  78. void loop()
  79. {
  80.   // Read the button state
  81.   button1.read();
  82.  
  83.   static int pressCount = 0;
  84.  
  85.   if (button1.wasPressed())
  86.   {
  87.     pressCount++;
  88.    
  89.     // Check if the press count is a prime number
  90.     if (isPrimeNumber(pressCount))
  91.     {
  92.       // Turn on the backlight and display the prime number
  93.       lcd.setBacklight(HIGH);
  94.       lcd.setCursor(0, 0);
  95.       lcd.print("Prime Number: ");
  96.       lcd.print(pressCount);
  97.     }
  98.     else
  99.     {
  100.       // Turn off the backlight and clear the LCD screen
  101.       lcd.setBacklight(LOW);
  102.       lcd.clear();
  103.     }
  104.   }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement