Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* 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 Finder
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2025-12-08 15:16:38
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Turn on an light emitting diode led when pushing a */
- /* pressbuttom. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES ******/
- #include <EasyButton.h>
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t mybutton_PushButton_PIN_D4 = 4;
- const uint8_t ledPin = 2; // Arduino pin for the LED
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- unsigned long startTime;
- unsigned long duration = 30000; // 30 seconds
- unsigned long currentNumber = 2;
- unsigned long maxPrime = 2;
- // Instance of the button.
- EasyButton myButton(mybutton_PushButton_PIN_D4);
- // Pin configuration for LED
- void setup() {
- Serial.begin(115200);
- startTime = millis();
- pinMode(mybutton_PushButton_PIN_D4, INPUT_PULLUP);
- pinMode(ledPin, OUTPUT);
- myButton.begin();
- // Attach callback for button press
- myButton.onPressed([](){ digitalWrite(ledPin, HIGH); }); // Turn on LED on press
- // Attach callback for button release
- myButton.onReleased([](){ digitalWrite(ledPin, LOW); }); // Turn off LED on release
- }
- // Prime number calculation and LED control loop
- void loop() {
- // Turn LED on when button is pressed
- myButton.read();
- if (millis() - startTime < duration) {
- if (isPrime(currentNumber)) {
- maxPrime = currentNumber;
- }
- currentNumber++;
- } else {
- Serial.print("Highest prime found in 30 seconds: ");
- Serial.println(maxPrime);
- while (true) {} // Stop the program
- }
- }
- // Check if number is prime
- bool isPrime(unsigned long n) {
- if (n < 2) return false;
- if (n % 2 == 0 && n != 2) return false;
- for (unsigned long i = 3; i * i <= n; i += 2) {
- if (n % i == 0) return false;
- }
- return true;
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment