Martin Stone / microbit_8ball

Dependencies:   microbit

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 The MIT License (MIT)
00003 
00004 Copyright (c) 2016 British Broadcasting Corporation.
00005 This software is provided by Lancaster University by arrangement with the BBC.
00006 
00007 Permission is hereby granted, free of charge, to any person obtaining a
00008 copy of this software and associated documentation files (the "Software"),
00009 to deal in the Software without restriction, including without limitation
00010 the rights to use, copy, modify, merge, publish, distribute, sublicense,
00011 and/or sell copies of the Software, and to permit persons to whom the
00012 Software is furnished to do so, subject to the following conditions:
00013 
00014 The above copyright notice and this permission notice shall be included in
00015 all copies or substantial portions of the Software.
00016 
00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00020 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00021 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00022 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00023 DEALINGS IN THE SOFTWARE.
00024 */
00025 
00026 #include "MicroBit.h"
00027 
00028 MicroBit uBit;
00029 
00030 const char Answers[9][27] = {"It is certain;", "It is decidedly so;",  "Cannot predict now;", 
00031                              "Concentrate and ask again;", "Don't count on it;",  "My reply is no;", 
00032                              "My sources say no;", "Outlook not so good;", "Very doubtful;"};
00033 //
00034 //
00035 //
00036 int checkForShake() {
00037      uint32_t accVal = 0;
00038      int x = (uBit.accelerometer.getX());
00039      int y = (uBit.accelerometer.getY());
00040      accVal = (abs(x) + abs(y));  
00041      return accVal;
00042 }
00043 //
00044 //
00045 int main()
00046 {
00047     // Initialise the micro:bit runtime.
00048     uBit.init();
00049     uBit.seedRandom(); 
00050    while(1) {
00051      uBit.display.print('8');
00052      if (checkForShake() >= 2500) {                //   (uBit.accelerometer.getGesture()== SHAKE)
00053        char response[27] = ""; 
00054        int idx1 = uBit.random(9);
00055        int idx2 = 0;
00056        while(Answers[idx1][idx2] != ';')           //   terminates on ;
00057           {
00058           response[idx2] = Answers[idx1][idx2];
00059           idx2++;
00060           }
00061         uBit.display.scroll(response);
00062         uBit.sleep(1000);}}
00063 }
00064 //