/********* 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: Button Control
- Source Code compiled for: Arduino Nano
- Source Code created on: 2024-01-22 13:37:09
********* Pleasedontcode.com **********/
/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
/* вмикає рандомно один з двух світлодіодів при */
/* натисканні кнопки */
/****** SYSTEM REQUIREMENT 2 *****/
/* вмикає рандомно один з п'яти світлодіодів при */
/* натисканні кнопки */
/****** END SYSTEM REQUIREMENTS *****/
/****** DEFINITION OF LIBRARIES *****/
#include
#include
/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void toggleRandomLED(void);
void toggleRandomMultiLED(void);
/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t knopka_PushButton_PIN_D2 = 2;
/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t svitlo_LED_PIN_D3 = 3;
const uint8_t svitlo_LED_PIN_D4 = 4;
const uint8_t svitlo_LED_PIN_D5 = 5;
const uint8_t svitlo_LED_PIN_D6 = 6;
const uint8_t svitlo_LED_PIN_D7 = 7;
/****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
EasyButton knopka_PushButton(knopka_PushButton_PIN_D2); // Create an instance of EasyButton for the push button
void setup(void)
{
// put your setup code here, to run once:
pinMode(knopka_PushButton_PIN_D2, INPUT_PULLUP);
pinMode(svitlo_LED_PIN_D3, OUTPUT);
pinMode(svitlo_LED_PIN_D4, OUTPUT);
pinMode(svitlo_LED_PIN_D5, OUTPUT);
pinMode(svitlo_LED_PIN_D6, OUTPUT);
pinMode(svitlo_LED_PIN_D7, OUTPUT);
knopka_PushButton.begin(); // Initialize the EasyButton instance
}
void loop(void)
{
// put your main code here, to run repeatedly:
knopka_PushButton.read(); // Read the state of the push button
// System Requirement 1: Toggle a random LED when the button is pressed
if (knopka_PushButton.isPressed()) {
toggleRandomLED();
} else {
digitalWrite(svitlo_LED_PIN_D3, LOW);
}
// System Requirement 2: Toggle a random multi-LED pattern when the button is pressed
if (knopka_PushButton.isPressed()) {
toggleRandomMultiLED();
} else {
digitalWrite(svitlo_LED_PIN_D4, LOW);
digitalWrite(svitlo_LED_PIN_D5, LOW);
digitalWrite(svitlo_LED_PIN_D6, LOW);
digitalWrite(svitlo_LED_PIN_D7, LOW);
}
}
void toggleRandomLED(void)
{
// Generate a random number between 3 and 7
uint8_t randomLED = random(3, 8);
// Turn off all LEDs
digitalWrite(svitlo_LED_PIN_D3, LOW);
digitalWrite(svitlo_LED_PIN_D4, LOW);
digitalWrite(svitlo_LED_PIN_D5, LOW);
digitalWrite(svitlo_LED_PIN_D6, LOW);
digitalWrite(svitlo_LED_PIN_D7, LOW);
// Toggle the random LED
if (randomLED == svitlo_LED_PIN_D3) {
digitalWrite(svitlo_LED_PIN_D3, HIGH);
} else if (randomLED == svitlo_LED_PIN_D4) {
digitalWrite(svitlo_LED_PIN_D4, HIGH);
} else if (randomLED == svitlo_LED_PIN_D5) {
digitalWrite(svitlo_LED_PIN_D5, HIGH);
} else if (randomLED == svitlo_LED_PIN_D6) {
digitalWrite(svitlo_LED_PIN_D6, HIGH);
} else if (randomLED == svitlo_LED_PIN_D7) {
digitalWrite(svitlo_LED_PIN_D7, HIGH);
}
}
void toggleRandomMultiLED(void)
{
// Generate a random number between 4 and 7
uint8_t randomMultiLED = random(4, 8);
// Turn off all LEDs
digitalWrite(svitlo_LED_PIN_D3, LOW);
digitalWrite(svitlo_LED_PIN_D4, LOW);
digitalWrite(svitlo_LED_PIN_D5, LOW);
digitalWrite(svitlo_LED_PIN_D6, LOW);
digitalWrite(svitlo_LED_PIN_D7, LOW);
// Toggle the random multi-LED pattern
if (randomMultiLED == 4) {
digitalWrite(svitlo_LED_PIN_D4, HIGH);
} else if (randomMultiLED == 5) {
digitalWrite(svitlo_LED_PIN_D5, HIGH);
} else if (randomMultiLED == 6) {
digitalWrite(svitlo_LED_PIN_D6, HIGH);
} else if (randomMultiLED == 7) {
digitalWrite(svitlo_LED_PIN_D7, HIGH);
}
}