mak4000 发表于 2018-11-2 21:52:28

总是计算错误。第3次输入就少一个数

总是计算错误。第3次输入就少一个数
我要作一个密码锁在在转化数字的时候总是出错
代码
#include <Keypad.h>
long pass_in;//输入的密码
long pass_rom=453452;//芯片储存密码
int pass_bit=6;//密码位数
int pass_mun;//转换数字后的一位密码
int i=0;



const byte ROWS = 4; //四行
const byte COLS = 4; //四列
//定义键盘上的按键标识
char hexaKeys = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins = {6,7,8,9}; //连接到行扫描的输入输出端口
byte colPins = {10,11,12}; //连接到列扫描的输入输出端口


//定义Keypad类的实例
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);


void setup(){
Serial.begin(9600);
}


void loop(){
char customKey = customKeypad.getKey();
if (customKey){
pass_mun = long( customKey ) -48;
    Serial.println(customKey);
   if( i != (pass_bit-1) ) {//   
      pass_in = pass_in +( pass_mun * pow(10,i));
   
      Serial.print(" pow(10,i)---");
      Serial.println( pow(10,i));
       i++;
      
      }
      else
      {
       i=0;
       if(pass_in == pass_rom ){
         digitalWrite(2,HIGH);//启动继电器
         delay(400);
         digitalWrite(2,LOW);//关闭继电器
      }
      else
      {//密码错误
          pass_in=0;
          //下面是错误处理
         
          }
      
      }
    Serial.print("pass_in---");
    Serial.println(pass_in);
    Serial.print("i---");
    Serial.println(i);
}
}


串口输出:
4
pow(10,i)---1.00
pass_in---4
i---1
2
pow(10,i)---10.00
pass_in---24
i---2
5
pow(10,i)---100.00
pass_in---524
i---3
3
pow(10,i)---1000.00
pass_in---3523
i---4
9
pow(10,i)---10000.00
pass_in---93522
i---5
4
pass_in---0
i---0

pass_in---的值,头3个按键正常。从第三关开始就减1.

请问大神们是什么问题。这单片机太不靠谱了
!!!!!!!!!!!!!!

页: [1]
查看完整版本: 总是计算错误。第3次输入就少一个数