@@ -192,7 +192,24 @@ Blockly.Arduino['ultrasonic_sensor'] = function(block) {
192192 Blockly . Arduino . setups_ [ 'setup_output_' + PIN_TRIG ] = 'pinMode(' + PIN_TRIG + ', OUTPUT);' ;
193193 Blockly . Arduino . setups_ [ 'setup_input_' + PIN_ECHO ] = 'pinMode(' + PIN_ECHO + ', INPUT);' ;
194194
195- Blockly . Arduino . definitions_ [ 'var_ultrasonic' + PIN_TRIG ] = 'long ultrasound_distance_' + us_number + '() {\n' +
195+
196+ if ( ( PIN_TRIG == "4" ) && ( PIN_ECHO == "5" ) )
197+ {
198+ Blockly . Arduino . definitions_ [ 'var_ultrasonic' + PIN_TRIG ] = 'long ultrasound_distance_' + us_number + '() {\n' +
199+ ' long duration, distance;\n' +
200+ ' digitalWrite(' + PIN_TRIG + ',LOW);\n' +
201+ ' delayMicroseconds(100);\n' +
202+ ' digitalWrite(' + PIN_TRIG + ', HIGH);\n' +
203+ ' delayMicroseconds(100);\n' +
204+ ' digitalWrite(' + PIN_TRIG + ', LOW);\n' +
205+ ' duration = pulseIn(' + PIN_ECHO + ', HIGH);\n' +
206+ ' distance = duration/55;\n' +
207+ ' return distance;\n' +
208+ '}\n' ;
209+ }
210+ else
211+ {
212+ Blockly . Arduino . definitions_ [ 'var_ultrasonic' + PIN_TRIG ] = 'long ultrasound_distance_' + us_number + '() {\n' +
196213 ' long duration, distance;\n' +
197214 ' digitalWrite(' + PIN_TRIG + ',LOW);\n' +
198215 ' delayMicroseconds(2);\n' +
@@ -202,7 +219,8 @@ Blockly.Arduino['ultrasonic_sensor'] = function(block) {
202219 ' duration = pulseIn(' + PIN_ECHO + ', HIGH);\n' +
203220 ' distance = duration/58;\n' +
204221 ' return distance;\n' +
205- '}\n' ;
222+ '}\n' ;
223+ }
206224
207225 var code = '' ;
208226 return code ;
0 commit comments