Вы находитесь на странице: 1из 6
Chapter 9 Project 30 Evil Genius Countdown Timer No book on projects for an Evil Genius should be without the Bond-style countdown timer, complete with a rat’s nest of colored wires (see Figure 9-15). This timer also doubles as an egg timer, because there is nothing that annoys the Evil Genius more than an overcooked soft-boiled egg! COMPONENTS AND EQUIPMENT. Description Appendix Arduino Diecimila or Duemilanove board or clone 1 DI-4 2-digit, 7-segment LED display (common anode) 33 RIS 100 KN OSW metal film resistor B R47 1K0 0.5 metal film resistor R815 100 1 0.5W metal film resistor Tra BC307 39 Rotary encoder 97 Piezobuzzer (integrated electronics) 68 = Miscellaneous Projects 163 used is For optimal loudness, the piezobwz the kind that has integrated electronics so all that is necessary to make it sound is to provide it with SV. Be careful to connect it in the correct way. Hardware ‘The project is similar to Project 15, but with the extra seven-segment display and associated transistors. We also have a rotary encoder that we will use to set the time to count down from. We have used both components before; for more information on rotary encoders, see Chapter 6. The schematic diagram for the project is shown in Figure 9-16 and the breadboard layout in Figure 9-17 Software ‘The sketch for this project (Project Listing 30) is, mostly concerned with keeping the display up-to- date and creating the illusion that all four displays are lit at the same time, when in fact, only one will ever be lit. The way this is accomplished is described in Project 15, BENE Project 30 Evil Genius countdown timer, 164 30 Arduino Projects for the Evil Genius HEIRS Schematic diagram for Project 30, (GRENIER © powerea-up Arduino board with LED it Chapter 9 include int segmentPins{] = (3, 2, 19, 16, 18, 4 5, 17); int displayPins{] = (14, 7, 18, 6): int times{] = (5, 10, 15, 20, 30, 45, 100, 130, 200, 700, 800, 900, 1000, 1800, 2000, 3000); int numPimes = 19; byte selectedrimerndex; int timerMinute; int timersecond; int buzzerPin = 11; int ain = 8; int bPin = 10; int buttonPin boolean stopped = crue, byte digits(10}(8) = ¢ Wave dota CL db ido 0, “0 (0, 2, 4, 9, 0, 0, 0, 0), M4 C44, 0,4, 2,0, 2,0), 2 C4 1 1, 0, 0, 1,0), 3 (0,2, 2,9, 0,1, 3,0, “4 4,0 1,1, 0,1, 1,0, 75 (4,04, 1,1, 1, 4,0), 1 6 C2, 4, 2,9, 0, 0, 0, 0, 17 CLiuiad io, ve (Lib ott 0 8 » void setup() ‘ for (int iso; i < 8; it) ‘ pinkode(segnentPins [i], OUTPUT) ; : for (int isd; i < 4; int « pinMode(displayPins|i], OUPPUT) ; > pinMode(buzzerPin, OUTPUT) ; pinYode(buttonPin, INPUT} ; pinYode(aPin, INPUT); pinMode(bPin, INPUT) selectedTimetadex = B5PROM, read (0) ; UimerMinute = times[selected?inerndex] / 100; timerSecond = tines[selectedzimeindex] * 100; Miscellaneous Projects 165 230, 300, 400, 509, 600, (continued) 166 30 Arduino Projects for the Evil Genius > void loop() ‘ Af (@igitalRead (buttonPin) ) ‘ stopped = | storped; digitelwrive(ouzzerPin, LON) ; waile (digitalRead(buttonPia}} QO; BEPROM.write(0, selectedrimerndex) ; ) updatebisplay(); void updateDisplay() // mss ‘ int mingecs = timerMinute * 100 + timersecons int v = minsecs; for (int i= 0; i < 4; doe) ‘ int digit = v $ 10; setDigit (i); setSegnents (digit) ; vev/ 10; process (); ) serbigit (5); // all digits off to prevent uneven illuminacion void process () ‘ for (int i = 0; i < 100; i++) // tweak this number between flicker and blur ‘ int change = getEncoderTurn(); if (stopped) « changesetTime (change) ; ) else ‘ updateCounting?ime() ; > Af (timerminute ‘ digitalwrite(buzzerPin, HIGH) ; © && timersecond void changesetTine(int change] Chapter 9 = Miscellaneous Projects 167 ‘ selected?imelndex += change; if (selectearimemdex < 0) ‘ selected?imetndex = nun?ines ? else if (selectedTimeindex > nunimes) ‘ selected?inetndex = 0; > eimeztinute tines (selectedPinetndex] / 100; eimezSecond = times[selected?imelndex] ¥ 100 void updatecountingTime() ‘ static unsigned long lastMillis; unsigned long m = millis(}; Af (m > (lastMillis + 1000) 4& (timersecond > 0 || timerMinute > 0)) ‘ Aigitalirite(buzzerPin, HIGH); delay (10); Gigitalwrive(buzzerPin, LOW) ; if (imersecond « ° timersecond = 59, timerinute —; > else « timersecond -; > lastuillis void setDigit (int digit) ‘ for (int i = 0; i < 4; ise « Gigitalwrite(displayPins(i], (digit != in); void setSegnents(int n) t for (int i = 0; i < 8; ise) « Gigitalmrive(segmentPins(iJ, | digits(n] (il); (continued) 168 30 Arduino Projects for the Evil Genius LISTING PROJECT 30 (continued) > ing getsneoderturn () ‘ “ no-1, 0, or 41 int 014A = LOW int 016B = Low; vesult = 0; Int newa, digitalRead (api) ; int new8 = digitalRead(pPin) if (newA != 018A || news ‘ // something has changed Lf (014A == LOW && newa olde) RICH) result ae*2- 1; } olda = newa, ola = news, return result; ‘The timer will always be in one of two states. It will cither be stopped, in which case, turning the rotary encoder will change the time, or it can be running, in which case it will be counting down. Pressing the button on the rotary encoder will toggle between the two states. Rather than make the rotary encoder change the time one second per rotation step, we have an array of standard times that fit with the egg- cooking habits of the Evil Genius. This array can be edited and extended, but if you change its length, you must alter the num'Times variable accordingly The EEPROM library is used to store the last used time so that each time the project is powered up, it will remember the last time used, The project makes a little chirp as each second ticks by. You may wish to disable this. You will find the relevant lines of code to comment out or delete this in the updateCountTime function. Putting It All Together Load the completed sketch for Project 30 from ‘your Arduino Sketchbook and download it to the board (see Chapter 1). Summary This is the final chapter containing projects. The author hopes that in trying the projects in this, book, the Evil Genius’ appetite for experimentation and design has been stirred and they will have the urge to design some projects of their own, ‘The next chapter sets out to help you in the process of developing your own projects

Вам также может понравиться