SHARE
TWEET

Untitled

a guest Mar 20th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream> // only needed beccause i print to console
  2.  
  3. int leds [4] = {12, 11, 10, 9};
  4. bool state [sizeof(*leds)];
  5. int brightness = 255;
  6.  
  7. void applyChanges() {
  8.     for(int i = 0; i < sizeof(*leds); i++) {
  9.         if (state[i]) {
  10.             std::cout << "Led " << i+1 << "(" << leds[i] << ") is now on, ";
  11.             std::cout << "with brightness " << brightness << ".\n";
  12.         } else {
  13.             std::cout << "Led " << i+1 << "(" << leds[i] << ") is now off.\n";
  14.         }
  15.     }
  16.    
  17. }
  18.  
  19. void toggle(int led) {
  20.     for(int i = 0; i < sizeof(*leds); i++) {
  21.         if (led == leds[i]) {
  22.             if (state[i]) {
  23.             state[i] = false;
  24.             break;
  25.         } else {
  26.             state[i] = true;
  27.             break;
  28.         }
  29.         }
  30.     }
  31.     applyChanges();
  32. }
  33.  
  34. void adjustBrightness(int value, bool increase) {
  35.     if (increase) {
  36.         brightness += value;
  37.     } else {
  38.         brightness -= value;
  39.     }
  40.     applyChanges();
  41. }
  42.  
  43. int main(int argc, char const *argv[]) {
  44.     // program starts here
  45.     std::string userInput;
  46.    
  47.     while (true) {
  48.         std::cin >> userInput;
  49.         if (userInput == "1"){
  50.             toggle(leds[0]);
  51.         } else if (userInput == "2") {
  52.             toggle(leds[1]);
  53.         } else if (userInput == "3") {
  54.             toggle(leds[2]);
  55.         } else if (userInput == "4"){
  56.             toggle(leds[3]);
  57.         } else if (userInput == "+") {
  58.             adjustBrightness(10, true);
  59.         } else if (userInput == "-") {
  60.             adjustBrightness(10, false);
  61.         }
  62.        
  63.         std::cout << "\nCurrent lamp state:\n ";        
  64.        
  65.         std::cout << leds[0];
  66.         for(int i = 1; i < sizeof(*leds); i++) {
  67.             std::cout << ", " << leds[i];
  68.         }
  69.        
  70.         std::cout << "\n[" << state[0];
  71.         for(int i = 1; i < sizeof(*leds); i++) {
  72.             std::cout << ",  " << state[i];
  73.         }
  74.         std::cout << "]\n---------------------------------------------------------------------\n";
  75.     }
  76.     return 0;
  77. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top