2021年8月1日 星期日

btn1(按一下加1+buzzer) btn2(按一下減1+buzzer) 7seg

int seg7_ping[]={2,3,4,5,6,7,8};

int btn1=9;

int btn2=10;

int i;

int j;

int k;

int buzzer=11;

char seg7_table[]={~0xc0,~0xf9,~0xa4,~0xb0,~0x99,~0x92,~0x82,~0xf8,~0x80,~0x90};

int val1;

int val2;

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

void setup() 

{

 for(i=0;i<7;i++)

 {

   pinMode(seg7_ping[i],OUTPUT);

 }

 pinMode(btn1,INPUT);

 pinMode(btn2,INPUT);

 pinMode(buzzer,OUTPUT);

}

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

void outport(char data)

{

  for(k=0;k<7;k++)

  {

    if(data % 2 == 1)

      digitalWrite(seg7_ping[k],HIGH);

    else

      digitalWrite(seg7_ping[k],LOW);

    data = data / 2;

  }

}

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

void loop()

{

 val1=digitalRead(btn1);

 val2=digitalRead(btn2);

 if(val1==LOW)

 {

  delay(1000);

  while(digitalRead(btn1)==0);

  tone(buzzer,500,500);delay(1000);

  if(j==9)

   j=0;

  else

   j++;

 }

 if(val2==LOW)

 {

  delay(1000);

  while(digitalRead(btn2)==0);

  tone(buzzer,500,500);delay(1000);

  if(j==0)

   j=9;

  else

   j--;

 }

 outport(seg7_table[j]);

}

沒有留言:

張貼留言

algorithm

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