pleasedontcode

Prime Finder rev_01

Dec 8th, 2025
35
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 Finder
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-12-08 15:16:38
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Turn on an light emitting diode led when pushing a */
  21.     /* pressbuttom. */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24.  
  25. /* START CODE */
  26.  
  27. /****** DEFINITION OF LIBRARIES ******/
  28. #include <EasyButton.h>
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33.  
  34. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  35. const uint8_t mybutton_PushButton_PIN_D4 = 4;
  36. const uint8_t ledPin = 2; // Arduino pin for the LED
  37.  
  38. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  39.  
  40. unsigned long startTime;
  41. unsigned long duration = 30000; // 30 seconds
  42. unsigned long currentNumber = 2;
  43. unsigned long maxPrime = 2;
  44.  
  45. // Instance of the button.
  46. EasyButton myButton(mybutton_PushButton_PIN_D4);
  47.  
  48. // Pin configuration for LED
  49. void setup() {
  50.   Serial.begin(115200);
  51.   startTime = millis();
  52.   pinMode(mybutton_PushButton_PIN_D4, INPUT_PULLUP);
  53.   pinMode(ledPin, OUTPUT);
  54.   myButton.begin();
  55.   // Attach callback for button press
  56.   myButton.onPressed([](){ digitalWrite(ledPin, HIGH); }); // Turn on LED on press
  57.   // Attach callback for button release
  58.   myButton.onReleased([](){ digitalWrite(ledPin, LOW); }); // Turn off LED on release
  59. }
  60.  
  61. // Prime number calculation and LED control loop
  62. void loop() {
  63.   // Turn LED on when button is pressed
  64.   myButton.read();
  65.  
  66.   if (millis() - startTime < duration) {
  67.     if (isPrime(currentNumber)) {
  68.       maxPrime = currentNumber;
  69.     }
  70.     currentNumber++;
  71.   } else {
  72.     Serial.print("Highest prime found in 30 seconds: ");
  73.     Serial.println(maxPrime);
  74.     while (true) {} // Stop the program
  75.   }
  76. }
  77.  
  78. // Check if number is prime
  79. bool isPrime(unsigned long n) {
  80.   if (n < 2) return false;
  81.   if (n % 2 == 0 && n != 2) return false;
  82.   for (unsigned long i = 3; i * i <= n; i += 2) {
  83.     if (n % i == 0) return false;
  84.   }
  85.   return true;
  86. }
  87.  
  88. /* END CODE */
  89.  
Advertisement
Add Comment
Please, Sign In to add comment