无法检测到Nodemcu上按下的闪光灯按钮

时间:2019-01-04 16:39:57

标签: iot esp8266 micropython

我正在使用带有Esp8266模块的简单Micropython,无法检测esp上的闪光灯按钮是否被按下

我尝试使用引脚0,但不起作用。

p2=Pin(0,Pin.IN)
while True:
    print(p2.value())
    if p2.value():
        break;

我也尝试过此操作,但它也无法按预期工作。

p2 = Pin(0,Pin.PULL_DOWN)
while True:
    print(p2.value())
if p2.value():
        break;

1 个答案:

答案 0 :(得分:0)

我刚刚解决了我的问题。 这就是我能够检测到esp上的闪光灯按钮

var points = 1000;
 document.getElementById("points").innerHTML = points
function randomNum() {
 // var YourMoney = 100
  //var MoneyLeft
  if(points.toString() == "100"){
   alert("you have lost!");
  }else{
  points = points-100;
   document.getElementById("points").innerHTML = points

  var n = []
  var number = 0;
var digits = '';

for (i = 0; i < 3; i++) {
    number = Math.floor(Math.random() * 10);
    digits = digits + number.toString();
}
console.log("digits--",digits)
  document.getElementById("randomNumbers").innerHTML = digits;
let x = String(digits).split('')
console.log("x--",x)
// for(var i =0; i<x.length; i++){
// if(Math.abs(i % 2) == 1){
// console.log("no is odd");
// }
// })
// if( i % 2 == 0){
// console.log("no is even");
// }
// }
var result = [];
for(var i =0; i<x.length; i++){
console.log("Math.abs(i % 2)--",Math.abs(i % 2))
if(Math.abs(x[i] % 2) == 1){
console.log("no is odd");
result.push('odd');
}else if(Math.abs(x[i] % 2) == 0){
console.log("no is even");
result.push('even');
}
}

var matches = [""]; digits.split("").forEach(function(val){ 
  var lastNum = 0;
  if ( matches[matches.length-1].length > 0 )
  {
    lastNum = parseInt(matches[matches.length-1].slice(-1),10);
  }
  var currentNum = parseInt(val,10);
  if ( currentNum == lastNum + 1 ) 
  { 
    matches[matches.length-1] += String(currentNum); 
  }
  else 
  { 
    if ( matches[matches.length-1].length > 1 ) 
    { 
       matches.push(String(currentNum)) 
    } 
    else 
    { matches[matches.length-1] = String(currentNum); 
    } 
  }
});
matches = matches.filter(function(val){ return val.length > 1 }) //outputs ["6789", "123"]
console.log("matchg",matches.length);
//document.body.innerHTML += JSON.stringify(matches,0,4);
console.log("arrarrarr",result,"--",result.every( (val, i, arr) => val === arr[0] ));
if(result.every( (val, i, arr) => val === arr[0] )){
//same values
points = points+300;
alert("congratulation you have won "+300+ "points")
   document.getElementById("points").innerHTML = points
 
}else if(matches.length == 3){
//sequence
points = points+800;
alert("congratulation you have won "+800+ "points")
   document.getElementById("points").innerHTML = points

}else if (x.every( (val, i, arr) => val === arr[0] )){
//all values are same
points = points+1000;
alert("congratulation you have won "+1000+ "points")
   document.getElementById("points").innerHTML = points
}else{
alert("better luck next time!");
}
  }
  }