2020年2月2日 星期日

功能:設計sw1~sw4

/*
    1.開機led閃三下
    2.sw1右移
    3.sw2左移
    4.sw3內縮外放
    5.漸近燈
*/
int ledPin[] = {2, 3, 4, 5, 6, 7, 8, 9};
int bootTable1[] = {0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00,};
int pilliTable1[] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01, 0x00};
int pilliTable2[] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x00};
int pilliTable3[] = {0x081, 0x42, 0x24, 0x18, 0x24, 0x42, 0x81, 0x00};
int pilliTable4[] = {0x080, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff, 0x00};
int buttonPin[] = {10, 11, 12, 13};
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int num1 = 0;
int num2 = 0;
int num3 = 0;
int num4 = 0;
int num5 = 0;
int delaytime = 200;
int i, j;
void setup()
{
  for (i = 0; i < 8; i++)
  {
    pinMode(ledPin[i], OUTPUT);
  }
  for (j = 0; j < 4; j++)
  {
    pinMode(buttonPin[j], INPUT);
  }
  for (num5 = 0; num5 < 6; num5++)
  {
    out(bootTable1[num5]);
    delay(delaytime);
  }
}
void loop()
{
  buttonState1 = digitalRead(buttonPin[0]);
  buttonState2 = digitalRead(buttonPin[1]);
  buttonState3 = digitalRead(buttonPin[2]);
  buttonState4 = digitalRead(buttonPin[3]);
  if (buttonState1 == LOW)
  {
    for (num1 = 0; num1 < 9; num1++)
    {
      out(pilliTable1[num1]);
      delay(delaytime);
    }
  }
  else if (buttonState2 == LOW)
  {
    for (num2 = 0; num2 < 9; num2++)
    {
      out(pilliTable2[num2]);
      delay(delaytime);
    }
  }
  else if (buttonState3 == LOW)
  {
    for (num3 = 0; num3 < 8; num3++)
    {
      out(pilliTable3[num3]);
      delay(delaytime);
    }
  }
  else if (buttonState4 == LOW)
  {
    for (num4 = 0; num4 < 9; num4++)
    {
      out(pilliTable4[num4]);
      delay(delaytime);
    }
  }
}
void out(byte data)
{
  for (j = 0; j < 7; j++)
  {
    if (data % 2 == 1)
      digitalWrite(ledPin[j], HIGH);//
    else
      digitalWrite(ledPin[j], LOW);
    data = data / 2;
  }
}
法二:
int ledPin[] = {2, 3, 4, 5, 6, 7, 8, 9};
int bootTable1[] = {0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00,};
int pilliTable1[] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01, 0x00};
int pilliTable2[] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x00};
int pilliTable3[] = {0x081, 0x42, 0x24, 0x18, 0x24, 0x42, 0x81, 0x00};
int pilliTable4[] = {0x080, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff, 0x00};
int buttonPin[] = {10, 11, 12, 13};
int buttonState[] = {0,0,0,0};
int num1 = 0;
int num2 = 0;
int num3 = 0;
int num4 = 0;
int num5 = 0;
int delaytime = 200;
int i, j;
void setup()
{
  for (i = 0; i < 8; i++)
  {
    pinMode(ledPin[i], OUTPUT);
  }
  for (j = 0; j < 4; j++)
  {
    pinMode(buttonPin[j], INPUT);
  }
  for (num5 = 0; num5 < 6; num5++)
  {
    out(bootTable1[num5]);
    delay(delaytime);
  }
  for (i = 0; i < 3; i++)
  {
    tone(14, 3000, 100); delay(delaytime);
  }
}
void loop()
{
  buttonState[0] = digitalRead(buttonPin[0]);
  buttonState[1] = digitalRead(buttonPin[1]);
  buttonState[2] = digitalRead(buttonPin[2]);
  buttonState[3] = digitalRead(buttonPin[3]);
  if (buttonState[0] == LOW)
  {
    for (num1 = 0; num1 < 9; num1++)
    {
      out(pilliTable1[num1]);
      delay(delaytime);
    }
  }
  else if (buttonState[1] == LOW)
  {
    for (num2 = 0; num2 < 9; num2++)
    {
      out(pilliTable2[num2]);
      delay(delaytime);
    }
  }
  else if (buttonState[2] == LOW)
  {
    for (num3 = 0; num3 < 8; num3++)
    {
      out(pilliTable3[num3]);
      delay(delaytime);
    }
  }
  else if (buttonState[3] == LOW)
  {
    for (num4 = 0; num4 < 9; num4++)
    {
      out(pilliTable4[num4]);
      delay(delaytime);
    }
  }
}
void out(byte data)
{
  for (j = 0; j < 7; j++)
  {
    if (data % 2 == 1)
      digitalWrite(ledPin[j], HIGH);//
    else
      digitalWrite(ledPin[j], LOW);
    data = data / 2;
  }
}

沒有留言:

張貼留言

algorithm

 #include <iostream> #include <string.h> using namespace std; int main(int argc, char** argv)  { for(int j=2;j<=100;j++)//j...