Fix ultrasonic sensor code

The Grove libraries don't have "RangeInInches" or "RangeInCentimeters" functions, and the Measure functions return the measurements directly, no setup required.
zeedee
Nick Johnson 7 years ago
parent cc30912a37
commit 078c1471f8
  1. 7
      blockly/arduino_compressed.js
  2. 856
      blockly/blockly_uncompressed.js
  3. 2
      blockly/blocks/grove.js
  4. 2
      blockly/blocks_compressed.js
  5. 8
      blockly/generators/arduino/grove.js

@ -30,10 +30,9 @@ Blockly.Arduino.grove_serial_lcd_power=function(){var a=this.getFieldValue("PIN"
Blockly.Arduino.grove_serial_lcd_effect=function(){var a=this.getFieldValue("PIN"),b=this.getFieldValue("STAT");Blockly.Arduino.definitions_.define_seriallcd="#include <SerialLCD.h>\n";Blockly.Arduino.definitions_.define_softwareserial="#include <SoftwareSerial.h>\n";var c=_get_next_pin(a);Blockly.Arduino.definitions_["var_lcd"+a]="SerialLCD slcd_"+a+"("+a+","+c+");\n";a="slcd_"+a;return"LEFT"===b?a+".scrollDisplayLeft();\n":"RIGHT"===b?a+".scrollDisplayRight();\n":a+".autoscroll();\n"};
Blockly.Arduino.grove_sound_sensor=function(){return["analogRead("+this.getFieldValue("PIN")+")",Blockly.Arduino.ORDER_ATOMIC]};Blockly.Arduino.grove_pir_motion_sensor=function(){var a=this.getFieldValue("PIN");Blockly.Arduino.setups_["setup_input_"+a]="pinMode("+a+", INPUT);";return["digitalRead("+a+")",Blockly.Arduino.ORDER_ATOMIC]};
Blockly.Arduino.grove_line_finder=function(){var a=this.getFieldValue("PIN");Blockly.Arduino.setups_["setup_input_"+a]="pinMode("+a+", INPUT);";return["digitalRead("+a+")",Blockly.Arduino.ORDER_ATOMIC]};
Blockly.Arduino.grove_ultrasonic_ranger=function(){var a=this.getFieldValue("PIN"),b=this.getFieldValue("UNIT");Blockly.Arduino.definitions_.define_ultrasonic="#include <Ultrasonic.h>\n";Blockly.Arduino.definitions_["var_ultrasonic"+a]="Ultrasonic ultrasonic_"+a+"("+a+");\n";"cm"===b?(Blockly.Arduino.setups_["setup_ultrasonic_"+a]="ultrasonic_"+a+".MeasureInCentimeters();",a="ultrasonic_"+a+".RangeInCentimeters();"):(Blockly.Arduino.setups_["setup_ultrasonic_"+a]="ultrasonic_"+a+".MeasureInInches();",
a="ultrasonic_"+a+".RangeInInches();");return[a,Blockly.Arduino.ORDER_ATOMIC]};
Blockly.Arduino.grove_motor_shield=function(){var a=this.getFieldValue("DIRECTION");Blockly.Arduino.setups_.setup_motor="pinMode(8,OUTPUT);//I1\n pinMode(11,OUTPUT);//I2\n pinMode(9,OUTPUT);//speedPinA\n pinMode(12,OUTPUT);//I3\n pinMode(13,OUTPUT);//i4\n pinMode(10,OUTPUT);//speedPinB\n";var b="";"forward"===a?(Blockly.Arduino.definitions_.define_forward="void forward()\n{\n analogWrite(9,127);//input a simulation value to set the speed\n analogWrite(10,127);\n digitalWrite(13,HIGH);//turn DC Motor B move clockwise\n digitalWrite(12,LOW);\n digitalWrite(11,LOW);//turn DC Motor A move anticlockwise\n digitalWrite(8,HIGH);\n}\n",b=
"forward();\n"):"right"===a?(Blockly.Arduino.definitions_.define_right="void right()\n{\n analogWrite(9,127);//input a simulation value to set the speed\n analogWrite(10,127);\n digitalWrite(13,LOW);//turn DC Motor B move anticlockwise\n digitalWrite(12,HIGH);\n digitalWrite(11,LOW);//turn DC Motor A move anticlockwise\n digitalWrite(8,HIGH);\n}\n\n",b="right();\n"):"left"===a?(Blockly.Arduino.definitions_.define_left="void left()\n{\n analogWrite(9,127);//input a simulation value to set the speed\n analogWrite(10,127);\n digitalWrite(13,HIGH);//turn DC Motor B move clockwise\n digitalWrite(12,LOW);\n digitalWrite(11,HIGH);//turn DC Motor A move clockwise\n digitalWrite(8,LOW);\n}\n\n",
Blockly.Arduino.grove_ultrasonic_ranger=function(){var a=this.getFieldValue("PIN"),b=this.getFieldValue("UNIT");Blockly.Arduino.definitions_.define_ultrasonic="#include <Ultrasonic.h>\n";Blockly.Arduino.definitions_["var_ultrasonic"+a]="Ultrasonic ultrasonic_"+a+"("+a+");";return["cm"===b?"ultrasonic_"+a+".MeasureInCentimeters()":"ultrasonic_"+a+".MeasureInInches()",Blockly.Arduino.ORDER_ATOMIC]};
Blockly.Arduino.grove_motor_shield=function(){var a=this.getFieldValue("DIRECTION");Blockly.Arduino.setups_.setup_motor="pinMode(8,OUTPUT);//I1\n pinMode(11,OUTPUT);//I2\n pinMode(9,OUTPUT);//speedPinA\n pinMode(12,OUTPUT);//I3\n pinMode(13,OUTPUT);//i4\n pinMode(10,OUTPUT);//speedPinB\n";var b="";"forward"===a?(Blockly.Arduino.definitions_.define_forward="void forward()\n{\n analogWrite(9,127);//input a simulation value to set the speed\n analogWrite(10,127);\n digitalWrite(13,HIGH);//turn DC Motor B move clockwise\n digitalWrite(12,LOW);\n digitalWrite(11,LOW);//turn DC Motor A move anticlockwise\n digitalWrite(8,HIGH);\n}\n",
b="forward();\n"):"right"===a?(Blockly.Arduino.definitions_.define_right="void right()\n{\n analogWrite(9,127);//input a simulation value to set the speed\n analogWrite(10,127);\n digitalWrite(13,LOW);//turn DC Motor B move anticlockwise\n digitalWrite(12,HIGH);\n digitalWrite(11,LOW);//turn DC Motor A move anticlockwise\n digitalWrite(8,HIGH);\n}\n\n",b="right();\n"):"left"===a?(Blockly.Arduino.definitions_.define_left="void left()\n{\n analogWrite(9,127);//input a simulation value to set the speed\n analogWrite(10,127);\n digitalWrite(13,HIGH);//turn DC Motor B move clockwise\n digitalWrite(12,LOW);\n digitalWrite(11,HIGH);//turn DC Motor A move clockwise\n digitalWrite(8,LOW);\n}\n\n",
b="left();\n"):"backward"===a?(Blockly.Arduino.definitions_.define_backward="void backward()\n{\n analogWrite(9,127);//input a simulation value to set the speed\n analogWrite(10,127);\n digitalWrite(13,LOW);//turn DC Motor B move anticlockwise\n digitalWrite(12,HIGH);\n digitalWrite(11,HIGH);//turn DC Motor A move clockwise\n digitalWrite(8,LOW);\n}\n\n",b="backward();\n"):"stop"===a&&(Blockly.Arduino.definitions_.define_stop="void stop()\n{\ndigitalWrite(9,LOW);// Unenble the pin, to stop the motor. this should be done to avid damaging the motor.\ndigitalWrite(10,LOW);\ndelay(1000);\n}\n\n",
b="stop();\n");return b};Blockly.Arduino.grove_thumb_joystick=function(){var a=this.getFieldValue("PIN"),b="0",b="y"===this.getFieldValue("AXIS")?_get_next_pin(a):a;return["analogRead("+b+")",Blockly.Arduino.ORDER_ATOMIC]};function hexToR(a){return parseInt(cutHex(a).substring(0,2),16)}function hexToG(a){return parseInt(cutHex(a).substring(2,4),16)}function hexToB(a){return parseInt(cutHex(a).substring(4,6),16)}function cutHex(a){return"#"==a.charAt(0)?a.substring(1,7):a}
Blockly.Arduino.grove_rgb_led=function(){var a=this.getFieldValue("PIN"),b=_get_next_pin(a);Blockly.Arduino.setups_["setup_input_"+a]="pinMode("+a+", OUTPUT);";Blockly.Arduino.setups_["setup_input_"+b]="pinMode("+b+", OUTPUT);";Blockly.Arduino.definitions_.define_uint8="#define uint8 unsigned char";Blockly.Arduino.definitions_.define_uint16="#define uint16 unsigned int";Blockly.Arduino.definitions_.define_uint32="#define uint32 unsigned long int";Blockly.Arduino.definitions_["define_clkproduce_"+

File diff suppressed because one or more lines are too long

@ -259,7 +259,7 @@ Blockly.Blocks['grove_ultrasonic_ranger'] = {
.appendField(new Blockly.FieldDropdown(profile.default.digital), "PIN")
.appendField("unit")
.appendField(new Blockly.FieldDropdown([["cm", "cm"], ["inch", "inch"]]), "UNIT");
this.setOutput(true, 'Boolean');
this.setOutput(true, 'Number');
this.setTooltip('Non-contact distance measurement module');
}
};

@ -61,7 +61,7 @@ Blockly.Blocks.grove_sound_sensor={helpUrl:"http://www.seeedstudio.com/wiki/Grov
Blockly.Blocks.grove_pir_motion_sensor={helpUrl:"http://www.seeedstudio.com/wiki/Grove_-_PIR_Motion_Sensor",init:function(){this.setColour(190);this.appendDummyInput().appendField("PIR Motion Sensor").appendField(new Blockly.FieldImage("http://www.seeedstudio.com/wiki/images/thumb/f/fd/Twig-PIR_Motion_Sensor.jpg/400px-Twig-PIR_Motion_Sensor.jpg",64,64)).appendField("PIN#").appendField(new Blockly.FieldDropdown(profile["default"].digital),"PIN");this.setOutput(!0,"Number");this.setTooltip("When anyone moves in it's detecting range, the sensor outputs HIGH.")}};
Blockly.Blocks.grove_line_finder={helpUrl:"http://www.seeedstudio.com/wiki/Grove_-_Line_Finder",init:function(){this.setColour(190);this.appendDummyInput().appendField("Line Finder").appendField(new Blockly.FieldImage("http://www.seeedstudio.com/wiki/images/thumb/8/82/Grovelinefinder1.jpg/400px-Grovelinefinder1.jpg",64,64)).appendField("PIN#").appendField(new Blockly.FieldDropdown(profile["default"].digital),"PIN");this.setOutput(!0,"Boolean");this.setTooltip("Output digital signal so the robot can reliably follow a black line on a white background")}};
Blockly.Blocks.grove_ultrasonic_ranger={helpUrl:"http://www.seeedstudio.com/wiki/Grove_-_Ultrasonic_Ranger",init:function(){this.setColour(190);this.appendDummyInput().appendField("Ultrasonic Ranger").appendField(new Blockly.FieldImage("http://www.seeedstudio.com/wiki/images/thumb/b/b0/Twig_-_Ultrasonic_Ranger2.jpg/200px-Twig_-_Ultrasonic_Ranger2.jpg",64,64)).appendField("PIN#").appendField(new Blockly.FieldDropdown(profile["default"].digital),"PIN").appendField("unit").appendField(new Blockly.FieldDropdown([["cm",
"cm"],["inch","inch"]]),"UNIT");this.setOutput(!0,"Boolean");this.setTooltip("Non-contact distance measurement module")}};
"cm"],["inch","inch"]]),"UNIT");this.setOutput(!0,"Number");this.setTooltip("Non-contact distance measurement module")}};
Blockly.Blocks.grove_motor_shield={helpUrl:"http://www.seeedstudio.com/wiki/Motor_Shield",init:function(){this.setColour(190);this.appendDummyInput().appendField("Motor").appendField(new Blockly.FieldImage("http://www.seeedstudio.com/wiki/images/thumb/4/4d/Smotoshield2.jpg/400px-Smotoshield2.jpg",64,64)).appendField(new Blockly.FieldDropdown([["Stop","stop"],["Forward","forward"],["Right","right"],["Left","left"],["Backward","backward"]]),"DIRECTION");this.setPreviousStatement(!0,null);this.setNextStatement(!0,
null);this.setTooltip("Drive two brushed DC motors")}};
Blockly.Blocks.grove_thumb_joystick={helpUrl:"http://www.seeedstudio.com/wiki/Grove_-_Thumb_Joystick",init:function(){this.setColour(10);this.appendDummyInput().appendField("Thumb Joystick").appendField(new Blockly.FieldImage("http://www.seeedstudio.com/wiki/images/thumb/e/e0/Twig_-_Thumb_Joystick_v0.9b.jpg/200px-Twig_-_Thumb_Joystick_v0.9b.jpg",64,64)).appendField("PIN#").appendField(new Blockly.FieldDropdown(profile["default"].analog),"PIN").appendField("axis").appendField(new Blockly.FieldDropdown([["x","x"],

@ -276,14 +276,12 @@ Blockly.Arduino.grove_ultrasonic_ranger = function() {
var dropdown_pin = this.getFieldValue('PIN');
var dropdown_unit = this.getFieldValue('UNIT');
Blockly.Arduino.definitions_['define_ultrasonic'] = '#include <Ultrasonic.h>\n';
Blockly.Arduino.definitions_['var_ultrasonic'+dropdown_pin] = 'Ultrasonic ultrasonic_'+dropdown_pin+'('+dropdown_pin+');\n';
Blockly.Arduino.definitions_['var_ultrasonic'+dropdown_pin] = 'Ultrasonic ultrasonic_'+dropdown_pin+'('+dropdown_pin+');';
var code;
if(dropdown_unit==="cm"){
Blockly.Arduino.setups_['setup_ultrasonic_'+dropdown_pin] = 'ultrasonic_'+dropdown_pin+'.MeasureInCentimeters();';
code = 'ultrasonic_'+dropdown_pin+'.RangeInCentimeters();';
code = 'ultrasonic_'+dropdown_pin+'.MeasureInCentimeters()';
} else {
Blockly.Arduino.setups_['setup_ultrasonic_'+dropdown_pin] = 'ultrasonic_'+dropdown_pin+'.MeasureInInches();';
code = 'ultrasonic_'+dropdown_pin+'.RangeInInches();';
code = 'ultrasonic_'+dropdown_pin+'.MeasureInInches()';
}
return [code, Blockly.Arduino.ORDER_ATOMIC];
};

Loading…
Cancel
Save