-
-
Thanks for your suggestions. I'll test them towards the evening and update the tutorial accordingly. If you want, I can add the updated version to Github directly, I just didn't know that you could post tutorials through Github. Regardless, it's probably best to suggest tutorials on the forum first, just to get this kind of useful feedback!
-
This is a fairly simple tutorial to demonstrate how you can use the button BTN1 to turn on one or more LEDs.
First, let's turn on LED1:
LED1.write(1);
Next, let us create a function that will alternate between turning LED1 on and off:
var next_state = 1; function swap() { LED1.write(next_state); next_state = !next_state; }
we can now simply type swap(); to turn the LED on when it was off or vice versa. The next step is to keep a watch for that button and whether the user pressed it. For this, we have the function setWatch. However, this will be triggered twice: once when the button is pressed down, and once when the user releases the button. We only want our LED to change when the user presses down the button, so we need another function:
function swap_on_down() { if (digitalRead(BTN1) == 1) swap(); }
This function is very easy, and will just verify whether BTN1 is pressed down. As such, it won't call the function swap when the user removes his/her finger from the button. This is exactly what we want! Now we can use the setWatch and let it call swap_on_down:
setWatch(swap_on_down, BTN1, true);
The last true option is provided so that this watch will keep on calling our function on successive presses on the button instead of only once. Try it, and you should see the red LED go on and off, each time you press the button.
Maybe even more fun is alternating the colours. The idea here is that, when we press the button, the red light comes on. When we press the button again, the green lights goes on. Next time, the blue goes on, after which another press of the button will turn the red light back on etc. As one added tough, we can add that all LEDS turn off after the user stopped pressing the button for 5 seconds.
// keep track of the next LED var next_LED = 1; // keep track of the ID, see later var timeout_ID = 0; function swap() { // remove the timeout to turn of all LEDs when the user pressed the button clearTimeout(timeout_ID); // determine which LED to turn on/off switch(next_LED) { case 1: LED1.write(1); LED3.write(0); break; case 2: LED2.write(1); LED1.write(0); break; case 3: LED3.write(1); LED2.write(0); break; } // determine the next LED to turn on next_LED = Math.wrap(next_LED, 3) + 1; // prepare a timeout to turn off all LEDs after a while // we capture the ID here, so that we can use it in a next call to this function timeout_ID = setTimeout(function () { LED1.write(0); LED2.write(0); LED3.write(0); }, 5000); }
As you will see, this works as expected but returns an ERROR: Unknown Timeout at some times (when all LEDs are off). Sadly, there is nothing to be done about that using this code, but you are always welcome to improve upon it :).
-
-
-
First: boards are here! Long wait, but well worth it!
Now, I suck at soldering. In fact, I soldered for the first time about 2 hours ago. While the pin headers are easy, getting those teensy bluetooth modules on was not.
hint 1: the 4 top spots are doable to solder, just let the solder drip in the slot. But then it is best to put something underneath them so that the solder will also flow down into the other 2 spots. If you don't do that ... well, it took me a while on my first try.
Happy with my result, and only 3 of my prototyping holes soldered shut, I turned on Bluetooth on my mac ... to find nothing. I did the precautionary thing of not connecting them to the USB of my mac, but instead to the USB of an external battery. But ... nothing.
hint 2: go to "Open Bluetooth Preferences" in the Bluetooth menu. You'll see 6 groups of 2 characters separated by dashes, which will quickly turn into a readable name. It is there that you need to fill in the pairing code of 1234. Phieuw, my soldering was decent.
And finally, for those of you with a decent level of security on their Macs, you may not be running as an Administrator account. Good for you and a pat on the back. Once again though, you might run into some issues.
hint 3: in the instructions found on http://www.espruino.com/Bluetooth you will notice that the sudo command will not work. Instead, first use su to get into an account with administrator privileges. If that user is called admin, then use su admin. Once in an administrator account, you can type in the sudo command.
Thanks. I'll delve into the code myself some day so that I can help out in fixing these issues, but some day so far seems to have been a moving target ;).
As for the fix, I assume you'll just need to use some threshold when converting floating points as you will never get a perfect mapping. I have some code that I wrote a while back for a similar issue, but I'll have to dig it up though ...