motor.js 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. /**
  2. * @license Licensed under the Apache License, Version 2.0 (the "License"):
  3. * http://www.apache.org/licenses/LICENSE-2.0
  4. */
  5. /**
  6. * @fileoverview Code generator for the test 2 blocks.
  7. */
  8. 'use strict';
  9. goog.provide('Blockly.Python.motor');
  10. goog.require('Blockly.Python');
  11. // Blockly.Python['ultrasonicavoidingrobot'] = function(block) {
  12. // var text_uarechopin = block.getFieldValue('uarEchoPin');
  13. // var text_uartrigpin = block.getFieldValue('uarTrigPin');
  14. // var number_uarspeed = block.getFieldValue('uarSpeed');
  15. // var number_uardistance = block.getFieldValue('uarDistance');
  16. // var uarMovementEntity;
  17. // var uarDeclaration = '#include <NewPing.h>\n' +
  18. // '#define stA ' + text_uartrigpin + '\n' +
  19. // '#define seA ' + text_uarechopin + '\n' +
  20. // '#define MAX_DISTANCE 100\n' +
  21. // 'int PWMA = 5;\n' + //speed control
  22. // 'int PWMB = 6;\n' +
  23. // 'int AIN2 = 10;\n' + //direction
  24. // 'int AIN1 = 9;\n' +
  25. // 'int BIN1 = 4;\n' + //direction
  26. // 'int BIN2 = 8;\n' +
  27. // 'int RA = 0;\n' + //init rate
  28. // 'int RB = 0;\n' +
  29. // 'int STBY = A5;\n' + //stand-by
  30. // 'int carSpeedSonar;\n' +
  31. // 'volatile int state = LOW;\n' +
  32. // 'NewPing sonarHead(stA, seA, MAX_DISTANCE);\n';
  33. // var uarSetup = 'pinMode(STBY, OUTPUT);\n' +
  34. // 'pinMode(PWMA, OUTPUT);\n' +
  35. // 'pinMode(AIN1, OUTPUT);\n' +
  36. // 'pinMode(AIN2, OUTPUT);\n' +
  37. // 'pinMode(PWMB, OUTPUT);\n' +
  38. // 'pinMode(BIN1, OUTPUT);\n' +
  39. // 'pinMode(BIN2, OUTPUT);\n' +
  40. // 'Serial.begin(115200);\n';
  41. // var uarStop = 'void Stop() {\n' +
  42. // 'move(1, 0, 2); //motor 1, full speed, left\n' +
  43. // 'move(2, 0, 2); //motor 2, full speed, left\n' +
  44. // '}\n';
  45. // var uarMove = 'void move(int motor, int speed, int direction)\n' +
  46. // '{\n' +
  47. // ' digitalWrite(STBY, HIGH); //disable standby\n' +
  48. // '\n' +
  49. // ' boolean inPin1 = LOW;\n' +
  50. // ' boolean inPin2 = HIGH;\n' +
  51. // '\n' +
  52. // ' if (direction == 1) {\n' +
  53. // ' inPin1 = HIGH;\n' +
  54. // ' inPin2 = LOW;\n' +
  55. // ' }\n' +
  56. // '\n' +
  57. // ' if (motor == 1) {\n' +
  58. // ' digitalWrite(AIN1, inPin1);\n' +
  59. // ' digitalWrite(AIN2, inPin2);\n' +
  60. // ' analogWrite(PWMA, speed);\n' +
  61. // ' } else {\n' +
  62. // ' digitalWrite(BIN1, inPin1);\n' +
  63. // ' digitalWrite(BIN2, inPin2);\n' +
  64. // ' analogWrite(PWMB, speed);\n' +
  65. // ' }\n' +
  66. // '}\n';
  67. // Blockly.Python.addDeclaration('uarDeclaration', uarDeclaration);
  68. // Blockly.Python.addSetup('uarSetupn', uarSetup);
  69. // Blockly.Python.addFunction('uarStop', uarStop);
  70. // Blockly.Python.addFunction('uarMove', uarMove);
  71. // var code = 'int dist = sonarHead.ping_cm();\n' +
  72. // 'int threshold = ' + number_uardistance + '; \n' +
  73. // '// adjustable\n' +
  74. // 'carSpeedSonar = ' + number_uarspeed + '; \n' +
  75. // 'move(1, carSpeedSonar, 0); //motor 1, full speed, left\n' +
  76. // 'move(0, carSpeedSonar, 0); //motor 2, full speed, left\n' +
  77. // 'if ( dist < threshold && dist > 0 ) {\n';
  78. // return code;
  79. // };
  80. // Blockly.Python['uardirectionforward'] = function(block) {
  81. // var number_uardirduration = block.getFieldValue('uarDirDuration');
  82. // var uarDurationSec = number_uardirduration * 1000;
  83. // // TODO: Assemble JavaScript into code variable.
  84. // var code = 'move(1, carSpeedSonar, 0); //motor 1, full speed, left\n' +
  85. // 'move(0, carSpeedSonar, 0); //motor 2, full speed, left\n' +
  86. // 'delay(' + uarDurationSec + ');\n';
  87. // return code;
  88. // };
  89. // Blockly.Python['uardirectionbackward'] = function(block) {
  90. // var number_uardirduration = block.getFieldValue('uarDirDuration');
  91. // var uarDurationSec = number_uardirduration * 1000;
  92. // // TODO: Assemble JavaScript into code variable.
  93. // var code = 'move(1, carSpeedSonar, 1); //motor 1, full speed, left\n' +
  94. // 'move(0, carSpeedSonar, 1); //motor 2, full speed, left\n' +
  95. // 'delay(' + uarDurationSec + ');\n';
  96. // return code;
  97. // };
  98. // Blockly.Python['uardirectionleft'] = function(block) {
  99. // var number_uardirduration = block.getFieldValue('uarDirDuration');
  100. // var uarDurationSec = number_uardirduration * 1000;
  101. // // TODO: Assemble JavaScript into code variable.
  102. // var code = 'move(1, carSpeedSonar, 1); //motor 1, full speed, left\n' +
  103. // 'move(0, carSpeedSonar, 0); //motor 2, full speed, left\n' +
  104. // 'delay(' + uarDurationSec + ');\n';
  105. // return code;
  106. // };
  107. // Blockly.Python['uardirectionright'] = function(block) {
  108. // var number_uardirduration = block.getFieldValue('uarDirDuration');
  109. // var uarDurationSec = number_uardirduration * 1000;
  110. // // TODO: Assemble JavaScript into code variable.
  111. // var code = 'move(1, carSpeedSonar, 0); //motor 1, full speed, left\n' +
  112. // 'move(0, carSpeedSonar, 1); //motor 2, full speed, left\n' +
  113. // 'delay(' + uarDurationSec + ');\n';
  114. // return code;
  115. // };
  116. // Blockly.Python['uarfinishsetup'] = function(block) {
  117. // // TODO: Assemble JavaScript into code variable.
  118. // var code = '}\n';
  119. // return code;
  120. // };
  121. // Blockly.Python['motor_setup'] = function(block) {
  122. // var motor_LT_decl = '#define SWITCHA 12\n' +
  123. // '#define SWITCHB 13\n' +
  124. // 'int LightAnalogValue_A, LightAnalogValue_B;\n';
  125. // Blockly.Python.addDeclaration('motor_LT_decl', motor_LT_decl);
  126. // //motor A & B setup
  127. // var motor_AB_decl = 'int PWMA = 5;\n' + //speed control
  128. // 'int PWMB = 6;\n' +
  129. // 'int AIN2 = 10;\n' + //direction
  130. // 'int AIN1 = 9;\n' +
  131. // 'int BIN1 = 4;\n' + //direction
  132. // 'int BIN2 = 8;\n' +
  133. // 'int RA = 0;\n' + //init rate
  134. // 'int RB = 0;\n' +
  135. // 'int STBY = A5;\n' + //stand-by
  136. // 'volatile int state = LOW;\n' +
  137. // 'int threshold = 500;\n' + //speed control
  138. // 'int carSpeed = 100, speedLeft = 100, speedRight = 100;';
  139. // Blockly.Python.addDeclaration('motor_AB_decl', motor_AB_decl);
  140. // var motor_AB_setup = 'pinMode(STBY, OUTPUT);\n' +
  141. // 'pinMode(PWMA, OUTPUT);\n' +
  142. // 'pinMode(AIN1, OUTPUT);\n' +
  143. // 'pinMode(AIN2, OUTPUT);\n' +
  144. // 'pinMode(PWMB, OUTPUT);\n' +
  145. // 'pinMode(BIN1, OUTPUT);\n' +
  146. // 'pinMode(BIN2, OUTPUT);\n' +
  147. // 'pinMode(A3, INPUT);\n' +
  148. // 'pinMode(SWITCHA, INPUT);\n' +
  149. // 'pinMode(3, INPUT);\n' +
  150. // 'Serial.begin(9600);\n';
  151. // Blockly.Python.addSetup('motor_AB_setup', motor_AB_setup);
  152. // var motor_setup = 'gotOne = false;\ngotNew = false;\n' +
  153. // 'codeProtocol = UNKNOWN;\n' +
  154. // 'codeValue = 0;\n' +
  155. // 'Serial.begin(9600);\n' +
  156. // 'delay(2000);\n' +
  157. // 'myReceiver.enableIRIn();\n'; //Start the receiver
  158. // // Blockly.Python.addSetup('motor_setup', motor_setup);
  159. // var motor_stop = 'void Stop() {\n' +
  160. // ' move(1, 0, 2); //motor 1, full speed, left\n' +
  161. // ' move(2, 0, 2); //motor 2, full speed, left\n' +
  162. // ' // delay(200);\n' +
  163. // '}\n';
  164. // Blockly.Python.addFunction('motor_stop', motor_stop);
  165. // var motor_move_code = 'void move(int motor, int speed, int direction)\n' +
  166. // '{\n' +
  167. // ' digitalWrite(STBY, HIGH); //disable standby\n' +
  168. // '\n' +
  169. // ' boolean inPin1 = LOW;\n' +
  170. // ' boolean inPin2 = HIGH;\n' +
  171. // '\n' +
  172. // ' if (direction == 1) {\n' +
  173. // ' inPin1 = HIGH;\n' +
  174. // ' inPin2 = LOW;\n' +
  175. // ' }\n' +
  176. // '\n' +
  177. // ' if (motor == 1) {\n' +
  178. // ' digitalWrite(AIN1, inPin1);\n' +
  179. // ' digitalWrite(AIN2, inPin2);\n' +
  180. // ' analogWrite(PWMA, speed);\n' +
  181. // ' } else {\n' +
  182. // ' digitalWrite(BIN1, inPin1);\n' +
  183. // ' digitalWrite(BIN2, inPin2);\n' +
  184. // ' analogWrite(PWMB, speed);\n' +
  185. // ' }\n' +
  186. // '}\n';
  187. // Blockly.Python.addFunction('motor_move_code', motor_move_code);
  188. // var motor_adjustLeft = 'void adjustLeft() {\n' +
  189. // ' move(2, speedLeft, 2); \n' +
  190. // ' move(1, speedRight, 1); \n' +
  191. // ' // delay(100);\n' +
  192. // '}\n';
  193. // var motor_adjustRight = 'void adjustRight() {\n' +
  194. // ' move(1, speedLeft, 2); \n' +
  195. // ' move(2, speedRight, 1);\n' +
  196. // ' // delay(100);\n' +
  197. // '}\n';
  198. // var motor_goStraight = 'void goStraight() {\n' +
  199. // ' move(1, speedLeft, 2); //motor 1, full speed, left\n' +
  200. // ' move(2, speedRight, 2); //motor 2, full speed, left\n' +
  201. // ' // delay(200);\n' +
  202. // '}\n';
  203. // var motor_reverse = 'void reverse() {\n' +
  204. // ' move(1, speedLeft, 1);\n' +
  205. // ' move(2, speedRight, 1);\n' +
  206. // '}\n';
  207. // Blockly.Python.addFunction('motor_adjustLeft', motor_adjustLeft);
  208. // Blockly.Python.addFunction('motor_adjustRight', motor_adjustRight);
  209. // Blockly.Python.addFunction('motor_goStraight', motor_goStraight);
  210. // Blockly.Python.addFunction('motor_reverse', motor_reverse);
  211. // return '';
  212. // };
  213. Blockly.Python['motor_setup'] = function() {
  214. var stby_def = 'int STBY = A5;';
  215. var motor_A_def = '// Motor A Definition\n' +
  216. 'int PWMA = 5; // Speed control\n' +
  217. 'int AIN1 = 9; // Direction\n' +
  218. 'int AIN2 = 10; // Direction';
  219. var motor_B_def = '// Motor B Definition\n' +
  220. 'int PWMB = 6; // Speed control\n' +
  221. 'int BIN1 = 4; // Direction\n' +
  222. 'int BIN2 = 8; // Direction\n';
  223. Blockly.Python.addDeclaration('STBY_define', stby_def);
  224. Blockly.Python.addDeclaration('motor_A_define', motor_A_def);
  225. Blockly.Python.addDeclaration('motor_B_define', motor_B_def);
  226. var stby_setup = 'pinMode(STBY, OUTPUT);';
  227. var motor_A_setup = '// Motor A Setup\n' +
  228. ' pinMode(PWMA, OUTPUT);\n' +
  229. ' pinMode(AIN1, OUTPUT);\n' +
  230. ' pinMode(AIN2, OUTPUT);';
  231. var motor_B_setup = '// Motor B Setup\n' +
  232. ' pinMode(PWMB, OUTPUT);\n' +
  233. ' pinMode(BIN1, OUTPUT);\n' +
  234. ' pinMode(BIN2, OUTPUT);';
  235. Blockly.Python.addSetup('stby_setup', stby_setup);
  236. Blockly.Python.addSetup('motor_A_setup', motor_A_setup);
  237. Blockly.Python.addSetup('motor_B_setup', motor_B_setup);
  238. var move_func = 'void move(int motor, int speed, int direction) {\n' +
  239. ' //Move specific motor at speed and direction\n' +
  240. ' //motor: 0 for B 1 for A\n' +
  241. ' //speed: 0 is off, and 255 is full speed\n' +
  242. ' //direction: 0 clockwise, 1 counter-clockwise\n' +
  243. ' digitalWrite(STBY, HIGH); //disable standby\n' +
  244. ' boolean inPin1 = LOW;\n' +
  245. ' boolean inPin2 = HIGH;\n' +
  246. ' if (direction == 1) {\n' +
  247. ' inPin1 = HIGH;\n' +
  248. ' inPin2 = LOW;\n }\n' +
  249. ' if (motor == 1) {\n' +
  250. ' digitalWrite(AIN1, inPin1);\n' +
  251. ' digitalWrite(AIN2, inPin2);\n' +
  252. ' analogWrite(PWMA, speed);\n' +
  253. ' } else {\n' +
  254. ' digitalWrite(BIN1, inPin1);\n' +
  255. ' digitalWrite(BIN2, inPin2);\n' +
  256. ' analogWrite(PWMB, speed);\n }\n}';
  257. Blockly.Python.addFunction('motor_move_function', move_func);
  258. var stop_func = 'void stop() {\n' +
  259. ' //enable standby\n' +
  260. ' digitalWrite(STBY, LOW);\n' +
  261. '}';
  262. Blockly.Python.addFunction('motor_stop_function', stop_func);
  263. return '';
  264. }
  265. Blockly.Python['motor_speed'] = function(block) {
  266. var number_speed = block.getFieldValue('SPEED');
  267. var code = number_speed;
  268. return [code, Blockly.Python.ORDER_NONE];
  269. }
  270. Blockly.Python['motorA_move_clockwise'] = function(block) {
  271. var dropdown_motor = block.getFieldValue('MOTOR');
  272. var value_speed = Blockly.Python.valueToCode(block, 'SPEED', Blockly.Python.ORDER_ATOMIC);
  273. var dropdown_direction = block.getFieldValue('DIRECTION');
  274. var code = 'move('+dropdown_motor+','+value_speed+','+dropdown_direction+');\n';
  275. return code;
  276. }
  277. Blockly.Python['motorA_move_anticlockwise'] = function(block) {
  278. var dropdown_motor = block.getFieldValue('MOTOR');
  279. var value_speed = Blockly.Python.valueToCode(block, 'SPEED', Blockly.Python.ORDER_ATOMIC);
  280. var dropdown_direction = block.getFieldValue('DIRECTION');
  281. var code = 'move('+dropdown_motor+','+value_speed+','+dropdown_direction+');\n';
  282. return code;
  283. }
  284. Blockly.Python['motorB_move_clockwise'] = function(block) {
  285. var dropdown_motor = block.getFieldValue('MOTOR');
  286. var value_speed = Blockly.Python.valueToCode(block, 'SPEED', Blockly.Python.ORDER_ATOMIC);
  287. var dropdown_direction = block.getFieldValue('DIRECTION');
  288. var code = 'move('+dropdown_motor+','+value_speed+','+dropdown_direction+');\n';
  289. return code;
  290. }
  291. Blockly.Python['motorB_move_anticlockwise'] = function(block) {
  292. var dropdown_motor = block.getFieldValue('MOTOR');
  293. var value_speed = Blockly.Python.valueToCode(block, 'SPEED', Blockly.Python.ORDER_ATOMIC);
  294. var dropdown_direction = block.getFieldValue('DIRECTION');
  295. var code = 'move('+dropdown_motor+','+value_speed+','+dropdown_direction+');\n';
  296. return code;
  297. }
  298. Blockly.Python['motor_stop'] = function(block) {
  299. var code = 'stop();\n';
  300. return code;
  301. }
  302. // //TODO: remote control button setup
  303. // Blockly.Python['motor_remote_setup'] = function(block) {
  304. // //Blockly.Python.addDeclaration('remote_button_setup', remote_button_decl);
  305. // var dropdown_run = block.getFieldValue('run');
  306. // var dropdown_reverse = block.getFieldValue('reverse');
  307. // var dropdown_turn_left = block.getFieldValue('turn_left');
  308. // var dropdown_turn_right = block.getFieldValue('turn_right');
  309. // var dropdown_stop = block.getFieldValue('stop');
  310. // var dropdown_speed_0 = block.getFieldValue('speed_0');
  311. // var dropdown_speed_80 = block.getFieldValue('speed_80');
  312. // var dropdown_speed_100 = block.getFieldValue('speed_100');
  313. // var dropdown_speed_120 = block.getFieldValue('speed_120');
  314. // var button_setup = '#define FW '+dropdown_run+'\n' +
  315. // '#define BW '+dropdown_reverse+'\n' +
  316. // '#define LT '+dropdown_turn_left+'\n' +
  317. // '#define RT '+dropdown_turn_right+'\n' +
  318. // '#define ST '+dropdown_stop+'\n' +
  319. // '#define SPD0 '+dropdown_speed_0+'\n' +
  320. // '#define SPD1 '+dropdown_speed_80+'\n' +
  321. // '#define SPD2 '+dropdown_speed_100+'\n' +
  322. // '#define SPD3 '+dropdown_speed_120+'\n';
  323. // Blockly.Python.addDeclaration('button_setup', button_setup);
  324. // var ir_setup = 'int bufTime = 20;';
  325. // Blockly.Python.addDeclaration('ir_setup', ir_setup);
  326. // var sendCode = 'void sendCode(void) {\n' +
  327. // ' if ( !gotNew ) { //We have already sent this so handle toggle bits\n' +
  328. // ' if (codeProtocol == RC5) {\n' +
  329. // ' codeValue ^= 0x0800;\n' +
  330. // ' }\n' +
  331. // ' else if (codeProtocol == RC6) {\n' +
  332. // ' switch (codeBits) {\n' +
  333. // ' case 20: codeValue ^= 0x10000; break;\n' +
  334. // ' case 24: codeValue ^= 0x100000; break;\n' +
  335. // ' case 28: codeValue ^= 0x1000000; break;\n' +
  336. // ' case 32: codeValue ^= 0x8000; break;\n' +
  337. // ' }\n' +
  338. // ' }\n' +
  339. // ' }\n' +
  340. // ' gotNew = false;\n' +
  341. // ' if (codeProtocol == UNKNOWN) {\n' +
  342. // ' //The raw time values start in decodeBuffer[1] because\n' +
  343. // ' //the [0] entry is the gap between frames. The address\n' +
  344. // ' //is passed to the raw send routine.\n' +
  345. // ' // codeValue = (uint32_t) & (recvGlobal.decodeBuffer[1]);\n' +
  346. // ' //This is not really number of bits. It is the number of entries\n' +
  347. // ' //in the buffer.\n' +
  348. // ' codeBits = recvGlobal.decodeLength - 1;\n' +
  349. // ' Serial.println(F("Sent raw"));\n' +
  350. // ' }\n' +
  351. // ' mySender.send(codeProtocol, codeValue, codeBits);\n' +
  352. // ' if (codeProtocol == UNKNOWN) return;\n' +
  353. // ' Serial.print(F("Sent "));\n' +
  354. // ' Serial.print(Pnames(codeProtocol));\n' +
  355. // ' Serial.print(F(" Value:0x"));\n' +
  356. // ' Serial.println(codeValue, HEX);\n' +
  357. // '}\n' +
  358. // '\n';
  359. // Blockly.Python.addFunction('sendCode', sendCode);
  360. // var storeCode = 'void storeCode(void) {\n' +
  361. // ' gotNew = true; gotOne = true;\n' +
  362. // ' codeProtocol = myDecoder.protocolNum;\n' +
  363. // ' Serial.print(F("Received "));\n' +
  364. // ' Serial.print(Pnames(codeProtocol));\n' +
  365. // ' if (codeProtocol == UNKNOWN) {\n' +
  366. // ' Serial.println(F(" saving raw data."));\n' +
  367. // ' // myDecoder.dumpResults();\n' +
  368. // ' // codeValue = myDecoder.value;\n' +
  369. // ' }\n' +
  370. // ' else {\n' +
  371. // ' if (myDecoder.value == REPEAT_CODE) {\n' +
  372. // ' // Do not record a NEC repeat value as that is useless.\n' +
  373. // ' Serial.println(F("repeat; ignoring."));\n' +
  374. // ' } else {\n' +
  375. // ' codeValue = myDecoder.value;\n' +
  376. // ' codeBits = myDecoder.bits;\n' +
  377. // ' }\n' +
  378. // ' Serial.print(F(" Value:0x"));\n' +
  379. // ' Serial.println(codeValue, HEX);\n' +
  380. // ' }\n' +
  381. // '}\n' +
  382. // '\n';
  383. // Blockly.Python.addFunction('storeCode', storeCode);
  384. // var speedProc = 'void speedProc() {\n' +
  385. // ' switch(myDecoder.value)\n' +
  386. // ' {\n' +
  387. // ' case SPD0:\n' +
  388. // ' stop();\n' +
  389. // ' delay(20);\n' +
  390. // ' carSpeed = 0;\n' +
  391. // ' break;\n' +
  392. // ' case SPD1:\n' +
  393. // ' stop();\n' +
  394. // ' delay(20);\n' +
  395. // ' carSpeed = 80;\n' +
  396. // ' break;\n' +
  397. // ' case SPD2:\n' +
  398. // ' stop();\n' +
  399. // ' delay(20);\n' +
  400. // ' carSpeed = 100;\n' +
  401. // ' break;\n' +
  402. // ' case SPD3:\n' +
  403. // ' stop();\n' +
  404. // ' delay(20);\n' +
  405. // ' carSpeed = 120;\n' +
  406. // ' break;\n' +
  407. // ' }\n' +
  408. // '}\n' +
  409. // '\n';
  410. // Blockly.Python.addFunction('speedProc', speedProc);
  411. // var carProc = 'void carProc() {\n' +
  412. // ' switch (myDecoder.value) {\n' +
  413. // ' case FW:\n' +
  414. // ' stop();\n' +
  415. // ' delay(bufTime);\n' +
  416. // ' move(1, carSpeed*2, 0);\n' +
  417. // ' move(2, carSpeed*2, 0);\n' +
  418. // ' delay(bufTime);\n' +
  419. // ' digitalWrite(0, HIGH);\n' +
  420. // ' break;\n' +
  421. // ' case BW:\n' +
  422. // ' stop();\n' +
  423. // ' delay(bufTime);\n' +
  424. // ' move(1, carSpeed*2, 1);\n' +
  425. // ' move(2, carSpeed*2, 1);\n' +
  426. // ' delay(bufTime);\n' +
  427. // ' digitalWrite(0, LOW);\n' +
  428. // ' break;\n' +
  429. // ' case LT:\n' +
  430. // ' stop();\n' +
  431. // ' delay(bufTime);\n' +
  432. // ' move(1, carSpeed, 0);\n' +
  433. // ' move(2, carSpeed*2, 0);\n' +
  434. // ' delay(bufTime);\n' +
  435. // ' digitalWrite(1, HIGH);\n' +
  436. // ' break;\n' +
  437. // ' case RT:\n' +
  438. // ' stop();\n' +
  439. // ' delay(bufTime);\n' +
  440. // ' move(1, carSpeed*2, 0);\n' +
  441. // ' move(2, carSpeed, 0);\n' +
  442. // ' digitalWrite(1, LOW);\n' +
  443. // ' delay(bufTime);\n' +
  444. // ' break;\n' +
  445. // ' case ST:\n' +
  446. // ' stop();\n' +
  447. // ' delay(bufTime);\n' +
  448. // ' break;\n' +
  449. // ' }\n' +
  450. // '}\n';
  451. // Blockly.Python.addFunction('carProc', carProc);
  452. // var stop = 'void stop() {\n' +
  453. // ' //enable standby\n' +
  454. // ' digitalWrite(STBY, LOW);\n' +
  455. // '}\n';
  456. // Blockly.Python.addFunction('stop', stop);
  457. // var code = 'speedProc();\n' +
  458. // ' carProc();\n' +
  459. // ' \n' +
  460. // ' if (myReceiver.getResults()) {\n' +
  461. // ' myDecoder.decode();\n' +
  462. // ' storeCode();\n' +
  463. // ' myReceiver.enableIRIn(); // Re-enable receiver\n' +
  464. // ' }\n' +
  465. // ' delay(100);\n';
  466. // return code;
  467. // }
  468. // Blockly.Python['motor_path_planning'] = function(block) {
  469. // var motor_IR_decl = '#include <IRLibDecodeBase.h>\n' +
  470. // '#include <IRLibSendBase.h>\n' +
  471. // '#include <IRLib_P01_NEC.h>\n' +
  472. // '#include <IRLib_P02_Sony.h>\n' +
  473. // '#include <IRLib_P03_RC5.h>\n' +
  474. // '#include <IRLib_P04_RC6.h>\n' +
  475. // '#include <IRLib_P05_Panasonic_Old.h>\n' +
  476. // '#include <IRLib_P07_NECx.h>\n' +
  477. // '#include <IRLib_HashRaw.h>\n' + //We need this for IRsendRaw
  478. // '#include <IRLibCombo.h>\n' +
  479. // 'IRdecode myDecoder;\n' +
  480. // 'IRsend mySender;\n' +
  481. // '#include <IRLibRecv.h>\n' +
  482. // 'IRrecv myReceiver(A2);\n' + //pin number for the receiver
  483. // 'int carSpeedIR = 100;\n' +
  484. // 'int bufTimeIR = 50;\n';
  485. // Blockly.Python.addDeclaration('motor_IR_decl', motor_IR_decl);
  486. // Blockly.Python.addDeclaration('line_tracking_init_B', 'uint8_t codeProtocol;\nuint32_t codeValue;\nuint8_t codeBits;\nbool gotOne, gotNew;\n');
  487. // // Blockly.Python.addSetup('path_planning_setup', Blockly.Python.statementToCode(block, 'motor_motion'));
  488. // return Blockly.Python.statementToCode(block, 'motor_motion');
  489. // };
  490. // Blockly.Python['motor_turn_left'] = function(block) {
  491. // var text_delay = block.getFieldValue('delay');
  492. // var code = "adjustLeft();\ndelay("+text_delay+");\n";
  493. // return code;
  494. // };
  495. // Blockly.Python['motor_turn_right'] = function(block) {
  496. // var text_delay = block.getFieldValue('delay');
  497. // var code = "adjustRight();\ndelay("+text_delay+");\n";
  498. // return code;
  499. // };
  500. // Blockly.Python['motor_go_forward'] = function(block) {
  501. // var text_delay = block.getFieldValue('delay');
  502. // var code = "goStraight();\ndelay("+text_delay+");\n";
  503. // return code;
  504. // };
  505. // Blockly.Python['motor_go_backward'] = function(block) {
  506. // var text_delay = block.getFieldValue('delay');
  507. // var code = "reverse();\ndelay("+text_delay+");\n";
  508. // return code;
  509. // };
  510. // Blockly.Python['motor_line_tracking'] = function(block) {
  511. // var text_pin_0 = block.getFieldValue('pin_0');
  512. // var text_pin_1 = block.getFieldValue('pin_1');
  513. // var text_speed = block.getFieldValue('speed');
  514. // var LT_decl = '#define ANALOG_SENSOR_PIN_A '+text_pin_0+'\n' +
  515. // '#define ANALOG_SENSOR_PIN_B '+text_pin_1+'\n';
  516. // Blockly.Python.addDeclaration('LT_decl',LT_decl);
  517. // var line_tracking_carLineTracking = 'void carLineTracking() {\n' +
  518. // ' LightAnalogValue_A = analogRead(ANALOG_SENSOR_PIN_A); //Read the voltage from sensor\n' +
  519. // ' LightAnalogValue_B = analogRead(ANALOG_SENSOR_PIN_B);\n' +
  520. // ' \n' +
  521. // //' carSpeed = analogRead(A3);\n' +
  522. // ' carSpeed = '+text_speed+';\n'+
  523. // ' speedLeft = carSpeed;\n' +
  524. // ' speedRight = carSpeed;\n' +
  525. // ' /* Serial.println(LightAnalogValue_A);\n' +
  526. // ' Serial.println(LightAnalogValue_B);\n' +
  527. // ' Serial.println("---"); */\n' +
  528. // ' \n' +
  529. // ' if ( LightAnalogValue_A < threshold && LightAnalogValue_B < threshold )\n' +
  530. // ' { \n' +
  531. // ' goStraight();\n' +
  532. // ' Serial.println("Straight");\n' +
  533. // ' Serial.println(LightAnalogValue_A);\n' +
  534. // ' Serial.println(LightAnalogValue_B);\n' +
  535. // ' //digitalWrite(3, HIGH);\n' +
  536. // ' //digitalWrite(6, HIGH);\n' +
  537. // ' Serial.println("---");\n' +
  538. // ' } else if ( LightAnalogValue_A > threshold && LightAnalogValue_B < threshold )\n' +
  539. // ' {\n' +
  540. // ' adjustLeft();\n' +
  541. // ' Serial.println("Left");\n' +
  542. // ' Serial.println(LightAnalogValue_A);\n' +
  543. // ' Serial.println(LightAnalogValue_B);\n' +
  544. // ' //digitalWrite(6, HIGH);\n' +
  545. // ' Serial.println("---");\n' +
  546. // ' } else if ( LightAnalogValue_A < threshold && LightAnalogValue_B > threshold )\n' +
  547. // ' {\n' +
  548. // ' adjustRight();\n' +
  549. // ' Serial.println("Right");\n' +
  550. // ' Serial.println(LightAnalogValue_A);\n' +
  551. // ' Serial.println(LightAnalogValue_B);\n' +
  552. // ' //digitalWrite(3, HIGH);\n' +
  553. // ' Serial.println("---");\n' +
  554. // ' } else\n' +
  555. // ' {\n' +
  556. // ' Stop();\n' +
  557. // ' Serial.println("Stopped");\n' +
  558. // ' Serial.println(LightAnalogValue_A);\n' +
  559. // ' Serial.println(LightAnalogValue_B);\n' +
  560. // ' Serial.println("---");\n' +
  561. // ' }\n' +
  562. // ' delay(10);\n' +
  563. // '}\n';
  564. // Blockly.Python.addFunction('line_tracking_carLineTracking', line_tracking_carLineTracking);
  565. // var line_tracking_speedProc = 'void speedProc() {\n' +
  566. // ' switch(myDecoder.value)\n' +
  567. // ' {\n' +
  568. // ' case SPD0:\n' +
  569. // ' Stop();\n' +
  570. // ' delay(20);\n' +
  571. // ' carSpeedIR = 0;\n' +
  572. // ' break;\n' +
  573. // ' case SPD1:\n' +
  574. // ' Stop();\n' +
  575. // ' delay(20);\n' +
  576. // ' carSpeedIR = 80;\n' +
  577. // ' break;\n' +
  578. // ' case SPD2:\n' +
  579. // ' Stop();\n' +
  580. // ' delay(20);\n' +
  581. // ' carSpeedIR = 100;\n' +
  582. // ' break;\n' +
  583. // ' case SPD3:\n' +
  584. // ' Stop();\n' +
  585. // ' delay(20);\n' +
  586. // ' carSpeedIR = 120;\n' +
  587. // ' break;\n' +
  588. // ' }\n' +
  589. // '}\n';
  590. // //Blockly.Python.addFunction('line_tracking_speedProc', line_tracking_speedProc);
  591. // var line_tracking_carProc = 'void carProc() {\n' +
  592. // ' switch (myDecoder.value) {\n' +
  593. // ' case FW:\n' +
  594. // ' Stop();\n' +
  595. // ' delay(bufTimeIR);\n' +
  596. // ' move(1, carSpeedIR*2, 0);\n' +
  597. // ' move(2, carSpeedIR*2, 0);\n' +
  598. // ' delay(bufTimeIR);\n' +
  599. // ' digitalWrite(0, HIGH);\n' +
  600. // ' break;\n' +
  601. // ' case BW:\n' +
  602. // ' Stop();\n' +
  603. // ' delay(bufTimeIR);\n' +
  604. // ' move(1, carSpeedIR*2, 1);\n' +
  605. // ' move(2, carSpeedIR*2, 1);\n' +
  606. // ' delay(bufTimeIR);\n' +
  607. // ' digitalWrite(0, LOW);\n' +
  608. // ' break;\n' +
  609. // ' case LT:\n' +
  610. // ' Stop();\n' +
  611. // ' delay(bufTimeIR);\n' +
  612. // ' move(1, carSpeedIR, 0);\n' +
  613. // ' move(2, carSpeedIR*2, 0);\n' +
  614. // ' delay(bufTimeIR);\n' +
  615. // ' digitalWrite(1, HIGH);\n' +
  616. // ' break;\n' +
  617. // ' case RT:\n' +
  618. // ' Stop();\n' +
  619. // ' delay(bufTimeIR);\n' +
  620. // ' move(1, carSpeedIR*2, 0);\n' +
  621. // ' move(2, carSpeedIR, 0);\n' +
  622. // ' digitalWrite(1, LOW);\n' +
  623. // ' delay(bufTimeIR);\n' +
  624. // ' break;\n' +
  625. // ' case ST:\n' +
  626. // ' Stop();\n' +
  627. // ' delay(bufTimeIR);\n' +
  628. // ' break;\n' +
  629. // ' }\n' +
  630. // '}\n';
  631. // //Blockly.Python.addFunction('line_tracking_carProc', line_tracking_carProc);
  632. // var code = 'carLineTracking();\n';
  633. // return code;
  634. // };
  635. // Blockly.Python['motor_ultrasonic_pin'] = function(block) {
  636. // var text_pin_var0 = block.getFieldValue('pin_var0');
  637. // var text_pin_0 = block.getFieldValue('pin_0');
  638. // var text_pin_var1 = block.getFieldValue('pin_var1');
  639. // var text_pin_1 = block.getFieldValue('pin_1');
  640. // var text_pin_var2 = block.getFieldValue('pin_var2');
  641. // var text_pin_2 = block.getFieldValue('pin_2');
  642. // var text_pin_var3 = block.getFieldValue('pin_var3');
  643. // var text_pin_3 = block.getFieldValue('pin_3');
  644. // var text_pin_var4 = block.getFieldValue('pin_var4');
  645. // var text_pin_4 = block.getFieldValue('pin_4');
  646. // var text_pin_var5 = block.getFieldValue('pin_var5');
  647. // var text_pin_5 = block.getFieldValue('pin_5');
  648. // var text_max_distance_var = block.getFieldValue('max_distance_var');
  649. // var text_max_distance = block.getFieldValue('max_distance');
  650. // var text_real_time_distance_var = block.getFieldValue('real_time_distance_var');
  651. // var text_real_time_distance = block.getFieldValue('real_time_distance');
  652. // var motor_ultrasonic_decl = '#include <NewPing.h>\n' +
  653. // '#define TRIGGER_PIN 1\n' +
  654. // '#define ECHO_PIN 0\n' +
  655. // '#define ' + text_pin_var0 + ' '+text_pin_0+'\n' +
  656. // '#define ' + text_pin_var1 + ' '+text_pin_1+'\n' +
  657. // '#define ' + text_pin_var2 + ' '+text_pin_2+'\n' +
  658. // '#define ' + text_pin_var3 + ' '+text_pin_3+'\n' +
  659. // '#define ' + text_pin_var4 + ' '+text_pin_4+'\n' +
  660. // '#define ' + text_pin_var5 + ' '+text_pin_5+'\n' +
  661. // '#define ' + text_max_distance_var + ' ' + text_max_distance + '\n' + // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
  662. // '#define ' + text_real_time_distance_var + ' ' + text_real_time_distance + '\n' +
  663. // 'NewPing sonar(TRIGGER_PIN, ECHO_PIN, '+text_max_distance_var+');\n' +
  664. // 'int carSpeedSonar;\n' +
  665. // '#define POT A3\n' +
  666. // 'NewPing sonarHead('+text_pin_var0+', '+text_pin_var1 +', '+text_max_distance_var+');\n' +
  667. // 'NewPing sonarLeft('+text_pin_var2+', '+text_pin_var3+','+text_max_distance_var+');\n' +
  668. // 'NewPing sonarRigt('+text_pin_var4+', '+text_pin_var5+', '+text_max_distance_var+');\n';
  669. // Blockly.Python.addDeclaration('motor_ultrasonic_decl', motor_ultrasonic_decl);
  670. // var ultrasonic_loop_code = 'delay(10); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.\n' +
  671. // ' // Left: 1, 0\n' +
  672. // ' // Right: 0, 1\n' +
  673. // ' // Forward: 1, 1\n' +
  674. // ' int distHead = sonarHead.ping_cm();\n' +
  675. // ' int distLeft = sonarLeft.ping_cm();\n' +
  676. // ' int distRigt = sonarRigt.ping_cm();\n' +
  677. // ' // carSpeedSonar = analogRead(A3);\n' +
  678. // ' carSpeedSonar = 180;\n' +
  679. // ' move(0, carSpeedSonar - 20, 1);\n' +
  680. // ' move(1, carSpeedSonar - 20, 1);\n' +
  681. // ' if ( distHead < '+text_real_time_distance_var+' && distHead != 0 ) {\n' +
  682. // ' move(0, carSpeedSonar, 0);\n' +
  683. // ' move(1, carSpeedSonar, 0);\n' +
  684. // ' delay(100);\n' +
  685. // ' move(0, carSpeedSonar, 1);\n' +
  686. // ' move(1, carSpeedSonar, 0);\n' +
  687. // ' delay(100);\n' +
  688. // ' if ( distLeft > distRigt ) {\n' +
  689. // ' move(0, carSpeedSonar, 1);\n' +
  690. // ' move(1, carSpeedSonar, 0);\n' +
  691. // ' delay(50);\n' +
  692. // ' } else if (distLeft < distRigt) {\n' +
  693. // ' move(0, carSpeedSonar, 0);\n' +
  694. // ' move(1, carSpeedSonar, 1);\n' +
  695. // ' delay(150);\n' +
  696. // ' }\n' +
  697. // ' } else if ( distLeft < '+text_real_time_distance_var+' && distRigt < '+text_real_time_distance_var+' ) {\n' +
  698. // ' if ( distLeft > distRigt ) {\n' +
  699. // ' move(0, carSpeedSonar*1.2, 1);\n' +
  700. // ' move(1, carSpeedSonar, 0);\n' +
  701. // ' delay(80);\n' +
  702. // ' } else if (distLeft < distRigt) {\n' +
  703. // ' move(0, carSpeedSonar, 0);\n' +
  704. // ' move(1, carSpeedSonar*1.2, 1);\n' +
  705. // ' delay(80);\n' +
  706. // ' }\n' +
  707. // ' }\n' +
  708. // ' Serial.print("Left detect:\t");\n' +
  709. // ' Serial.print(sonarLeft.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)\n' +
  710. // ' Serial.print("cm");\n' +
  711. // ' Serial.print("\t");\n' +
  712. // ' Serial.print("Head detect:\t");\n' +
  713. // ' Serial.print(sonarHead.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)\n' +
  714. // ' Serial.print("cm");\n' +
  715. // ' Serial.print("\t");\n' +
  716. // ' Serial.print("Right detect:\t");\n' +
  717. // ' Serial.print(sonarRigt.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)\n' +
  718. // ' Serial.println("cm");\n';
  719. // return ultrasonic_loop_code;
  720. // };
  721. // var count = 0;
  722. // Blockly.Python['motor_ultrasonic_custom_pin'] = function(block) {
  723. // var text_custom_pin_var0 = block.getFieldValue('custom_pin_var0');
  724. // var text_custom_pin_0 = block.getFieldValue('custom_pin_0');
  725. // var text_custom_pin_var1 = block.getFieldValue('custom_pin_var1');
  726. // var text_custom_pin_1 = block.getFieldValue('custom_pin_1');
  727. // var text_custom_max_distance_var = block.getFieldValue('custom_max_distance_var');
  728. // var text_custom_max_distance = block.getFieldValue('custom_max_distance');
  729. // var text_custom_real_time_distance_var = block.getFieldValue('custom_real_time_distance_var');
  730. // var text_custom_real_time_distance = block.getFieldValue('custom_real_time_distance');
  731. // var custom_decl = '#define ' + text_custom_pin_var0 + ' '+text_custom_pin_0+'\n' +
  732. // '#define ' + text_custom_pin_var1 + ' '+text_custom_pin_1+'\n' +
  733. // '// Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.\n' +
  734. // '#define ' + text_custom_max_distance_var + ' ' + text_custom_max_distance + '\n' +
  735. // '#define ' + text_custom_real_time_distance_var + ' '+text_custom_real_time_distance+'\n';
  736. // Blockly.Python.addDeclaration('custom_decl' + count, custom_decl, false);
  737. // count += 1;
  738. // return '';
  739. // };
  740. // Blockly.Python['motor_controller'] = function(block) {
  741. // var text_motor_name = block.getFieldValue('motor_name');
  742. // var number_motor_speed = block.getFieldValue('motor_speed');
  743. // var dropdown_motor_direction = block.getFieldValue('motor_direction');
  744. // // TODO: Assemble JavaScript into code variable.
  745. // var code = 'move('+text_motor_name+','+number_motor_speed+','+dropdown_motor_direction+');\n';
  746. // return code;
  747. // };
  748. // Blockly.Python['motor_stop'] = function(block) {
  749. // var code = "while(true) {\n" +
  750. // "Stop();\n" +
  751. // "}\n";
  752. // return code;
  753. // };
  754. // Blockly.Python['motor_pause'] = function(block) {
  755. // var text_delay = block.getFieldValue('delay');
  756. // var code = "Stop();\ndelay("+text_delay+");\n";
  757. // return code;
  758. // };
  759. // Blockly.Python['robotcar3in1'] = function(block) {
  760. // var declarationUltra = '#include <NewPing.h>\n' +
  761. // '#define TRIGGER_PIN A6\n' +
  762. // '#define ECHO_PIN A7\n' +
  763. // '#define MAX_DISTANCE 50\n' +
  764. // 'NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);\n' +
  765. // 'int carSpeedSonar;\n' +
  766. // '#include <IRLibDecodeBase.h>\n' +
  767. // '#include <IRLibSendBase.h>\n' +
  768. // '#include <IRLib_P01_NEC.h>\n' +
  769. // '#include <IRLib_P02_Sony.h>\n' +
  770. // '#include <IRLib_P03_RC5.h>\n' +
  771. // '#include <IRLib_P04_RC6.h>\n' +
  772. // '#include <IRLib_P05_Panasonic_Old.h>\n' +
  773. // '#include <IRLib_P07_NECx.h>\n' +
  774. // '#include <IRLib_HashRaw.h>\n' +
  775. // '#include <IRLibCombo.h>\n' +
  776. // 'IRdecode myDecoder;\n' +
  777. // 'IRsend mySender;\n' +
  778. // '#include <IRLibRecv.h>\n' +
  779. // 'IRrecv myReceiver(A2);\n' +
  780. // 'uint8_t codeProtocol;\n' +
  781. // 'uint32_t codeValue;\n' +
  782. // 'uint8_t codeBits;\n' +
  783. // 'bool gotOne, gotNew;\n' +
  784. // '#define FW 0xFF18E7\n' +
  785. // '#define BW 0xFF4AB5\n' +
  786. // '#define LT 0xFF10EF\n' +
  787. // '#define RT 0xFF5AA5\n' +
  788. // '#define ST 0xFF38C7\n' +
  789. // '#define SPD0 0xFF9867\n' +
  790. // '#define SPD1 0xFFA25D\n' +
  791. // '#define SPD2 0xFF629D\n' +
  792. // '#define SPD3 0xFFE21D\n' +
  793. // 'int carSpeedIR = 60;\n' +
  794. // 'int bufTimeIR = 50;\n' +
  795. // '#define ANALOG_SENSOR_PIN_A A4\n' +
  796. // '#define ANALOG_SENSOR_PIN_B A0\n' +
  797. // '#define SWITCHA 12\n' +
  798. // '#define SWITCHB 13\n' +
  799. // 'int LightAnalogValue_A, LightAnalogValue_B;\n' +
  800. // 'int PWMA = 5;\n' +
  801. // 'int AIN2 = 10;\n' +
  802. // 'int AIN1 = 9;\n' +
  803. // 'int PWMB = 6;\n' +
  804. // 'int BIN1 = 4;\n' +
  805. // 'int BIN2 = 8;\n' +
  806. // 'int RA = 0;\n' +
  807. // 'int RB = 0;\n' +
  808. // 'int STBY = A5;\n' +
  809. // 'volatile int state = LOW;\n' +
  810. // 'int threshold = 500;\n' +
  811. // 'int carSpeed, speedLeft, speedRight;\n';
  812. // var setupUltra = 'pinMode(STBY, OUTPUT); pinMode(PWMA, OUTPUT); pinMode(AIN1, OUTPUT); pinMode(AIN2, OUTPUT); pinMode(PWMB, OUTPUT); pinMode(BIN1, OUTPUT); pinMode(BIN2, OUTPUT); pinMode(A3, INPUT); pinMode(SWITCHA, INPUT); pinMode(3, INPUT); gotOne = false; gotNew = false; codeProtocol = UNKNOWN; codeValue = 0; myReceiver.enableIRIn();';
  813. // var functionUltra = 'void storeCode(void) { gotNew = true; gotOne = true; codeProtocol = myDecoder.protocolNum; Serial.print(F("Received ")); Serial.print(Pnames(codeProtocol)); if (codeProtocol == UNKNOWN) { Serial.println(F(" saving raw data.")); } else { if (myDecoder.value == REPEAT_CODE) { Serial.println(F("repeat; ignoring.")); } else { codeValue = myDecoder.value; codeBits = myDecoder.bits; } Serial.print(F(" Value:0x")); Serial.println(codeValue, HEX); }}void sendCode(void) { if ( !gotNew ) { if (codeProtocol == RC5) { codeValue ^= 0x0800; } else if (codeProtocol == RC6) { switch (codeBits) { case 20: codeValue ^= 0x10000; break; case 24: codeValue ^= 0x100000; break; case 28: codeValue ^= 0x1000000; break; case 32: codeValue ^= 0x8000; break; } } } gotNew = false; if (codeProtocol == UNKNOWN) { codeBits = recvGlobal.decodeLength - 1; Serial.println(F("Sent raw")); } mySender.send(codeProtocol, codeValue, codeBits); if (codeProtocol == UNKNOWN) return; Serial.print(F("Sent ")); Serial.print(Pnames(codeProtocol)); Serial.print(F(" Value:0x")); Serial.println(codeValue, HEX);}void adjustLeft() { move(1, speedLeft, 1); move(2, speedRight, 2); delay(100);}void adjustRight() { move(1, speedLeft, 2); move(2, speedRight, 1); delay(100);}void goStraight() { move(1, speedLeft, 2); move(2, speedRight, 2); delay(200);}void Stop() { move(1, 0, 2); move(2, 0, 2); delay(200);}void move(int motor, int speed, int direction){ digitalWrite(STBY, HIGH); boolean inPin1 = LOW; boolean inPin2 = HIGH; if (direction == 1) { inPin1 = HIGH; inPin2 = LOW; } if (motor == 1) { digitalWrite(AIN1, inPin1); digitalWrite(AIN2, inPin2); analogWrite(PWMA, speed); } else { digitalWrite(BIN1, inPin1); digitalWrite(BIN2, inPin2); analogWrite(PWMB, speed); }}void carLineTracking() { LightAnalogValue_A = analogRead(ANALOG_SENSOR_PIN_A); LightAnalogValue_B = analogRead(ANALOG_SENSOR_PIN_B); carSpeed = analogRead(A3); speedLeft = carSpeed/6; speedRight = carSpeed/6; if ( LightAnalogValue_A < threshold && LightAnalogValue_B < threshold ) { goStraight(); Serial.println("Straight"); Serial.println(LightAnalogValue_A); Serial.println(LightAnalogValue_B); Serial.println("---"); } else if ( LightAnalogValue_A > threshold && LightAnalogValue_B < threshold ) { adjustRight(); Serial.println("Left"); Serial.println(LightAnalogValue_A); Serial.println(LightAnalogValue_B); Serial.println("---"); } else if ( LightAnalogValue_A < threshold && LightAnalogValue_B > threshold ) { adjustLeft(); Serial.println("Right"); Serial.println(LightAnalogValue_A); Serial.println(LightAnalogValue_B); Serial.println("---"); } else { Stop(); Serial.println("Stopped"); Serial.println(LightAnalogValue_A); Serial.println(LightAnalogValue_B); Serial.println("---"); } delay(10);}void speedProc() { switch(myDecoder.value) { case SPD0: Stop(); delay(20); carSpeedIR = 0; break; case SPD1: Stop(); delay(20); carSpeedIR = 80; break; case SPD2: Stop(); delay(20); carSpeedIR = 100; break; case SPD3: Stop(); delay(20); carSpeedIR = 225; break; }}void carProc() { switch (myDecoder.value) { case FW: Stop(); delay(bufTimeIR); move(1, carSpeedIR*2, 0); move(2, carSpeedIR*2, 0); delay(bufTimeIR); digitalWrite(0, HIGH); break; case BW: Stop(); delay(bufTimeIR); move(1, carSpeedIR*2, 1); move(2, carSpeedIR*2, 1); delay(bufTimeIR); digitalWrite(0, LOW); break; case LT: Stop(); delay(bufTimeIR); move(1, carSpeedIR, 0); move(2, carSpeedIR*2, 0); delay(bufTimeIR); digitalWrite(1, HIGH); break; case RT: Stop(); delay(bufTimeIR); move(1, carSpeedIR*2, 0); move(2, carSpeedIR, 0); digitalWrite(1, LOW); delay(bufTimeIR); break; case ST: Stop(); delay(bufTimeIR); break; }}void carUltraSonar() { int dist = sonar.ping_cm(); int threshold = 20; carSpeedSonar = analogRead(A3); move(1, carSpeedSonar/5, 2); move(2, carSpeedSonar/5, 2); delay(50); if (dist < threshold && dist > 0) { move(1, carSpeedSonar/5, 0); move(2, carSpeedSonar/5, 1); delay(200); dist = 15; } Serial.print("Ping: "); Serial.print(sonar.ping_cm()); Serial.println("cm"); }';
  814. // Blockly.Python.addDeclaration('declarationUltra', declarationUltra);
  815. // Blockly.Python.addSetup('setupUltra', setupUltra);
  816. // Blockly.Python.addFunction('functionUltra', functionUltra)
  817. // var code = 'int switchVal = digitalRead(SWITCHA); int switchValB = digitalRead(SWITCHB); int switchNum; if (switchValB == HIGH) { if (switchVal == HIGH) { switchNum = 0; } else if (switchVal == LOW) { switchNum = 1; } } else if (switchValB == LOW) { switchNum = 2; } switch (switchNum) { case 0: carLineTracking(); Stop(); delay(0); break; case 1: digitalWrite(3, HIGH); speedProc(); carProc(); if (myReceiver.getResults()) { myDecoder.decode(); storeCode(); myReceiver.enableIRIn(); } delay(100); break; case 2: delay(10); carUltraSonar(); Stop(); delay(0); break; } ';
  818. // return code;
  819. // };