一千萬個為什麽

搜索

通過鍵盤控制LED延遲

我正在嘗試根據鍵盤輸入的延遲打開LED,同時將其關閉,但LED沒有響應。這是我正在使用的代碼:

#include 

const byte ROWS = 4;//four Rows
const byte COLS = 3;//three columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = {13,10,9,8}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {7,6,1}; //connect to the column pinouts of the keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );


int x;
int y;
int z;
int e ; 
int w;
int r;
int Pin1 = A0;
int Pin2 = A1;
int Pin3 = A2;


void setup() {
 //initialize pins A0,A1 and A2 as an output.
  pinMode(A0, OUTPUT);
  pinMode(A1, OUTPUT);
  pinMode(A2, OUTPUT);

}

void loop ()
{
  char key = kpd.getKey();

l1:  if (key) //Check for a valid key.
  {
    switch(key)
    {
      case '1':
      w=1;
      break;
      case '2':
      w=2;
      break;
      case '3':
      w=3;
      break;
      case'4':
      w=4;
      break;
      case '5':
      w=5;
      break;
      case '6':
      w=6;
      break;
      case '7':
      w=7;
      break;
      case '8':
      w=8;
      break;
      case '9':
      w=9;
      break;
    }
  }
  else { goto l1;}//To guarantee that a key is pressed before moving on with the rest of code

  delay(w*1000*60); //To reach the time specified by the user before moving on
  digitalWrite(A2,HIGH);

l2: if (key)
{
  switch (key)
  {
    case '1':
        r=1;
        break;
        case '2':
        r=2;
        break;
        case '3':
        r=3;
        break;
        case '4':
        r=4;
        break;
        case '5':
        r=5;
        break;
        case '6':
        r=6;
        break;
        case '7':
        r=7;
        break;
        case '8':
        r=8;
        break;
        case '9':
        r=9;
        break;
      }
}
else {goto l2;}//To guarantee that a key is pressed before moving n with the rest of code

delay ((r-w)*1000*60); //To reach the time specified by the user before moving on
digitalWrite (A2,LOW);

l3: if (key)
{
  switch(key)
  {
    case '1':
        x=1;
        break;
        case '2':
        x=2;
        break;
        case '3':
        x=3;
        break;
        case '4':
        x=4;
        break;
        case '5':
        x=5;
        break;
        case '6':
        x=6;
        break;
        case '7':
        x=7;
        break;
        case '8':
        x=8;
        break;
        case '9':
        x=9;
        break;
      }
}

else { goto l3;}//To guarantee that a key is pressed before moving n with the rest of code
delay ((x-r)*1000*60);//To reach the time specified by the user before moving on
digitalWrite (A0,HIGH);

l4: if (key)
{
  switch (key)
  {
    case '1':
        y=1;
        break;
        case '2':
        y=2;
        break;
        case '3':
        y=3;
        break;
        case '4':
        y=4;
        break;
        case '5':
        y=5;
        break;
        case '6':
        y=6;
        break;
        case '7':
        y=7;
        break;
        case '8':
        y=8;
        break;
        case '9':
        y=9;
        break;
      }
}
else {goto l4;}//To guarantee that a key is pressed before moving n with the rest of code
delay((y-x)*1000*60); //To reach the time specified by the user before moving on
digitalWrite (A0, LOW );

l5:
if(key)
{
  switch(key)
  {
    case '1':
      z=1;
      break;
      case '2':
      z=2;
      break;
      case '3':
      z=3;
      break;
      case'4':
      z=4;
      break;
      case '5':
      z=5;
      break;
      case '6':
      z=6;
      break;
      case '7':
      z=7;
      break;
      case '8':
      z=8;
      break;
      case '9':
      z=9;
      break;
    }
}
else {goto l5;}// To guarantee that a key is pressed before moving n with the rest of code
delay ((z-y)*1000*60); //To reach the time specified by the user before moving on
digitalWrite(A1,HIGH);

l6: if (key)
{
  switch(key)
  {
    case '1':
        e=1;
        break;
        case '2':
        e=2;
        break;
        case '3':
        e=3;
        break;
        case '4':
        e=4;
        break;
        case '5':
        e=5;
        break;
        case '6':
        e=6;
        break;
        case '7':
        e=7;
        break;
        case '8':
        e=8;
        break;
        case '9':
        e=9;
        break;
  }
}
else{goto l6;}// To guarantee that a key is pressed before moving n with the rest of code
delay((e-z)*1000*60); //To reach the time specified by the user before moving on
digitalWrite (A1, LOW );

}

最佳答案

暫時沒有最佳答案

轉載註明原文: 通過鍵盤控制LED延遲