Turns the micro:bit into a magic 8-ball
main.cpp@0:82f9c5a8891c, 2017-09-16 (annotated)
- Committer:
- martyStone
- Date:
- Sat Sep 16 00:34:34 2017 +0000
- Revision:
- 0:82f9c5a8891c
Sample magic 8 ball program
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
martyStone | 0:82f9c5a8891c | 1 | /* |
martyStone | 0:82f9c5a8891c | 2 | The MIT License (MIT) |
martyStone | 0:82f9c5a8891c | 3 | |
martyStone | 0:82f9c5a8891c | 4 | Copyright (c) 2016 British Broadcasting Corporation. |
martyStone | 0:82f9c5a8891c | 5 | This software is provided by Lancaster University by arrangement with the BBC. |
martyStone | 0:82f9c5a8891c | 6 | |
martyStone | 0:82f9c5a8891c | 7 | Permission is hereby granted, free of charge, to any person obtaining a |
martyStone | 0:82f9c5a8891c | 8 | copy of this software and associated documentation files (the "Software"), |
martyStone | 0:82f9c5a8891c | 9 | to deal in the Software without restriction, including without limitation |
martyStone | 0:82f9c5a8891c | 10 | the rights to use, copy, modify, merge, publish, distribute, sublicense, |
martyStone | 0:82f9c5a8891c | 11 | and/or sell copies of the Software, and to permit persons to whom the |
martyStone | 0:82f9c5a8891c | 12 | Software is furnished to do so, subject to the following conditions: |
martyStone | 0:82f9c5a8891c | 13 | |
martyStone | 0:82f9c5a8891c | 14 | The above copyright notice and this permission notice shall be included in |
martyStone | 0:82f9c5a8891c | 15 | all copies or substantial portions of the Software. |
martyStone | 0:82f9c5a8891c | 16 | |
martyStone | 0:82f9c5a8891c | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
martyStone | 0:82f9c5a8891c | 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
martyStone | 0:82f9c5a8891c | 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
martyStone | 0:82f9c5a8891c | 20 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
martyStone | 0:82f9c5a8891c | 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
martyStone | 0:82f9c5a8891c | 22 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
martyStone | 0:82f9c5a8891c | 23 | DEALINGS IN THE SOFTWARE. |
martyStone | 0:82f9c5a8891c | 24 | */ |
martyStone | 0:82f9c5a8891c | 25 | |
martyStone | 0:82f9c5a8891c | 26 | #include "MicroBit.h" |
martyStone | 0:82f9c5a8891c | 27 | |
martyStone | 0:82f9c5a8891c | 28 | MicroBit uBit; |
martyStone | 0:82f9c5a8891c | 29 | |
martyStone | 0:82f9c5a8891c | 30 | const char Answers[9][27] = {"It is certain;", "It is decidedly so;", "Cannot predict now;", |
martyStone | 0:82f9c5a8891c | 31 | "Concentrate and ask again;", "Don't count on it;", "My reply is no;", |
martyStone | 0:82f9c5a8891c | 32 | "My sources say no;", "Outlook not so good;", "Very doubtful;"}; |
martyStone | 0:82f9c5a8891c | 33 | // |
martyStone | 0:82f9c5a8891c | 34 | // |
martyStone | 0:82f9c5a8891c | 35 | // |
martyStone | 0:82f9c5a8891c | 36 | int checkForShake() { |
martyStone | 0:82f9c5a8891c | 37 | uint32_t accVal = 0; |
martyStone | 0:82f9c5a8891c | 38 | int x = (uBit.accelerometer.getX()); |
martyStone | 0:82f9c5a8891c | 39 | int y = (uBit.accelerometer.getY()); |
martyStone | 0:82f9c5a8891c | 40 | accVal = (abs(x) + abs(y)); |
martyStone | 0:82f9c5a8891c | 41 | return accVal; |
martyStone | 0:82f9c5a8891c | 42 | } |
martyStone | 0:82f9c5a8891c | 43 | // |
martyStone | 0:82f9c5a8891c | 44 | // |
martyStone | 0:82f9c5a8891c | 45 | int main() |
martyStone | 0:82f9c5a8891c | 46 | { |
martyStone | 0:82f9c5a8891c | 47 | // Initialise the micro:bit runtime. |
martyStone | 0:82f9c5a8891c | 48 | uBit.init(); |
martyStone | 0:82f9c5a8891c | 49 | uBit.seedRandom(); |
martyStone | 0:82f9c5a8891c | 50 | while(1) { |
martyStone | 0:82f9c5a8891c | 51 | uBit.display.print('8'); |
martyStone | 0:82f9c5a8891c | 52 | if (checkForShake() >= 2500) { // (uBit.accelerometer.getGesture()== SHAKE) |
martyStone | 0:82f9c5a8891c | 53 | char response[27] = ""; |
martyStone | 0:82f9c5a8891c | 54 | int idx1 = uBit.random(9); |
martyStone | 0:82f9c5a8891c | 55 | int idx2 = 0; |
martyStone | 0:82f9c5a8891c | 56 | while(Answers[idx1][idx2] != ';') // terminates on ; |
martyStone | 0:82f9c5a8891c | 57 | { |
martyStone | 0:82f9c5a8891c | 58 | response[idx2] = Answers[idx1][idx2]; |
martyStone | 0:82f9c5a8891c | 59 | idx2++; |
martyStone | 0:82f9c5a8891c | 60 | } |
martyStone | 0:82f9c5a8891c | 61 | uBit.display.scroll(response); |
martyStone | 0:82f9c5a8891c | 62 | uBit.sleep(1000);}} |
martyStone | 0:82f9c5a8891c | 63 | } |
martyStone | 0:82f9c5a8891c | 64 | // |