极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 14428|回复: 0

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

[复制链接]
发表于 2018-11-2 21:52:28 | 显示全部楼层 |阅读模式
总是计算错误。第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[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = {6,7,8,9}; //连接到行扫描的输入输出端口
byte colPins[COLS] = {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.

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

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-3-29 10:02 , Processed in 0.038018 second(s), 17 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表