为什么我的arduino程序突然停止工作?

时间:2019-11-14 17:19:51

标签: arduino

因此,我有一个要为学校设计的项目,我们必须从头开始制作一个arduino项目。我对arduino和他的编码不熟悉,因此做了一个简单但有趣的项目。我的想法是在必须输入钥匙的地方制作一种保险箱,然后,使用电位计进行正确组合以打开保险箱。我不知道我做错了什么,但是当我将其上传到arduino时,程序到达了第一行并在将第一行打印到串行监视器后停止了。程序被卡住了,什么也没做。奇怪的是,它在5分钟前就可以工作了,但现在却无法使用。谁能帮我吗?我的截止日期是几个小时,我无法解决...这是代码和示意图:

Picture of the setup. The 2 resistors on the left are 1k, the other 2 are 10k.

//variabelen
const int pot = A0;
const int pinA = 6;
const int pinB = 5;
const int pinC = 2;
const int pinD = 3;
const int pinE = 4;
const int pinF = 7;
const int pinG = 8;
const int pinP = 9;
const int enter = 11;
const int reset = 12;
byte enterState = 0;
byte lastEnterState = 0;
byte resetState = 0;
byte lastResetState = 0;
int enterTimer = 0;
String wachtwoord;
String ingegeven = "";

//setup
void setup(){
  pinMode(pinA, OUTPUT);
  pinMode(pinB, OUTPUT);
  pinMode(pinC, OUTPUT);
  pinMode(pinD, OUTPUT);
  pinMode(pinE, OUTPUT);
  pinMode(pinF, OUTPUT);
  pinMode(pinG, OUTPUT);
  pinMode(pinP, OUTPUT);
  pinMode(enter, INPUT);
  pinMode(reset, INPUT);
  Serial.begin(9600);
  Serial.println("Geef een wachtwoord op voor de safe.");
  wachtwoord = Serial.read();
}

//eigen methoden


void dispChar(int getal){
  switch(getal){
    case 0:
      digitalWrite(pinA, LOW);   
      digitalWrite(pinB, LOW);   
      digitalWrite(pinC, LOW);   
      digitalWrite(pinD, LOW);   
      digitalWrite(pinE, LOW);   
      digitalWrite(pinF, LOW);   
      digitalWrite(pinG, HIGH);
      break;

    case 1:
      digitalWrite(pinA, HIGH);   
      digitalWrite(pinB, LOW);   
      digitalWrite(pinC, LOW);   
      digitalWrite(pinD, HIGH);   
      digitalWrite(pinE, HIGH);   
      digitalWrite(pinF, HIGH);   
      digitalWrite(pinG, HIGH);
      break;

    case 2:
      digitalWrite(pinA, LOW);   
      digitalWrite(pinB, LOW);   
      digitalWrite(pinC, HIGH);   
      digitalWrite(pinD, LOW);   
      digitalWrite(pinE, LOW);   
      digitalWrite(pinF, HIGH);   
      digitalWrite(pinG, LOW);
      break;

    case 3:
      digitalWrite(pinA, LOW);   
      digitalWrite(pinB, LOW);   
      digitalWrite(pinC, LOW);   
      digitalWrite(pinD, LOW);   
      digitalWrite(pinE, HIGH);   
      digitalWrite(pinF, HIGH);   
      digitalWrite(pinG, LOW);
      break;

    case 4:
      digitalWrite(pinA, HIGH);   
      digitalWrite(pinB, LOW);   
      digitalWrite(pinC, LOW);   
      digitalWrite(pinD, HIGH);   
      digitalWrite(pinE, HIGH);   
      digitalWrite(pinF, LOW);   
      digitalWrite(pinG, LOW);
      break;

    case 5:
      digitalWrite(pinA, LOW);   
      digitalWrite(pinB, HIGH);   
      digitalWrite(pinC, LOW);   
      digitalWrite(pinD, LOW);   
      digitalWrite(pinE, HIGH);   
      digitalWrite(pinF, LOW);   
      digitalWrite(pinG, LOW);
      break;

    case 6:
      digitalWrite(pinA, LOW);   
      digitalWrite(pinB, HIGH);   
      digitalWrite(pinC, LOW);   
      digitalWrite(pinD, LOW);   
      digitalWrite(pinE, LOW);   
      digitalWrite(pinF, LOW);   
      digitalWrite(pinG, LOW);
      break;

    case 7:
      digitalWrite(pinA, LOW);   
      digitalWrite(pinB, LOW);   
      digitalWrite(pinC, LOW);   
      digitalWrite(pinD, HIGH);   
      digitalWrite(pinE, HIGH);   
      digitalWrite(pinF, HIGH);   
      digitalWrite(pinG, HIGH);
      break;

    case 8:
      digitalWrite(pinA, LOW);   
      digitalWrite(pinB, LOW);   
      digitalWrite(pinC, LOW);   
      digitalWrite(pinD, LOW);   
      digitalWrite(pinE, LOW);   
      digitalWrite(pinF, LOW);   
      digitalWrite(pinG, LOW);
      break;

    case 9:
      digitalWrite(pinA, LOW);   
      digitalWrite(pinB, LOW);   
      digitalWrite(pinC, LOW);   
      digitalWrite(pinD, LOW);   
      digitalWrite(pinE, HIGH);   
      digitalWrite(pinF, LOW);   
      digitalWrite(pinG, LOW);  
      break;
  }
}

//Kijk of enter knop is ingedrukt
void onEnter(){
  enterState = digitalRead(enter);
  if(enterState == HIGH){
    enterTimer++;
    if(enterState != lastEnterState){
      ingegeven += map(analogRead(pot), 0, 1023, 0, 9);
      Serial.println(ingegeven);
      enterTimer = 0;
    }
  }
  delay(50); //Voorkomt bounce.
  if(enterTimer > 50){
    Serial.println("\nRESET");
    enterTimer = 0;
    ingegeven = "";
  }
  lastEnterState = enterState;
  check();
}

bool check(){
  if(ingegeven.equals(wachtwoord)){
    return true;
  } else{
    if(ingegeven.length() > wachtwoord.length()){
      Serial.println("FOUT!");
      ingegeven = "";
      return false;
    }
  }
}

//Kijk of reset knop is ingedrukt
void onReset(){
  resetState = digitalRead(reset);

  if(resetState != lastResetState){
    if(resetState == HIGH){
      ingegeven = "";
      Serial.println("Geef een wachtwoord op voor de safe.");
      wachtwoord = Serial.read();

    }
  }
  delay(50); //Voorkomt bounce.
  lastResetState = resetState;
}

//loop
void loop(){
  while(!check()){
    int getal = map(analogRead(pot), 0, 1023, 0, 9);
    digitalWrite(pinP, HIGH);
    dispChar(getal);
    onEnter();
    onReset();
  }
  Serial.println("CODE CORRECT!")
}

感谢您的帮助!

enter image description here

0 个答案:

没有答案