Moving ball
by jywglady
HTML
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>Bit Leveler</title>
<script src="https://bit.webduino.io/blockly/components/jquery/dist/jquery.min.js"></script>
<script src="https://bit.webduino.io/blockly/dist/lib/webduino-all.min.js"></script>
<script src="https://bit.webduino.io/blockly/dist/MessageTransport.min.js"></script>
<script src="https://bit.webduino.io/blockly/dist/webduino-blockly.min.js"></script>
<script src="https://bit.webduino.io/blockly/dist/lib/firebase.min.js"></script>
<script src="https://bit.webduino.io/blockly/dist/lib/runtime.min.js"></script>
<script src="https://bit.webduino.io/blockly/components/webduino-bit-module-button/BitButton.js"></script>
<script src="https://bit.webduino.io/blockly/components/webduino-bit-module-button/BitButton-blockly.js"></script>
<script src="https://bit.webduino.io/blockly/components/webduino-bit-module-mpu9250/BitMPU9250-blockly.js"></script>
<script src="https://bit.webduino.io/blockly/components/webduino-bit-module-mpu9250/BitMPU9250.js"></script>
<script
src="https://bit.webduino.io/blockly/components/webduino-bit-module-led-matrix/BitLedMatrix-blockly.js"></script>
<script src="https://bit.webduino.io/blockly/components/webduino-bit-module-led-matrix/BitLedMatrix.js"></script>
</head>
<body>
<div><span id="demo-area-01-show">X = N/A, Y = N/A</span></div>
</body>
</html>
CSS
#demo-area-01-show {
font-size: 60px;
pointer-events: auto !important;
}
JavaScript
(async function () {
var value;
var buttonA;
var buttonB;
var mpu9250;
var matrix;
var range;
var mx;
var my;
var nowx;
var nowy;
var column;
var row;
function Transform(value) {
if (value < 0) {
return 360 + Math.round(value);
}
return Math.round(value);
}
function Quantize(value) {
value = Math.round(((value - ((0 - range))) * (1 / ((range) - ((0 - range))))) * ((5) - (1)) + (1));
if (value>5) value=5;
if (value<1) value=1;
return value;
}
boardReady({ board: 'Bit', device: 'bit77730e', transport: 'mqtt', multi: true }, async function (board) {
board.samplingInterval = 250;
buttonA = getPullupButton(board, 35);
buttonB = getPullupButton(board, 27);
mpu9250 = getMPU9250(board);
matrix = getMatrix(board, 4, 25);
mx = 0;
my = 180;
nowx = 0;
nowy = 180;
range = 30;
buttonA.on('pressed', async function () {
await delay(0.1);
mx = nowx;
my = nowy;
});
buttonB.on('pressed', async function () {
mx = 0;
my = 180;
});
mpu9250.on(webduino.module.MPU9250Event.ANGLE_MESSAGE, async function () {
nowx = Math.round(mpu9250.angVals[1]);
nowy = Math.round(mpu9250.angVals[0]);
if (nowy<-90) nowy = nowy+360;
var dx = nowx - mx;
var dy = nowy - my;
//if (360+dy < -dy) dy = 360+dy;
console.log("dx = ", dx, " dy = ", dy);
column = Quantize(dx);
row = Quantize(dy);
console.log("row = ", row, " col = ", column);
document.getElementById("demo-area-01-show").innerHTML = (['X = ', column, ', ', 'Y = ', row].join(''));
matrix.off();
matrix.setColor((((row - 1) * 5 + column) - 1), '#ff0000');
});
});
}());