Advertisement
pleasedontcode

"Random Colors" rev_05

Sep 2nd, 2024
331
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: "Random Colors"
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-09-02 15:05:09
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* fix the code. */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /****** DEFINITION OF LIBRARIES *****/
  24. #include <Adafruit_NeoPixel.h> // Include the NeoPixel library
  25.  
  26. /****** FUNCTION PROTOTYPES *****/
  27. void setup(void);
  28. void loop(void);
  29. void rndRed();
  30. void rndBlue();
  31. void rndGreen();
  32. void rndPurple();
  33.  
  34. /****** CONSTANTS AND VARIABLES *****/
  35. #define PIN        6
  36. #define NUMPIXELS 15
  37. Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRBW + NEO_KHZ800);
  38.  
  39. const int btnRed = 2;    // the pin that the pushbutton is attached to
  40. const int btnBlue = 3;   // the pin that the pushbutton is attached to
  41. const int btnGreen = 4;  // the pin that the pushbutton is attached to
  42. const int btnPurple = 5; // the pin that the pushbutton is attached to
  43.  
  44. void setup(void)
  45. {
  46.     randomSeed(analogRead(A0)); // Seed the random number generator with noise from analog pin A0
  47.    
  48.     // Set button pins as input
  49.     pinMode(btnRed, INPUT);
  50.     pinMode(btnBlue, INPUT);
  51.     pinMode(btnGreen, INPUT);
  52.     pinMode(btnPurple, INPUT);
  53.  
  54.     // Initialize serial communication:
  55.     Serial.begin(9600);
  56.    
  57.     // Initialize the NeoPixel strip
  58.     pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
  59.     pixels.clear(); // Set all pixel colors to 'off'
  60.     pixels.show();  // Update the strip to show the changes
  61.     delay(1000);    // Wait for a second
  62. }
  63.  
  64. void loop(void)
  65. {
  66.     // Check the state of each button and call the corresponding function if pressed
  67.     if (digitalRead(btnRed) == HIGH) {
  68.         rndRed(); // Call function to light up a random red pixel
  69.         delay(100); // Debounce delay
  70.     }
  71.  
  72.     if (digitalRead(btnBlue) == HIGH) {
  73.         rndBlue(); // Call function to light up a random blue pixel
  74.         delay(100); // Debounce delay
  75.     }
  76.  
  77.     if (digitalRead(btnGreen) == HIGH) {
  78.         rndGreen(); // Call function to light up a random green pixel
  79.         delay(100); // Debounce delay
  80.     }
  81.  
  82.     if (digitalRead(btnPurple) == HIGH) {
  83.         rndPurple(); // Call function to light up a random purple pixel
  84.         delay(100); // Debounce delay
  85.     }
  86. }
  87.  
  88. void rndRed() {
  89.     int led = random(0, NUMPIXELS); // Use NUMPIXELS constant for random range
  90.     pixels.setPixelColor(led, pixels.Color(255, 0, 0, 0)); // Set the pixel to red
  91.     pixels.show();   // Send the updated pixel colors to the hardware.
  92.     delay(500);      // Wait for half a second
  93. }
  94.  
  95. void rndBlue() {
  96.     int led = random(0, NUMPIXELS); // Use NUMPIXELS constant for random range
  97.     pixels.setPixelColor(led, pixels.Color(0, 0, 255, 0)); // Set the pixel to blue
  98.     pixels.show();   // Send the updated pixel colors to the hardware.
  99.     delay(500);      // Wait for half a second
  100. }
  101.  
  102. void rndGreen() {
  103.     int led = random(0, NUMPIXELS); // Use NUMPIXELS constant for random range
  104.     pixels.setPixelColor(led, pixels.Color(0, 255, 0, 0)); // Set the pixel to green
  105.     pixels.show();   // Send the updated pixel colors to the hardware.
  106.     delay(500);      // Wait for half a second
  107. }
  108.  
  109. void rndPurple() {
  110.     int led = random(0, NUMPIXELS); // Use NUMPIXELS constant for random range
  111.     pixels.setPixelColor(led, pixels.Color(171, 0, 255, 0)); // Set the pixel to purple
  112.     pixels.show();   // Send the updated pixel colors to the hardware.
  113.     delay(500);      // Wait for half a second
  114. }
  115.  
  116. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement