var bottles = [P2, P3, P4, P5, P6, P7, P8]; var friendPin = A5; var holdPeriod = 500; // ms, требуемое время удержания рук var thres = 0.6; // если напряжение меньше, значит кто-то держится за ножки var friendPeriod = 0; // ms, сколько друзья уже держатся var pingPeriod = 50; // ms var isShaking = false; var start = true; var pitch = 500; // время открытия клапана для единичного объёма var shortValue = 5000; // суммарное время открытия на стакан setInterval(function() { if (analogRead(friendPin) < thres) { friendPeriod += pingPeriod; } else { friendPeriod = 0; start = true; } if (friendPeriod >= holdPeriod) { if ((!isShaking) && start) { shake(); start = false; } } }, pingPeriod); function shake() { isShaking = true; // сколько бутылок участвует var bottlesInRound = Math.floor(Math.random() * bottles.length) + 1; if (bottlesInRound == 1) { bottlesInRound = 2; } // print('bottlesInRound:', bottlesInRound); // какие бутылки участвуют? var usingBottles = shuffleArray(bottles); // print(usingBottles); // в цикле окрываем клапаны пока не заполним стакан for (var i = 0; i < shortValue; i += pitch) { // выбираем клапан var bottle = Math.floor(Math.random() * bottlesInRound); // печатаем какая бутылка наливается // print('bottle', usingBottles[bottle]); // высокий импульс digitalPulse(usingBottles[bottle], 1, pitch); // остановка до завершения импульса digitalPulse(usingBottles[bottle], 1, 0); // print('stop'); } isShaking = false; } function bottleOff(bottle) { setTimeout(function(){ digitalWrite(bottle, false); },pitch); } function shuffleArray(array) { for (var i = array.length - 1; i > 0; i--) { var j = Math.floor(Math.random() * (i + 1)); var temp = array[i]; array[i] = array[j]; array[j] = temp; } return array; }