2021年11月30日 星期二

逆時針轉3圈就停

 unsigned long time1 = 0;

unsigned long time2 = 0;

int seg7_pin[] = {2, 3, 4, 5, 6, 7, 8};//七段顯示器腳位設為一維陣列

char seg7_table[] = {0xfe, 0xfd, 0xfb, 0xf7, 0xef, 0xdf, 0xff};//seg7_table[bcd[0]]

//                   點亮a 點亮b 點亮c 點亮d 點亮e 點亮f

int scan_line[] = {9, 10, 11, 12}; //com1 com0 scan_lin[0]=存放第9支腳 scan_lin[1]=存放第10支腳

char bcd[] = {6, 6, 6, 6 }; //一開機,先清除畫面

int j = 0;//j參數是設掃瞄線,因為兩位數,and cnt bcd[0]=0 bcd[1]=6陣列元素=0 陣列元素=1設定七段顯示用的字型,利用陣列中的陣列

int cnt;

int num = 0; //每切換一次奇變偶,偶變奇 一開機就順時針

//-----------------------------------------------------------------------------

void setup()

{

  pinMode(13, INPUT);//設定13腳為輸入

  digitalWrite(13, INPUT_PULLUP); //13腳為輸入提升電阻

  for (int i = 0; i < 8; i++)//七段顯示器腳位,用迴圈指定腳位為輸出

    pinMode(seg7_pin[i], OUTPUT);//資料端, a b c d e f g

  for (int i = 0; i < 4; i++)//com0 com1 用迴圈指定掃瞄腳位為輸出

    pinMode(scan_line[i], OUTPUT);//掃瞄端

  Serial.begin(9600);

}

//-------------------------------------------------------------------------------

void outport(byte data)//七段顯示器 a b c d e f某一段字型副程式 data是十進位,因為宣告byte 0~255

{

  for (int j = 0; j < 7; j++)//abcdefg用loop跑7次

  {

    if (data % 2 == 1) // 取出 data 的最低位元 , 從最低位元開始取,每取一次就除以2,共取7次就給束

      digitalWrite(seg7_pin[j], LOW);

    else

      digitalWrite(seg7_pin[j], HIGH);

    data = data / 2; // 十進制整數是連除  除以2,進行下一位元的處理

  }

}

//-----------------------------------------------------------------------------

void loop()

{

  if (millis() - time1 <= 40000 && millis() - time2 <= 40000)//跑三圈只是加此行條件同時滿足即可, 條件成立就執行,不成立就跳出來

  {

    if (millis() - time1 > 5) //超過5ms=0.005秒掃瞄scan_line及秀字

    { //每隔5ms就跑進來一次,使用陣列的陣列

      outport(seg7_table[bcd[j]]);//傳送seg7_table[bcd[0或1]=0 1 2 3 4 5 6]

      Serial.println(j);

      digitalWrite(scan_line[j], HIGH);//exe j=0 個位數、十位數  送高電位再送低電位因為掃瞄線是5m進來一次,七段是1秒進來一次,

      delay(1);

      digitalWrite(scan_line[j], LOW);

      //0先跑一遍,j==1不成立,j++再從上面右大括號電上往下跑一遍,

      if (j == 4)//scan_line[0]第9支腳 scan_line[1]第10支腳

      {

        j = 0;//掃瞄是從右到左

      }

      else

      {

        j++;

      }

    }

    if (millis() - time2 >= 1000) //超過1秒,計數值cnt加1

    {

      time2 = millis();

      if (num % 2 == 0)//偶數的話就cnt加加0123456

      {

        if (cnt == 12) cnt = 0;

        else  cnt++;//順逆時針的變數一樣,即可以切換

      }

      else//偶數的話就cnt--0123456

      {

        if (cnt == 0) cnt = 12;

        else cnt--;

      }

    }//兩個七段幾乎同時顯示,因為速度很快, 使用一維陣列,總共要8次變化

    //每隔1秒,計數值cnt就加1,bcd[1]=

    if (cnt == 0) {

      bcd[3] = 6 ;    //-

      bcd[2] = 6;

      bcd[1] = 6 ;

      bcd[0] = 0;

    }

    if (cnt == 1) {

      bcd[3] = 6 ;    //  -

      bcd[2] = 6;

      bcd[1] = 0 ;

      bcd[0] = 6;

    }

    if (cnt == 2) {

      bcd[3] = 6 ;    //    -

      bcd[2] = 0;

      bcd[1] = 6 ;

      bcd[0] = 6;

    }

    if (cnt == 3) {

      bcd[3] = 0 ;    //      -

      bcd[2] = 6;

      bcd[1] = 6 ;

      bcd[0] = 6;

    }

    if (cnt == 4) {

      bcd[3] = 5 ;    //      |

      bcd[2] = 6;

      bcd[1] = 6 ;

      bcd[0] = 6;

    }

    if (cnt == 5) {

      bcd[3] = 4 ;    //      |

      bcd[2] = 6;

      bcd[1] = 6 ;

      bcd[0] = 6;

    }

    if (cnt == 6) {

      bcd[3] = 3 ;    //    _

      bcd[2] = 6;

      bcd[1] = 6 ;

      bcd[0] = 6;

    }

    if (cnt == 7) {

      bcd[3] = 6 ;    //  _

      bcd[2] = 3;

      bcd[1] = 6 ;

      bcd[0] = 6;

    }

    if (cnt == 8) {

      bcd[3] = 6 ;    //_

      bcd[2] = 6;

      bcd[1] = 3 ;

      bcd[0] = 6;

    }

    if (cnt == 9) {

      bcd[3] = 6 ;    //|

      bcd[2] = 6;

      bcd[1] = 6 ;

      bcd[0] = 3;

    }

    if (cnt == 10) {

      bcd[3] = 6 ;    //|

      bcd[2] = 6;

      bcd[1] = 6 ;

      bcd[0] = 2;

    }

    if (cnt == 11) {

      bcd[3] = 6 ;    //|

      bcd[2] = 6;

      bcd[1] = 6 ;

      bcd[0] = 1;

    }

  }

}

沒有留言:

張貼留言

algorithm

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