Professional Documents
Culture Documents
Maker Space
Maker Space
© ) Apert oe avo ¢ ( atgivattoos ( ab ) == 0) ROC noo sf ( aigivattond ( as ) == 0) 129 // pot your wetup code heze, to nin ence ae if ( digitatnend (A) == 0) 190 Serisl.bogin (9600 5 nee if ( digivalRond ( As ) == 0 ) dort + 16; 1m 303 if ( digivalnond ( a2 ) == 0.) iport +~ 32; tae pintede ( 40, INPUT FOLIA )5 aot 46 ( agivatneod ( aL ) == 0 ) iport += 08; peal eae ee aos ¢ ( atgtvatneca ( aD) == 0) dport + 1285, tok plage ( a2, mPUT_FULIAE a ioe pintlede ( a2 , PUT FUL? 138 pinede ( 48 , IPUF_FULLOP Sf ( clasthetter == iPort ) 137 pluteoe ( a8 , PUT FOLIA 198 pinMiode ( A¢ , INPUT_PULLUP ); ~ eee er 190 eriatrimsan (“cpmHES* 9s 0 a testes; 12 pine (10 5 UE FOLD); st enay (100095 = 19a for (10, 4 < itetterIndex-1, 1 ++) 1s plo (18 5 om); //eiepay ica eae 1st pinee (28 | OONOUE);//aitpay ck Peele eeeet eucd baa feemiyee (722 meme (2191 = alae, eae eeneeee anna sor saci rinctn ( pete earning (0 a Ttartertnaes ) Js eee Baie sere 19 pint (4 | oto >; pa 1st pine (4 | oe), 200 cise tee pinto ( 8 | Ome): om 18 pinto ( 8 | orto 9: ot erie. printn ( “TACRBRCT 1k pine (7, oeU ), ee ase oe erent ere er 208 delay ( 1000 ); cel ena 208 aloplaytureetO: ‘USE randomSeed ( analogRead ( 12 ) }; ia Sa 158 rotatedutputs(); aid : om 08 while ( digitttend (10) += 108) Seay (100 5 tee aeveetntenmity (0 5 #5 m0) MAKER:this operation evaluates to 1; otherwise, it evaluates to 8. Therefore, i¢ount incre- ‘ments by either 1 or 0 depending on whether the bit is set. The right shift op- erator ( >>= ) shifts (Count, discarding the low-order bit and moving everything, ‘over one place. The loop repeats, check- ing the new low-order bit again. Even- tally, when the entire value has been right shifted, only 0s are left in n. At that point, n = 0, the uiTe loop exits, and return {Count returns the number of bits that are set. You'll see how this {used in the next function rotateOutputs The rotateoutputs function (lines 3-67) picks a random number unlit finds one with the appropriate number of bits; then, i tums on the associate output pins (the Tower row of eletrical connee- tions inthe puzzle). These connections ave used inthe current round of the game, indicated by LEDs. ‘The thre variables are the number of bits tobe se (needetits), the number obits inthe generated random number (\currentarts), and the current random ‘number (1Randon). The numberof bits needed (iNeeded- Bits) is set by calling counteits with the current character the players seek. The Unie (lines 38-41) loops unt Needed ts isnot equal to eount8ts(Randon), which as intally set to to, but i set to ran- 4on(e,255) within te loop. n this way, random numbers are chosen until one is found in which the numberof set bits ‘matches the goal; then the loop exit. Line 43 prints the random number to the serial monitor for debugging, and lines 44-66 update all ofthe electrical connections so that they ae all either 70H oF LOW, depending on the value of ‘Random set above. The binary ANDs (&) with { (ine 44), 2 (line 47), 128 {line 65), checking only one bitin the ‘umber ata time. For each bit, éigi~ taliite either turns the appropriate pin on or off. ledYES and ledNO ‘The lee¥€S and lecvo utility functions (limes 69-80) just display YES or NO on the display. One or the other is called when the player checks to see whether the current wiring is correct. In each. cease, the shouDigit function is called with position (first argument) and 26 MAKER ee) character (second argument) hard coded. showDigit ‘The shouigit function (lines 82-126) is responsible for drawing the provided character onthe display. star by defn ing int Pattern and then use suiten to find the character provided in letter ‘On each line ofthe suite function, case says If match "thisy then do whatever is after the colon (2). It does not match, it moves on to the next ease statement unit finds a match or drops otf the end without a mate, Once a match is found, Pattern i et toa num- ber represented in binary with the at the front L.., much lke a preceding 6x denotes hex notation), The binary number specifies which segments of the seven-segment display should be illuminated to display this character. Once the pattern is defined, the break ests the suten block, Without break, C would keep evaluating the est of the cage statements and possibly find another match. Here, the function exit 2s soon as a math i found Finally, ine 125 calls 1c. setRou, which isthe LED controller’ function to turn on ase of LEDs, setou takes three angu- ments: the chip number (always 0 in this case), the postion in which to place this character (Pes ition) and which seg- ments to illuminate (iPattern) Setup In the Arduino word, the special setup function (lines 128-164) is called once when the Arduino powers on allowing you t initialize variables, turn on hard- ware, and make sure everything is prop- erly configured before your main pro gram runs, To begin, the code initializes the serial monitor in the Arduino IDE ith Seta begin (3688) for debugging ressages. The baud rate (speed) of the serial connection is 9600, but this num- ber doesn't really matter, as long as it matches what is selected inthe serial monitor The pintode of analog pins AO through AA7 are set to INPUT_PULLUP. As the value suggests, this makes the pin an input and also enables its internal pull-up re sistor. Even though I'm using the Ardu- ino analog pins, I'm just using them as digital inputs inthis case. Pin 10 is set in the same way and willbe the “check” input to determine whether the current wiring is correct. ‘The next series of pinfodes sets a num ber of GPIO pins to OUTPUT. Pins 14,15, and 16 are the data, clock, and chip se lect lines of the LED driver, and pins 2 through 9 are the top row of electrical connections on the puzzle. The dig~ tallrte sets pin 16 to HIGH, which al- lows the LED driver to listen to incoming data, Because the project only has one driver, it can be left on indefinitely; in this case, it could even be connected di rectly to V+, but this arrangement was ‘more convenient. ‘The randonseed function initializes the Arduino's random number engine. An ‘analogtead of an unused digital input es sentially picks up static, causing the re- turn ofa random number, which is used asa seed ‘The rotatedutputs was defined earlier, s0 when the puzzle stats, the lower row of electrical connections is ready to go. ‘The last couple of lines set up the LED driver. An te.shutdown set to false means the LEDs should not be shutdown. The ‘ec setIntensi ty ses the brightness of the display. The range is 0 to 16, s0 a value of 8 sets brightness at 50 percent. Finally, le,clearDisplay erases anything that was previously on the display. The fist two functions ae called with a8 as the fist argument. The 7219 driver chip can be daisy-chained, so you have to specify the chip in the cain to which you are talk- ing. Because this setup only has one chip, the value wil alays be 8 Loop After the setup function finishes, the Ar duino Joop function (lines 166-210) loops continuously until power is removed. In this function resides the main logic of the program, ‘To begin, | check to see whether etastLetter does not equal the current puzzle character, isplayTarget (ie, show the value the player is trying 10 achieve), and update cLastLetter to the current puzzle character. Next check to see whether pin 10 {is LOA Iso, the player wants to knovr whether their current wiring is correct “To check, initialize sPort, then otgit~ alRead each ofthe input pins, and add the appropriate power of 2 if the pin is Lou (lines 178-185). Fora pin to be Lod, the player had to connect clip lead