πŸ–¨οΈ COSMOS PRINTER
🚚 Shipping from 12 USD
πŸ’° DEALS OF THE DAY

Atom Pi (Pi Revelations) Β· MagiScript

David Penn's Pi Revelations

Pi Revelations is a stunning magic trick that will leave your spectators in awe. With the help of this trick, you can reveal your spectator’s PIN Number, Passcode, Date Of Birth, or Random Time in a unique and fascinating way – within the first fifty thousand decimals of Pi. The trick is sold with a book and a link to an indexing website, allowing you to create unbelievable revelations without the need for memory work.

BUY THE BOOK HERE: Pi Revelations by David Penn (worldmagicshop.com)

You will be able to enter the 4 digits on Atom, and it will reveal on PeekSmith where the digits are in the book (page, line number, and column). Atom stores the necessary logic to calculate this information, so we will have to query it.

See below the code for a Pi Revelations mini-app. It demonstrates how database queries will work, and also how you can implement entering numbers and doing calculations.

Please note, that the built-in Atom Time mini-app also supports displaying this information.

Thanks to Badasha Khan for helping me to add this feature.

How is it Working?

The main function runs when Atom loads your script (upload, or power on if you persisted the code). It connects to your PeekSmith.

The onEvent function is called when an event occurs. It displays “Atom Pi” on the PeekSmith when connected. For now, we are interested in button-press events from Atom. When an event like this happens, it transforms the value parameter to an integer number (it contains the button ID of Atom), and sends it to the onButtonPress function.

The onButtonPress function is processing the button ID. If it is a number button, it concatenates it to the number we are building. If it’s the button represents backspace, then it removes the last character if the num string is not empty. If it’s the bottom left button, then it just clears the number.

If the number has 4 digits, then it calculates the page information based on the number, and displays everything on the PeekSmith screen. Function showΒ responsible to display this information.

				
					
let num = '', lastNum = '';

function main() {
  ps.connect();
}

// Atom keyboard layout
const layout = [
  '1', '2', '3',
  '4', '5', '6',
  '7', '8', '9',
  'c', '0', '<'
];

function onButtonPress(buttonId) {
  const key = layout[buttonId];
  switch (key) {
    case 'c':
      num = '';
      lastNum = '';
      break;
    case '<':
      if (num === '' && lastNum !== '') num = lastNum;
      num = strSub(num, 0, -1);
      break;
    default:
      if (strLen(num) < 4) num += key;
      lastNum = '';
      break;
  }
  show();
}

function show() {
  let rest = '';
  for (let i = strLen(num); i < 4; i++) {
    rest += '_';
  }
  let text = `${num}${rest}\n`;
  if (strLen(num) === 4) {
    // query the information and display it
    const piInfo = db.query('pi', parseInt(num));
    text += `${piInfo.page} ${piInfo.line} ${piInfo.across}`;
    lastNum = num;
    num = '';
  }
  ps.print(text);
}

function onEvent(e) {
  if (e.source === 'ps:ble' && e.type === 'connected') {
    ps.print('Atom\nPi');
    setTimeout(show, 2000);
    return;
  }
  if (e.source === 'atom:button') {
    const buttonId = parseInt(e.value);
    if (e.type === 'click' || e.type === 'repeatpress') {
      return onButtonPress(buttonId);
    }
    if (strSub(e.type, 0, 5) === 'click' && strLen(e.type) === 6) {
      const clickCount = parseInt(strCharAt(e.type, 5));
      for (let i = 0; i < clickCount; i++) {
        onButtonPress(buttonId);
      }
      return;
    }
  }
}

				
			

Challanges

Modify the code to clear the screen (ps.print('')) if the number has no digits. It will save a lot of (PeekSmith 3) battery.

Don’t allow the code to add new digits if the number already has four digits.

General

πŸ“ƒ First Steps

MagiScript Editor

πŸ“ƒ Basics

πŸ“ƒ Keyboard Shortcuts

πŸ“ƒ Running Your First Program

πŸ“ƒ App Store

πŸ“ƒ Atom Settings (Editor)

πŸ“ƒ Debugging Techniques

Examples

πŸ“ƒ Atom Time

🎞️ Time Practice

πŸ“ƒ Atom Pi (Pi Revelations)

πŸ“ƒ Atom Drum

🎞️ Atom Stack

πŸ“ƒ Atom Square

πŸ“ƒ Atom Level

πŸ“ƒ Atom THMPR

πŸ“ƒ Poker Hands

πŸ“ƒ Keyboard Numeric

πŸ“ƒ Keyboard NOKIA

πŸ“ƒ Keyboard Cursor

πŸ“ƒ Keyboard Media

πŸ“ƒ Keyboard Custom

Input/Output

πŸ“ƒ Buttons

πŸ“ƒ Vibration Motor

πŸ“ƒ RGB LED

πŸ“ƒ Devices

πŸ“ƒ PeekSmith

πŸ“ƒ SB Watch

πŸ“ƒ Fossil Watch

πŸ“ƒ PeriPage Printer

πŸ“ƒ Cosmos Printer

πŸ“ƒ Teleport

πŸ“ƒ Bluetooth Keyboard

πŸ“ƒ Bluetooth Mouse

πŸ“ƒ Timers

πŸ“ƒ Database

πŸ“ƒ Events

πŸ“ƒ System (exit, sleep, rand)

πŸ“ƒ Objects (card, time)

Language

πŸ“ƒ Summary

πŸ“ƒ Comments

πŸ“ƒ Variable Declarations

πŸ“ƒ Operators

πŸ“ƒ Control Flow

πŸ“ƒ Functions

πŸ“ƒ Numbers

πŸ“ƒ Strings

πŸ“ƒ Arrays

πŸ“ƒ Objects

πŸ“ƒ Uint8Array