Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions blockly/blocks/arduino/sensor.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,77 @@ Blockly.Blocks['DHT_readRH'] = {
this.setWarningText(Blockly.Msg.ARD_COMPONENT_WARN1.replace('%1', Blockly.Msg.ARD_DHT_COMPONENT).replace('%2', Blockly.Msg.ARD_DHT_COMPONENT));
}
}
};

Blockly.Blocks['sonic_config_hub'] = {
/**
* Block for adding a Sonic sensor.
* @this Blockly.Block
*/
init: function() {
this.appendDummyInput()
.appendField(new Blockly.FieldImage("media/arduino/hc-sr04.png", 60, 40, "*"))
.appendField(Blockly.Msg.ARD_SONICHUB)
.appendField(
new Blockly.FieldInstance('Supersonic',
Blockly.Msg.ARD_SONIC_DEFAULT_NAME,
true, true, false),
'NAMESONIC')
this.appendDummyInput()
.appendField('Echo pin:')
.setAlign(Blockly.ALIGN_CENTRE)
.appendField(new Blockly.FieldDropdown(
Blockly.Arduino.Boards.selected.digitalPins), 'ECHO')
.appendField('Trigger pin:')
.setAlign(Blockly.ALIGN_CENTRE)
.appendField(new Blockly.FieldDropdown(
Blockly.Arduino.Boards.selected.digitalPins), 'TRIGGER')
this.appendValueInput('SONIC_TIME')
.appendField(Blockly.Msg.ARD_SONIC_TEXT_TIME)
.setCheck(Blockly.Types.NUMBER.checkList)
.setAlign(Blockly.ALIGN_RIGHT);
this.setColour(Blockly.Blocks.sensor.HUE);
this.setTooltip(Blockly.Msg.ARD_DHTHUB_TIP);
this.setHelpUrl('');
},
/**
* Set the connection pins that the component connects to
* @param {array} array of the connections (as string, eg '1', 'SDA', 'A1', ...
* @this Blockly.Block
*/
setHubConnector: function(connector) {
this['connector'] = connector;
},
/**
* Gets the variable type required.
* @param {!string} varName Name of the variable selected in this block to
* check.
* @return {string} String to indicate the variable type.
*/
getVarType: function(varName) {
return Blockly.Types.NUMBER;
}
};

Blockly.Blocks['sonic_read'] = {
/**
* Block for reading the distance of a supersonic distance sensor.
* @this Blockly.Block
*/
init: function() {
this.setHelpUrl('');
this.setColour(Blockly.Blocks.sensor.HUE);
this.appendDummyInput()
.appendField(Blockly.Msg.ARD_SONIC_READ)
.appendField(
new Blockly.FieldInstance('Supersonic',
Blockly.Msg.ARD_SONIC_DEFAULT_NAME,
false, true, false), 'NAMESONIC');
this.setOutput(true, Blockly.Types.NUMBER.output);
this.setTooltip(Blockly.Msg.ARD_SERVO_READ_TIP);
},
/** @return {string} The type of return value for the block, an integer. */
getBlockType: function() {
return Blockly.Types.NUMBER;
}
};
57 changes: 57 additions & 0 deletions blockly/generators/arduino/sensor.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,60 @@ Blockly.Arduino['DHT_readRH'] = function(block) {
var code = dhtName + '_readRH()'
return [code, Blockly.Arduino.ORDER_ATOMIC];
};

/**
* The Sonic Sensor HC-SR04 setup block
* @param {!Blockly.Block} block Block to generate the code from.
* @return {string} Completed code.
*/
Blockly.Arduino['sonic_config_hub'] = function(block) {
var sonicName = block.getFieldValue('NAMESONIC');
var sonicTime = Blockly.Arduino.valueToCode(block, 'SONIC_TIME',
Blockly.Arduino.ORDER_ATOMIC) || '0';
var sonicDistance = 0;

Blockly.Arduino.addVariable(sonicName + '_Echo',
'int ' + sonicName + '_Echo = ' + block.getFieldValue('ECHO')
+ ';\nint ' + sonicName + '_Trigger = ' +
block.getFieldValue('TRIGGER') + ';\nint ' +
sonicName + '_Distance = ' + sonicDistance + ';\nint ' +
sonicName + '_Time = ' + sonicTime + ';\nlong ' +
sonicName + '_Duration;', true);
Blockly.Arduino.reservePin(block, block.getFieldValue('ECHO'), Blockly.Arduino.PinTypes.INPUT, 'SONIC ECHO');
Blockly.Arduino.reservePin(block, block.getFieldValue('TRIGGER'), Blockly.Arduino.PinTypes.OUTPUT, 'SONIC TRIGGER');
var pinSetupCodeEcho = 'pinMode(' + sonicName + '_Echo, INPUT);';
var pinSetupCodeTrigger = 'pinMode(' + sonicName +
'_Trigger, OUTPUT);';
Blockly.Arduino.addSetup('io_' + sonicName + '_Echo', pinSetupCodeEcho, false);
Blockly.Arduino.addSetup('io_' + sonicName + '_Trigger', pinSetupCodeTrigger, false);
return '';
};

