🗼 BLACKPOOL 2024   🚚 FREE FEDEX SHIPPING from 197 USD   🌎 WORLDWIDE SHIPPING for 25 USD

AtomDrum · MagiScript

Just for Fun

AtomDrum is a fun project but demonstrates several features of Atom in one project, like how you can connect to PeekSmith and SB Watch, and how you can work with buttons.

After uploading this code to Atom, it will connect to your PeekSmith and SB Watch, just make sure you change the device names to your devices in lines 2 and 3.

It will print "AtomDrum" on PeekSmith, announce that Atom Drum is ready (your browser will speak on your machine), and sets the SB Watch to 12:00.

Then, on button press events coming from PeekSmith, SB Watch (the crown button), or Atom (the top three buttons), it will play a drum sound.

function main() {
    ps.connect('PeekSmith-031060');
    sbwatch.connect('SBWatch-155309');
    
    ps.print('AtomDrum');
    events.send('Atom drum is ready.', 'voice');
    sbwatch.setTime('12:00');
}

function onButtonPress(source, buttonId) {
    console.log(source, buttonId);
    if (buttonId === 0) {
        events.send('tom1', 'sound');
    }
    if (buttonId === 1) {
        events.send('tom2', 'sound');
    }
    if (buttonId === 2) {
        events.send('tom3', 'sound');
    }
}

function onEvent(e) {
    if (e.type === 'press') {
        onButtonPress(e.source, parseInt(e.value));
    }
}

Tips and Tricks

  • the main function will run when you upload the code
  • you should not write code outside functions, except for the variable declaration
  • connection to PeekSmith and SB Watch will happen in the background, so it may take a few seconds while Atom connects - that said, once connected, it will ignore connection requests

Challenges

  • Modify the code to print the buttonId (0-11) to PeekSmith's screen when you press the button.
  • Add setting the hour on SB Watch to the button pressed. You should use the sbwatch.setTime(12, 0); call, but replace 12 with the hour you would like to set.
cross