-
I need some help writing a save() block in blockly. This code moves the servo on pin B14 to two positions 0.5 seconds apart then saves it to the microcontroller so the code executes next time I plug it in. This code works when I write it in the JavaScript window:
setTimeout(function () {analogWrite(B14, 1.8 / 50.0, {freq:20});},500); setTimeout(function () {analogWrite(B14, 1.6 / 50.0, {freq:20});},1000); save();
Similarly, this code works to call the first two lines from a Blockly block, so I know I'm doing all of that correctly:
Blockly.Language.save = { category: 'Espruino', init: function() { this.appendDummyInput() .appendTitle('Save'); this.setPreviousStatement(true); this.setNextStatement(true); this.setColour(100); this.setInputsInline(true); this.setTooltip('Save Code to Board'); } }; Blockly.JavaScript.save = function() { var move = "setTimeout(function () {analogWrite(B14, 1.8 / 50.0, {freq:20});},500);\n"; var move2 = "setTimeout(function () {analogWrite(B14, 1.6 / 50.0, {freq:20});},1000);\n"; var saveCmd = "save();\n"; return move + move2; };
But as soon as I call the save() command from Blockly by changing line 18 to:
return move + move2 + saveCmd;
then the block does nothing. Meaning, the servo doesn't move, and there's no indication that anything was sent to the board. The frustrating part is that when I click on the command prompt side of the IDE and press up to see the commands sent, I see all three of the commands that I wanted to see:
setTimeout(function () {analogWrite(B14, 1.8 / 50.0, {freq:20});},500); setTimeout(function () {analogWrite(B14, 1.6 / 50.0, {freq:20});},1000); save();
Please help,
Rehman -
-
I've downloaded the source for the web IDE and I would like to customize a few things. Specifically, I'm writing new Blockly blocks, so I would like to be able to:
- Default to Blockly view as opposed to JavaScript
- Have the blockly portion take up most or all of the screen by default.
- I've added my new blocks to new list items like "Variables" or "Espruino". Is it possible to
(a) get rid of these lists all together and just show all of the blocks available?, or
(b) display only a subset of the list, then do something in the IDE to reveal more list items? The idea is to have a teacher "unlock" new blocks as a student passes a level with a subset of blocks.
Thanks,
Rehman - Default to Blockly view as opposed to JavaScript
-
Hi, I would like to group several Blockly blocks into a function, then have the ability to call that function by simply calling a single block. I've seen that functionality on other Blockly sites, so I'm curious to know why it isn't part of the Espruino Chrome extension.
Thanks,
Rehman -
-
-
Hi,
I'm writing some Blockly Blocks and would like to alert the user when they do something bad (e.g. try to move a servo beyond its limits). Can I have the code generate a pop up when this happens? Something like
window.alert()
or
window.confirm()
?
Also, how do I accept text input on the terminal side of the Chrome IDE? I know I can input JavaScript functions there, but what about text strings? If not possible, then how do I write my own function that will be recognized there, so the user can type "hello" and it executes the function
hello()
Finally, is there any way to accept a mouse input? e.g.
if(LeftMouseClick) turnOnLED(1);
Thanks,
Rehman -
Ah, that works well. Thanks DrAzzy.
Another quick questions is: how do you change the default Blockly code that appears when you launch the Chrome extension? The JavaScript seems to remember the last code I wrote, but the Blockly code is always the default. Where can I edit this default Blockly code?Thanks.
-
Thanks guys. I tried following your advice to not use blocking code, but ran into another problem. I can't change that value of variables inside the function prototype. For example, this code works:
setTimeout(function () {analogWrite(B14, 1.6 / 50.0, {freq:20});},1000); setTimeout(function () {analogWrite(B14, 1.55 / 50.0, {freq:20});},2000); setTimeout(function () {analogWrite(B14, 1.5 / 50.0, {freq:20});},3000); setTimeout(function () {analogWrite(B14, 1.45 / 50.0, {freq:20});},4000);
But this code does not:
var pos = 1.6; var posInc = 0.05; setTimeout(function () {analogWrite(B14, pos / 50.0, {freq:20});},1000); pos -= posInc; setTimeout(function () {analogWrite(B14, pos / 50.0, {freq:20});},2000); pos -= posInc; setTimeout(function () {analogWrite(B14, pos / 50.0, {freq:20});},3000); pos -= posInc; setTimeout(function () {analogWrite(B14, pos / 50.0, {freq:20});},4000);
The result is that it moves to position 1.45 at time 1000. So I guess it has already run "pos -= posInc;" three times before it first calls the analogWrite() function.
Please help,
Rehman -
Hi, I'm creating my own Blocks in Blockly by editing blockly_espruino.js. I can create global variables at the top of the file like
var ESPRUINO_COL = 190;
that are accessible in functions like
Blockly.Language.espruinoXXX = {...};
but these variables cannot be accessed in functions like
Blockly.JavaScript.espruinoXXX = function() {...};
How do I create global variables that I can then edit inside the JavaScript that defines what the block does?
Thanks,
Rehman -
-
-
-
Thanks Ducky. I can't count on my users having a device that supports OTG. But the BlueTerm app is good. I'm thinking the easiest solution may be to figure out how to run the existing Espruino Chrome extension on a mobile. Even if that means wrapping the extension as an app; if that's even possible.
Regards,
Rehman -
Hi,
One feature that I miss from BlocklyDuino in switching to Espruino is the ability to auto-generate code using Blockly. It's useful in teaching programming (and debugging) to toggle between the Blockly screen and the code screen (JavaScript in Espruino, Arduino in BlocklyDuino), and see the updated code from the Blockly changes.
Is it asking a lot to see this in a future update of the Espruino Chrome Extension?Thanks,
Rehman -
Hi,
I'm enjoying using the Chrome Extension for Espruino, but I just learned that you can't use extensions on Android. Are there any solutions for this? I guess if I can write to serial port, then I can program an Espruino board, right? I'd hate to write an app; is it possible to write a web interface instead? Ideally, users could go to my website and send signals to the Espruino. Even wirelessly over bluetooth?
Thanks,
Rehman -
-
Hmm, nested setTimeout() functions. That works too. But unfortunately it doesn't solve my problem :(
I'm actually trying to write easy Blockly Blocks to teach kids to program. So I want simple blocks like "Move Left", "Move Center", etc. I should be able to stack a bunch of these blocks in sequence, click run, and watch the servo go through the motions with a small delay between each.
I'm not sure how to write the blocks to achieve the nested functionality in your last post. But if nothing else, maybe I can write them such that the timeout value is larger for each block added. It's not a perfect solution because I think it will require an init() block at the top of each program.Thanks again,
Rehman -
Thanks Gordon. The up-arrow trick works well to see the code sent by Blockly. I'm not sure how to open the 'inspect element' window.
You're also right about the two calls happening at the same time. I read up on setTimeout() and see that it actually forks a new process that is executed at the specified time. But it is non-blocking. Is there a way to block? I tried writing a sleep() function, but that's not working either (see below). I also read that there's a way to get setTimeout() to work the way I want, but I can't get it to work.
Thanks,
Rehmanfunction sleep(milliseconds) { var start = getTime(); var i = 0; for (i = 0; i < 1e7; i++) { if ( ( getTime() - start) > milliseconds){ break; } } }
-
Thanks Gordon, that worked :)
I have two more questions though:
How do I see the JavaScript that the Blockly Code produces? I thought that toggling to the JavaScript view would update the code, but it does not.
Is there a delay() or sleep() function? I just want to move a servo, wait a second (for it to move), then move it again. I thought setTimeout() would work, but it does not. This line of code works:
setTimeout("analogWrite(C6, 2.0 / 50.0, {freq:20});",4000);
But if I put this line after it, the code no longer works (properly):
setTimeout("analogWrite(C6, 1.0 / 50.0, {freq:20});",4000);
I tried playing with clearTimeout() to no avail.
Thanks,
Rehman -
Hi,
I'm just trying to create my first block in Blockly for Espurino.
I'm running the Chrome extension offline and editing the file "blockly_espruino.js".
I made one function of the form "Blockly.Language.espruino_newFunction = {}" and one of the form "Blockly.JavaScript.espruino_newFunction = function() {}".
But how do I actually see my new block? Is there a setting or something I'm missing? If anyone could list the steps to create a new block, I would really appreciate it.Thanks,
Rehman
Just a clarification, where would you call
Espruino.Core.Code.switchToBlockly()
from?Also, the code in
js/core/app.js
has a TODO that you plan to load the splitster orientation from file. Just wondering if that has been implemented already.Thanks.