/**
* Code generator to read the distance from a sonic distance sensor.
* @param {!Blockly.Block} block Block to generate the code from.
* @return {array} Completed code with order of operation.
*/
Blockly.Arduino['sonic_read'] = function(block) {
var sonicName = block.getFieldValue('NAMESONIC');

/**
* Method to read the distance
* First delay is to clear the Trigger pin in case Trigger is not LOW
*/
var methodCode = `float SONICNAME_readDistance() {
digitalWrite(SONICNAME_Trigger, LOW);
delayMicroseconds(2);
digitalWrite(SONICNAME_Trigger, HIGH);
delayMicroseconds(10);
SONICNAME_Duration = pulseIn(SONICNAME_Echo, HIGH, SONICNAME_Time);
SONICNAME_Distance = 0.034/2 * SONICNAME_Duration;
return SONICNAME_Distance;
}
`
Blockly.Arduino.addFunction(sonicName + '_read', methodCode
.replace(new RegExp('SONICNAME', 'g'), sonicName));

var returnCode = sonicName + '_readDistance()';
return [returnCode, Blockly.Arduino.ORDER_ATOMIC];
};
16 changes: 12 additions & 4 deletions blockly/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@

<link rel="shortcut icon" href="blockly4Arduino/favicon3.png"/>
<!-- Blockly4Arduino - These three files contain the compress version-->
<script src="blockly_compressed.js"></script>
<!--<script src="blockly_compressed.js"></script>
<script src="blocks_compressed.js"></script>
<script src="arduino_compressed.js"></script>
<script src="arduino_compressed.js"></script>-->
<!-- To use the uncompressed version comment out the above and comment in the ones below -->
<!--script src="blockly_uncompressed.js"></script>
<script src="blockly_uncompressed.js"></script>
<script src="../closure-library/closure/goog/base.js"></script>
<script src="blocks/logic.js"></script>
<script src="blocks/loops.js"></script>
Expand Down Expand Up @@ -93,7 +93,7 @@
<script src="generators/arduino/md_actuator.js"></script>
<script src="generators/arduino/md_audio.js"></script>
<script src="generators/arduino/md_light.js"></script>
<script src="generators/arduino/ledup_blocks.js"></script-->
<script src="generators/arduino/ledup_blocks.js"></script>

<!-- Default language js files, user selection appended to head on load -->
<script type="text/javascript" src="msg/js/nl.js"></script>
Expand Down Expand Up @@ -1077,6 +1077,14 @@
<block type="DHT_config_hub"></block>
<block type="DHT_readTemp"></block>
<block type="DHT_readRH"></block>
<block type="sonic_config_hub">
<value name="SONIC_TIME">
<block type="math_number">
<field name="NUM">2000</field>
</block>
</value>
</block>
<block type="sonic_read"></block>
</category>
</category>
</xml>;
Expand Down
7 changes: 7 additions & 0 deletions blockly/index_en.html
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,13 @@
<block type="DHT_config_hub"></block>
<block type="DHT_readTemp"></block>
<block type="DHT_readRH"></block>
<block type="sonic_config_hub">
<value name="SONIC_TIME">
<block type="math_number">
<field name="NUM">2000</field>
</block>
</value>
</block>
</category>
</category>
</xml>;
Expand Down
Binary file added blockly/media/arduino/hc-sr04.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions blockly/msg/js/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,10 @@ Blockly.Msg.ARD_SERVO_WRITE_DEG_180 = "Degrees (0~180)";
Blockly.Msg.ARD_SERVO_WRITE_TIP = "Set a Servo to an specified angle";
Blockly.Msg.ARD_SERVO_WRITE_TO = "to";
Blockly.Msg.ARD_SETTONE = "Set tone on pin #";
Blockly.Msg.ARD_SONIC_DEFAULT_NAME = "SupersonicDistanceSensor1";
Blockly.Msg.ARD_SONICHUB = "Supersonic distance sensor";
Blockly.Msg.ARD_SONIC_TEXT_TIME = "Maximum waiting time (microseconds)";
Blockly.Msg.ARD_SONIC_READ = "read distance sensor (cm)";
Blockly.Msg.ARD_SPI_SETUP = "Setup";
Blockly.Msg.ARD_SPI_SETUP_CONF = "configuration:";
Blockly.Msg.ARD_SPI_SETUP_DIVIDE = "clock divide";
Expand Down
4 changes: 4 additions & 0 deletions blockly/msg/js/nl.js
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,10 @@ Blockly.Msg.ARD_SERVO_WRITE_DEG_180 = "graden (0~180)";
Blockly.Msg.ARD_SERVO_WRITE_TIP = "Positioneer een Servo motor naar de gegeven hoek";
Blockly.Msg.ARD_SERVO_WRITE_TO = "naar";
Blockly.Msg.ARD_SETTONE = "Speel noot op pin#";
Blockly.Msg.ARD_SONIC_DEFAULT_NAME = "UltrasoneAfstandssensor1";
Blockly.Msg.ARD_SONICHUB = "Ultrasone Afstandssensor";
Blockly.Msg.ARD_SONIC_TEXT_TIME = "Maximale wachttijd (microseconden)";
Blockly.Msg.ARD_SONIC_READ = "lees afstandssensor (cm)";
Blockly.Msg.ARD_SPI_SETUP = "Definieer";
Blockly.Msg.ARD_SPI_SETUP_CONF = "configuratie:";
Blockly.Msg.ARD_SPI_SETUP_DIVIDE = "klok divisie";
Expand Down