2つの圧力センサを使用し、arduinoでデータを取得したものをBLE通信でスマホに送り、スマホの画面を制御するプログラムを行なっています。
A0側のセンサを動かす:上部の円が動く
A1側のセンサを動かす:下部の円が動く
A0・A1両方のセンサを動かす:両方の円が動く
A0側のセンサで受け取ったデータで円を制御することはできているのですが、A1で受け取ったデータで別の円を動かす方法がわかりません。どうやって別々のデータとして受け取ったら良いのでしょうか。arduinonoではバッテリーモニターのライブラリをそのまま利用し、下記のようなプログラムにしています。初心者なのでわかりづらい質問をしていたらすみません、分かる方いらっしゃいましたら回答していただけると大変ありがたいです。よろしくお願いします。
↓arduinoでのデータ取得・通信
スマホ側で受け取るプログラム↓
Javascript
1!DOCTYPE html><html lang="en"><head> 2 <script src="p5.js"></script> 3 <script src="p5.sound.min.js"></script> 4 <link rel="stylesheet" type="text/css" href="style.css"> 5 <meta charset="utf-8"> 6 7 <style> 8 *{margin: 0; padding: 0;} 9 body,html{width: 100%; height: 100%;} 10 #base{width: 100%; height: 100%; background-color: #1A274C; position: absolute; top:0; left:0; z-index: 0;} 11 #white{width: 100%; height: 100%; background-color: transparent; position: absolute; top:0; left:0; z-index: 1;} 12 #black{width: 100%; height: 100%; background-color: transparent; position: absolute; top:0; left:200px; z-index: 2 ;} 13 14 #balldown{background-color: white; position: absolute; top: 0px; left: 100px; width: 200px; height: 200px; border-radius: 200px; transition: all ease 0.5s;} 15 #ballup{background-color: white; position: absolute; top: 0px; right: 100px; width: 200px; height: 200px; border-radius: 200px; transition: all ease 0.5s;} 16 #lefteyelid{animation: flash 1s linear infinite; position: absolute; top: 230px; left: -40px; width:200px; height:200px;border-radius: 200px; background:transparent; margin:200px;} 17@keyframes flash {0%,50%,100%{opacity: 0;}35%{opacity: 1; background:#1A274C;} 65%{opacity: 1;background:transparent;}} 18 #righteyelid{animation: flash 1s linear infinite; position: absolute; top: -10px; left: -40px; width:200px; height:200px;border-radius: 200px; background:transparent; margin:200px;} 19@keyframes flash {0%,50%,100%{opacity: 0;}35%{opacity: 1; background:#1A274C;} 65%{opacity: 1;background:transparent;}} 20 21 </style> 22 23 </head> 24 <body> 25 26 <div id="base"> 27 <div id="white"> 28 <div id="balldown"></div> 29 <div id="ballup"></div> 30 </div> 31 <div id="black"> 32 <button onClick='connect()'>connect</button> 33 <button onClick='disconnect()'>disconnect</button> 34 <img src="https://github.com/mizuki-yoshida/mizuki-yoshida.github.io/raw/main/LOOK_LEFT_2.png" alt="icon"> 35 <div id="lefteyelid"></div> 36 <div id="righteyelid"></div> 37 </div> 38 </div> 39 40 41 42<script> 43 44 var myDevice; 45 var all = false; 46 47 function connect(){ 48 navigator.bluetooth.requestDevice( 49 {filters: [{services: ['battery_service']}]}) 50 .then(function(device) { 51 // save the device returned so you can disconnect later: 52 myDevice = device; 53 console.log(device); 54 // connect to the device once you find it: 55 return device.gatt.connect(); 56 }) 57 .then(function(server) { 58 // get the primary service: 59 return server.getPrimaryService('battery_service'); 60 }) 61 .then(function(service) { 62 // get the characteristic: 63 return service.getCharacteristics('battery_level'); 64 }) 65 .then(function(characteristics) { 66 // subscribe to the characteristic: 67 for (c in characteristics) { 68 characteristics[c].startNotifications() 69 .then(subscribeToChanges); 70 } 71 }) 72 .catch(function(error) { 73 // catch any errors: 74 console.error('Connection failed!', error); 75 }); 76 } 77 78 // subscribe to changes from the meter: 79 function subscribeToChanges(characteristic) { 80 characteristic.oncharacteristicvaluechanged = handleData; 81 } 82 83 // handle incoming data: 84 function handleData(event) { 85 var buf = event.target.value.getUint8("Battery Level1 % is now:"); 86 var buf2 = event.target.value.getUint8("Battery Level2 % is now:"); 87// console.log(buf); 88 var bd = document.getElementById("balldown"); 89 var bu = document.getElementById("ballup"); 90 91 if(all === false){ 92 var bright = 200; 93 var bleft = 200; 94 var bwidth = 200; 95 var rad = 200; 96 97 if(buf >= 40){ 98 console.log(all); 99 console.log(buf); 100 bleft += buf * 10; 101 bright += buf * 10; 102 bwidth += buf * 5; 103 rad -= buf; 104 105 bd.style.left = bleft + "px"; 106 bd.style.width = bwidth + "px"; 107 bd.style.height = bwidth + "px"; 108 bd.style.borderRadius = rad + "px"; 109 110 bu.style.right = bright + "px"; 111 bu.style.width = bwidth + "px"; 112 bu.style.height = bwidth + "px"; 113 bu.style.borderRadius = rad + "px"; 114 115 }else{ 116 bd.style.left = "200px"; 117 bd.style.width = "200px"; 118 bd.style.height = "200px"; 119 bd.style.borderRadius = "200px"; 120 121 bu.style.right = "200px"; 122 bu.style.width = "200px"; 123 bu.style.height = "200px"; 124 bu.style.borderRadius = "200px"; 125 }if(buf >= 60){ 126 all = true; 127 } 128 } if(all === true){ 129 bd.style.left = "0"; 130 bd.style.width = "2000px"; 131 bd.style.height = "2000px"; 132 bd.style.borderRadius = "0px"; 133 134 bu.style.right = "0"; 135 bu.style.width = "2000px"; 136 bu.style.height = "2000px"; 137 bu.style.borderRadius = "0px"; 138 } 139 140if(all === false){ 141 var bright = 200; 142 var bleft = 200; 143 var bwidth = 200; 144 var rad = 200; 145 146 if(buf2 >= 40){ 147 console.log(all); 148 console.log(buf); 149 bleft += buf * 10; 150 bright += buf * 10; 151 bwidth += buf * 5; 152 rad -= buf; 153 154 bd.style.left = bleft + "px"; 155 bd.style.width = bwidth + "px"; 156 bd.style.height = bwidth + "px"; 157 bd.style.borderRadius = rad + "px"; 158 159 bu.style.right = bright + "px"; 160 bu.style.width = bwidth + "px"; 161 bu.style.height = bwidth + "px"; 162 bu.style.borderRadius = rad + "px"; 163 164 }else{ 165 bd.style.left = "200px"; 166 bd.style.width = "200px"; 167 bd.style.height = "200px"; 168 bd.style.borderRadius = "200px"; 169 170 bu.style.right = "200px"; 171 bu.style.width = "200px"; 172 bu.style.height = "200px"; 173 bu.style.borderRadius = "200px"; 174 }if(buf2 >= 60){ 175 all = true; 176 } 177 } if(all === true){ 178 bd.style.left = "0"; 179 bd.style.width = "2000px"; 180 bd.style.height = "2000px"; 181 bd.style.borderRadius = "0px"; 182 183 bu.style.right = "0"; 184 bu.style.width = "2000px"; 185 bu.style.height = "2000px"; 186 bu.style.borderRadius = "0px"; 187 } 188 189 //draw elements 190 } 191 192 // disconnect function: 193 function disconnect() { 194 if (myDevice) { 195 // disconnect: 196 myDevice.gatt.disconnect(); 197 } 198 } 199 200 201 202</script> 203 204</body></html> 205
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。