Advertisement
Guest User

LED Puzzle Code

a guest
Feb 8th, 2016
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.21 KB | None | 0 0
  1. /* Duncan Flint 1/19/2016
  2.  *  
  3. Spring 2015 Final Light Puzzle
  4.  
  5. This is a complete rewrite of my previous light puzzle code. This rewrite is significantly shorter because it utilizes arrays and global variables to eliminate redundant code
  6.  
  7. Complete Project Writeup Availible at: https://integrateddiscoveries.wordpress.com/
  8.  
  9.  
  10. */  
  11.  
  12.   // GENERAL NOTE:
  13.   // When looking at the six LED/six button configuration, the leftmost LED is designated as LED 0! The rightmost is LED 5!
  14.  
  15.   int ledPins[] = {8,9,10,11,12,13}; // The numbers in this array correspond to the pins of the 6 LEDS, when looking from left to right
  16.   int buttonPins[] = {2,3,4,5,6,7}; // The numbers in this array correspond to the pins of the 6 buttons, when looking from left to right
  17.  
  18.   int ledStatus[] = {0,0,0,0,0,0}; // This array signifies whether or not each LED is on (= 1) or off (= 0). It is (and must be!) updated every time any of the LEDS change state.
  19.  
  20.  
  21. void setup()
  22. {
  23.   for (int i = 0; i < 6; i = i + 1)
  24.   {
  25.     pinMode(buttonPins[i], INPUT);  // All our button pins are inputs (We'll read values FROM them)
  26.     pinMode(ledPins[i], OUTPUT);  // All are light pins are outputs (We'll write values TO them)
  27.   }
  28.  
  29.   //Serial.begin(9600);
  30.   randomSeed(analogRead(0));  // Seed the random number generation with a random value. This random value is obtained from reading an unconnected analog pin
  31.   randomizeGameBoard();       // Randomize the game board
  32. }
  33.  
  34. void loop()
  35. {
  36.   invertLeds(readButtons()); // This is a little weird without reading the functions first. Basically invertleds takes one parameter, an integer corresponding to the index of the led that must be flipped. We get that from the function readbuttons, which returns whichever button is currently being pressed (or -1 if none are)
  37.   checkWinLoss();            // See if the user has won or lost (all lit up, or all deluminated)
  38.  
  39.   // Generation Test Code Below. Should be commented out in final build
  40.   //randomizeGameBoard();
  41.   //delay(1000);
  42. }
  43.  
  44. void randomizeGameBoard() // Randomly generate the values of all leds
  45. {
  46.   int randNum;            
  47.   for (int i = 0; i < 6; i++) // Do the following 5 times
  48.   {
  49.     randNum = random(2); // Pick a random number from 0 to 1
  50.     ledStatus[i] = randNum; // Set the LED to be that number
  51.   }
  52.   verifyLEDS(); // This function checks if the leds are set up in a challenging way (so you cant just solve the puzzle in one move)
  53.   updateLEDS(); // This function turns on whatever leds the ledstatus array tells it to
  54. }
  55.  
  56.  
  57.  
  58. int readButtons() // NOTE: This function will return the FIRST button that is being pressed (from left to right). If you press two buttons at the same time, it will only invert the LEDS corresponding to the one closest to the left.
  59. {
  60.   for (int i = 0; i < 6; i++)
  61.   {
  62.     if (digitalRead(buttonPins[i]) == 1)
  63.     {
  64.       return i;
  65.     }
  66.   }
  67.   return -1;
  68. }
  69.  
  70. void updateLEDS()
  71. {
  72.   for (int i = 0; i < 6; i++)
  73.   {
  74.     digitalWrite(i + 8, ledStatus[i]);
  75.   }
  76. }
  77.  
  78. int sumLEDS() // This function determines how many LEDS are on, by just adding up all the numbers in the LED array (since 1 is on, 0 is off)
  79. {
  80.   int ledSums = 0;
  81.   for (int i = 0; i < 6; i++)
  82.   {
  83.     if (ledStatus[i] == 1)
  84.     {
  85.     ledSums += 1;
  86.     }
  87.   }
  88.   return ledSums;
  89. }
  90.  
  91. void verifyLEDS()
  92. {
  93.   bool allOn = false;
  94.   bool allOff = false;
  95.   int ledSums = 0;
  96.  
  97.   ledSums = sumLEDS();
  98.  
  99.   //The statements below determine what light configurations are unacceptable and must be reset. The comments indicate these configurations "look like". X's are on, O's are off
  100.   //Unacceptable configurations include all on/all off (since it means the user doesn't get to do anything), or configurations where only one button needs to be pressed to win (Have all leds lit)
  101.   if ((ledSums == 0) || (ledSums == 6))                                                                   // OOOOOO, XXXXXX
  102.   {
  103.     randomizeGameBoard();
  104.   }
  105.  
  106.   else if ((ledSums == 2) &&  (ledStatus[2] == 1) && (ledStatus[3] == 1))                             // OOXXOO
  107.   {
  108.     randomizeGameBoard();
  109.   }
  110.  
  111.   else if (ledSums == 1)                                                                              // XOOOOO, OXOOOO, OOXOOO, OOOXOO, OOOOXO, OOOOOX
  112.   {
  113.     randomizeGameBoard();
  114.   }
  115.   else if ((ledSums == 3) && (ledStatus[0] == 1) && (ledStatus[1] == 1) && (ledStatus[2] == 1))       // XXXOOO
  116.   {
  117.     randomizeGameBoard();
  118.   }
  119.   else if ((ledSums == 3) && (ledStatus[3] == 1) && (ledStatus[4] == 1) && (ledStatus[5] == 1))       // OOOXXX
  120.   {
  121.     randomizeGameBoard();
  122.   }
  123.   else if ((ledSums == 4) && (ledStatus[4] == 0) && (ledStatus[5] == 0))                              // XXXXOO
  124.   {
  125.     randomizeGameBoard();
  126.   }
  127.   else if ((ledSums == 4) && (ledStatus[0] == 0) && (ledStatus[1] == 0))                              // OOXXXX
  128.   {
  129.     randomizeGameBoard();
  130.   }
  131.  
  132.   else if ((ledSums == 3) && (ledStatus[1] == 0) && (ledStatus[2] == 0) && (ledStatus[3] == 0))       // XOOOXX
  133.   {
  134.     randomizeGameBoard();
  135.   }
  136.  
  137.   else if ((ledSums == 3) && (ledStatus[2] == 0) && (ledStatus[3] == 0) && (ledStatus[4] == 0))       // XXOOOX
  138.   {
  139.     randomizeGameBoard();
  140.   }
  141.  
  142.  
  143.  
  144. }
  145.  
  146. void invertLeds(int ledToInvt)
  147. {
  148.   if (ledToInvt == -1)
  149.   {
  150.   return;
  151.   }
  152.   int ledToInvtPin = ledToInvt + 8; // Because our first light would correspond to i = 0, but the pin our first light is controlled by is at 2. Same for (1,3), (2,4), etc..
  153.   switch (ledToInvtPin)
  154.   {
  155.     case 8:                                                      
  156.     digitalWrite(ledToInvtPin, !ledStatus[ledToInvt]);
  157.     digitalWrite(ledToInvtPin + 1, !ledStatus[ledToInvt + 1]);
  158.     ledStatus[ledToInvt] = !ledStatus[ledToInvt];
  159.     ledStatus[ledToInvt + 1] = !ledStatus[ledToInvt + 1];
  160.     delay(200);
  161.     break;
  162.  
  163.     case 9:                                                         // Exact same behavior for cases 9-12 (LEDS 1-4), so case statements without breaks used to cause identical behavior
  164.     case 10:
  165.     case 11:
  166.     case 12:
  167.     digitalWrite(ledToInvtPin - 1, !ledStatus[ledToInvt - 1]);
  168.     digitalWrite(ledToInvtPin, !ledStatus[ledToInvt]);
  169.     digitalWrite(ledToInvtPin + 1, !ledStatus[ledToInvt + 1]);
  170.     ledStatus[ledToInvt - 1] = !ledStatus[ledToInvt - 1];
  171.     ledStatus[ledToInvt] = !ledStatus[ledToInvt];
  172.     ledStatus[ledToInvt + 1] = !ledStatus[ledToInvt + 1];
  173.     delay(200);
  174.     break;
  175.  
  176.     case 13:
  177.     digitalWrite(ledToInvtPin - 1, !ledStatus[ledToInvt - 1]);
  178.     digitalWrite(ledToInvtPin, !ledStatus[ledToInvt]);
  179.     ledStatus[ledToInvt - 1] = !ledStatus[ledToInvt - 1];
  180.     ledStatus[ledToInvt] = !ledStatus[ledToInvt];
  181.     delay(200);
  182.     break;
  183.  
  184.   }
  185. }
  186.  
  187. void checkWinLoss()           // Determines if all leds are on (win) or off (lose) by summing LED status array
  188. {
  189.   int ledSums= sumLEDS();
  190.   if (ledSums == 0)
  191.   {
  192.     youLose();
  193.   }
  194.   else if (ledSums == 6)
  195.   {
  196.     youWin();
  197.   }
  198. }
  199.  
  200. void youWin()                 // Flashes lights on/off to indicate victory, and resets board
  201. {
  202.   for (int j = 0; j < 8; j++)
  203. {
  204.   for (int i = 0; i < 6; i++)
  205.   {
  206.     digitalWrite(i+8, HIGH);
  207.   }
  208.   delay(200);
  209.   for (int i = 0; i < 6; i++)
  210.   {
  211.     digitalWrite(i+8, LOW);
  212.   }
  213.   delay(200);
  214. }
  215.  
  216. randomizeGameBoard();
  217. }
  218.  
  219. void youLose()
  220. {
  221.   randomizeGameBoard();
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement