2019年6月25日 星期二

4X4 電路 bdata flag


#include <at89x52.h>
unsigned char ptn_table[]={0Xc0,0Xf9,0Xa4,0Xb0,0X99,0X92,0X82,0Xf8,0X80,0X90};
unsigned char time_500ms=10;
char disp_no;
unsigned char  bdata flag;
sbit up_flag=flag^0;
sbit down_flag=flag^1;
sbit stop_flag=flag^2;
void display(unsigned char num)
{
P0=num;
}
void delay_1ms(unsigned int k)
{
  unsigned int i,j;
  for (i=0;i<=k;i++)
    for (j=0;j<=110;j++);
}

void keyup(){
delay_1ms(300);
!=:不等於為關係運算式
while((P2|0xf0)!=0xff);
delay_1ms(300);
}
void timer_set()
{
TMOD=0X01; //T0 MODE 1
TL0=(65536-50000)%256;
TH0=(65536-50000)/256;
IE=0x82;
TR0=1;
}
void T0_INT(void) interrupt 1
{
TL0=(65536-50000)%256;
TH0=(65536-50000)/256;
time_500ms--;
if(time_500ms==0)
{
time_500ms=10;
選擇性敘述:很多個條件(些例為3個條件停止、上數、下數),選擇一個的需求條件來做
if(條件式1)動作1;條件式不成立就往下判斷
else if(條件式2)成立就動作2,做完就跳出來;
else if(否則條件式3成立)就動作3就跳出來;
else 動作4(若上面的動作都不成立就做動作4,如果你的條件沒有以上皆非就不需要此else);
if(stop_flag)
{
}
else if(up_flag)
{
disp_no++;
      if(disp_no>9)
{
disp_no=0;
}
display(ptn_table[disp_no]);
delay_1ms(500);
}
else if(down_flag)
{
disp_no--;
      if(disp_no<0)
{
disp_no=9;
}
display(ptn_table[disp_no]);
delay_1ms(500);
}
}
}
main進入主程式
void main()
{
timer_set();
P0=0xff;
while(1){
P2=0x7f;
最常用選擇性敘述:條件式成立 執行兩個statement (超過兩個statement要大括號)
==:為關係運算式\
if(P2_3==0){flag=0;P0=0xc0;disp_no=0;keyup();}//0
if(P2_2==0){flag=0;P0=0xf9;disp_no=1;keyup();}//1
 if(P2_1==0){flag=0;P0=0xa4;disp_no=2;keyup();}//2
 if(P2_0==0){flag=0;P0=0xb0;disp_no=3;keyup();}//3 
P2=0xbf;
if(P2_3==0){flag=0;P0=0x99;disp_no=4;keyup();}//4
if(P2_2==0){flag=0;P0=0x92;disp_no=5;keyup();}//5
 if(P2_1==0){flag=0;P0=0x82;disp_no=6;keyup();}//6
 if(P2_0==0){flag=0;P0=0xf8;disp_no=7;keyup();}//7
P2=0xdf;
if(P2_3==0){flag=0;P0=0x80;disp_no=8;keyup();}//8
if(P2_2==0){flag=0;P0=0x90;disp_no=9;keyup();}//9
 if(P2_1==0){flag=0;up_flag=1;keyup();}//a
 if(P2_0==0){flag=0;down_flag=1;keyup();}//b
P2=0xef;
if(P2_3==0){keyup();stop_flag=0;}//c
if(P2_2==0){keyup();stop_flag=1;}//d

 }
}

108學年資訊科實作試題_網路管理_數位電路


資訊科實作試題
一、網路管理設計(25%)
()(10)使用工具壓製一條Cat5e 網路線採EIA568b方式壓製,需測試通過才給分。
  
() (90)一部電腦,本機安裝windows server2012(usb安裝)Virtualbox上安裝Fedora core12(光碟安裝)
1.         Windows server2012
(1)建立使用者帳號(須具備administrator權限)admin密碼:A@108user1密碼:B@102
(2)IP192.168.1.1
(3)電腦名稱改為Teacher0615
(4)DNS: ftp:192.168.x.xmail:192.168.x.x
(5)DHCP: 教學大樓:192.168.x.x~192.168.x.x
      行政大樓:192.168.x.x~192.168.x.x
(6)IIS:Fedora透過瀏覽器連線(才給分),能開啟IIS預設網頁
(7)網頁: <h3> <font color=red> 工作崗位:xx 考試日期:xx 姓名:xx
2.         Fedora core12
(1)建立使用者帳號(須具備root權限root密碼同下述)admin
密碼:A@10803
(2)IP192.168.x.x
(3)可連線到server2012

二、數位邏輯實習(25%)
 請設計一同步計數器00->10->01->11->00…
1.         555輸出頻率0.5~2hz,可用LED觀看。
2.         同步計數器顯示00->10->01->11->00…可用LED觀看。

2019年6月24日 星期一

btn + tone

http://www.powenko.com/wordpress/digital-%E6%95%B8%E4%BD%8D%E8%BC%B8%E5%85%A5%E8%88%87%E8%BC%B8%E5%87%BA/
byte led1Pin = 8;
byte led2Pin = 9;
byte bnt1Pin = 2;
byte bnt2Pin = 3;
boolean state1;
boolean state2;
boolean buttonUp1;
boolean buttonUp2;
void setup()
{
  pinMode(led1Pin, OUTPUT);
  pinMode(led2Pin, OUTPUT);
  digitalWrite(led1Pin, HIGH);
  digitalWrite(led2Pin, HIGH);
  pinMode(bnt1Pin, INPUT);
  pinMode(bnt2Pin, INPUT);
  digitalWrite(bnt1Pin, HIGH);
  digitalWrite(bnt2Pin, HIGH);
}
void loop()
{
  if(digitalRead(bnt1Pin) == LOW && buttonUp1 == true)
  {
    state1 = !state1;
    digitalWrite(led1Pin, state1);
    tone(10,500,500);
    buttonUp1 = false;
  }
  else if(digitalRead(bnt1Pin) == HIGH && buttonUp1 == false)
  {
    buttonUp1 = true;
  }

  if(digitalRead(bnt2Pin) == LOW && buttonUp2 == true)
  {
    state2 = !state2;
    digitalWrite(led2Pin, state2);
    tone(10,500,500);
    buttonUp2 = false;
  }
  else if(digitalRead(bnt2Pin) == HIGH && buttonUp2 == false)
  {
    buttonUp2 = true;
  }
  delay(10); // Delay 時間可視情況調整
}

兩個按鍵_debounce

byte led1Pin = 8;
byte led2Pin = 9;
byte bnt1Pin = 2;
byte bnt2Pin = 3;
boolean state1;
boolean state2;
boolean buttonUp1;
boolean buttonUp2;
void setup()
{
  pinMode(led1Pin, OUTPUT);
  pinMode(led2Pin, OUTPUT);
  digitalWrite(led1Pin, HIGH);
  digitalWrite(led2Pin, HIGH);
  pinMode(bnt1Pin, INPUT);
  pinMode(bnt2Pin, INPUT);
  digitalWrite(bnt1Pin, HIGH);
  digitalWrite(bnt2Pin, HIGH);
}
void loop()
{
  if(digitalRead(bnt1Pin) == LOW && buttonUp1 == true)
  {
    state1 = !state1;
    digitalWrite(led1Pin, state1);
    buttonUp1 = false;
  }
  else if(digitalRead(bnt1Pin) == HIGH && buttonUp1 == false)
  {
    buttonUp1 = true;
  }
 
  if(digitalRead(bnt2Pin) == LOW && buttonUp2 == true)
  {
    state2 = !state2;
    digitalWrite(led2Pin, state2);
    buttonUp2 = false;
  }
  else if(digitalRead(bnt2Pin) == HIGH && buttonUp2 == false)
  {
    buttonUp2 = true;
  }
  delay(10); // Delay 時間可視情況調整
}

按鈕_防彈跳

byte ledPin = 8;
byte bntPin = 7;
boolean state;
boolean buttonUp;
void setup()
{
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  pinMode(bntPin, INPUT);
  digitalWrite(bntPin, HIGH);
}
void loop()
{
  if(digitalRead(bntPin) == LOW && buttonUp == true)
  {
    state = !state;
    digitalWrite(ledPin, state);
    buttonUp = false;
  }
  else if(digitalRead(bntPin) == HIGH && buttonUp == false)
  {
    buttonUp = true;
  }
  delay(10); // Delay 時間可視情況調整
}

2019年6月22日 星期六

step motor

https://atceiling.blogspot.com/2013/04/arduino.html
https://www.youtube.com/watch?v=GyEa5MPJw_A


int apin=8;
int bpin=9;
int cpin=10;
int dpin=11;
int delaytime=10;
void setup()
{
  // put your setup code here, to run once:
  pinMode(apin,OUTPUT);
  pinMode(bpin,OUTPUT);
  pinMode(cpin,OUTPUT);
  pinMode(dpin,OUTPUT);
}

void loop()
{
  // put your main code here, to run repeatedly:
  digitalWrite(apin,HIGH);delay(delaytime);digitalWrite(apin,LOW);
  digitalWrite(bpin,HIGH);delay(delaytime);digitalWrite(bpin,LOW);
  digitalWrite(cpin,HIGH);delay(delaytime);digitalWrite(cpin,LOW);
  digitalWrite(dpin,HIGH);delay(delaytime);digitalWrite(dpin,LOW);
}

rfid

https://atceiling.blogspot.com/2017/05/arduino-rfid.html

IR

杜邦線用久了,接觸會不良
/*
 * IRremote: IRrecvDemo - demonstrates receiving IR codes with IRrecv
 * An IR detector/demodulator must be connected to the input RECV_PIN.
 * Version 0.1 July, 2009
 * Copyright 2009 Ken Shirriff
 * http://arcfn.com
 */

#include <IRremote.h>

int RECV_PIN = 11;

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
  Serial.begin(9600);
  // In case the interrupt driver crashes on setup, give a clue
  // to the user what's going on.
  Serial.println("Enabling IRin");
  irrecv.enableIRIn(); // Start the receiver
  Serial.println("Enabled IRin");
  pinMode(2,OUTPUT);
}

void loop()
{
  if (irrecv.decode(&results))
  {
    Serial.println(results.value,HEX);
    if(results.value==0XFF30CF)
    {
      digitalWrite(2,!digitalRead(2));
    }
    irrecv.resume(); // Receive the next value
  }
  delay(100);
}

例二:
#include <IRremote.h>
long sw1=0xFF30CF;
long sw2=0xFF18E7;
long sw3=0xFF7A85;
long sw4=0xFF10EF;
int i,j=0;
int RECV_PIN = 11; // 使用數位腳位2接收紅外線訊號
IRrecv irrecv(RECV_PIN); // 初始化紅外線訊號輸入
decode_results results; // 儲存訊號的結構
const int led[4]={2,3,4,5};
int key=0;

void setup()
{
  Serial.begin(9600);
  irrecv.blink13(true); // 設為true的話,當收到訊號時,腳位13的LED便會閃爍
  irrecv.enableIRIn(); // 啟動接收
  for(i=0;i<4;i++)
    pinMode(led[i],OUTPUT);
}

void loop()
{
  if (irrecv.decode(&results))          // 接收紅外線訊號並解碼
  {
    irrecv.resume();                   // 準備接收下一個訊號
    if(results.value==sw1)
       key=1;
    else if(results.value==sw2)
       key=2;
    else if(results.value==sw3)
       key=3;
  }
  for(i=0;i<4;i++)
    digitalWrite(led[i],LOW);
  if(key==1)
  {
    j++;
    if(j>3)
      j=0;
    digitalWrite(led[j],HIGH);
    delay(1000); 
  } 
  else if(key==2)
  {
    j--;
    if(j<0)
      j=3;
    digitalWrite(led[j],HIGH);
    delay(1000); 
  }
}

例三:
#include <IRremote.h>
long sw1=0xFF30CF;
long sw2=0xFF18E7;
long sw3=0xFF7A85;
long sw4=0xFF10EF;
int i;
int RECV_PIN = 11; // 使用數位腳位2接收紅外線訊號
IRrecv irrecv(RECV_PIN); // 初始化紅外線訊號輸入
decode_results results; // 儲存訊號的結構
const int led[4]={2,3,4,5};
void setup()
{
  Serial.begin(9600);
  irrecv.blink13(true); // 設為true的話,當收到訊號時,腳位13的LED便會閃爍
  irrecv.enableIRIn(); // 啟動接收
  for(i=0;i<4;i++)
    pinMode(led[i],OUTPUT);
}

void loop()
{
  if (irrecv.decode(&results))          // 接收紅外線訊號並解碼
  {
    irrecv.resume();                   // 準備接收下一個訊號
    if(results.value==sw1)
       digitalWrite(2,!digitalRead(2));
    else if(results.value==sw2)
       digitalWrite(3,!digitalRead(3));
    else if(results.value==sw3)
       digitalWrite(4,!digitalRead(4));
    else if(results.value==sw4)
       digitalWrite(5,!digitalRead(5));
  }
}

2019年6月18日 星期二

Incrementing Motor Speed Control

//Incrementing Motor Speed Control 一開始motor=9 const int MOTOR = 9; //Motor connected to digital pin 9 const int BUTTON = 2; //Button connected to pin 2 boolean lastButton = LOW; //Variable containing the previous //button state boolean currentButton = LOW; //Variable containing the current //button state int speedIncrement = 0; void setup() { pinMode (MOTOR, OUTPUT); //Set the MOTOR pin as an output pinMode (BUTTON, INPUT); //Set button as an input } /* * Debouncing Funtion * Pass it the previous button state, * and get back the current debounced button state. */ boolean debounce(boolean last) { boolean current = digitalRead(BUTTON); //Read the button state if (last != current) //if it's different { delay(5); //wait 5ms current = digitalRead(BUTTON); //read it again } return current; } /* *Motor Speed Selection *Pass a number for the Motor speed and set it */ void setSpeed(int increment) { //speed one if (increment == 1) { analogWrite(MOTOR, 110); } else if (increment == 2) { analogWrite(MOTOR, 150); } else if (increment == 3) { analogWrite(MOTOR, 180); } else if (increment == 4) { analogWrite(MOTOR, 200); } else if (increment == 5) { analogWrite(MOTOR, 255); } //OFF else { analogWrite(MOTOR, 0); } } void loop() { currentButton = debounce(lastButton); //read debounce state
&&:且為兩個條件組合起來 if (lastButton == LOW && currentButton == HIGH) //if it was pressed { speedIncrement++; //Increment motor speed } lastButton = currentButton; //reset button value //if cycled through all speed increments //reset the speed to 0 if (speedIncrement == 6) speedIncrement = 0; setSpeed(speedIncrement); }

2019年6月17日 星期一

積化和差






























9013


4x4鍵盤







































const byte colPins[4] = {9, 8, 7, 6};     // 設定「行」腳位
const byte rowPins[4] = {13, 12, 11, 10}; // 設定「列」腳位
const char keymap[4][4] = {     // 設定按鍵的「行、列」代表值
    {'1','2','3','A'},
    {'4','5','6','B'},
    {'7','8','9','C'},
    {'*','0','#','D'}
};

byte i, j;      // 暫存迴圈的索引數字
byte scanVal;   // 暫存掃描到的按鍵值

void setup(){
  Serial.begin(9600);

  for (i = 0; i < = 3; i++) {
    pinMode(rowPins[i], INPUT);
    pinMode(colPins[i], OUTPUT);
    digitalWrite(colPins[i], HIGH);
    digitalWrite(rowPins[i], HIGH);
  }
}

void loop() {
  for (i = 0; i <= 3; i++) {
    for (j = 0; j <= 3; j++) {
      digitalWrite(colPins[j], LOW);
      scanVal = digitalRead(rowPins[i]);

      if (scanVal == LOW) {    // 如果輸入值是「低電位」…
        Serial.println(keymap[i][j]);  // 輸出按鍵代表的字元
        delay(200);  // 掃描按鍵的間隔時間
        digitalWrite(colPins[j], HIGH);
        break;       // 跳出迴圈
      }
      digitalWrite(colPins[j], HIGH);
    }
  }
}
練習
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
int m,n;
int a[4][4]={
             {0,1,2,3},
{4,5,6,7},
{8,9,10,11},
{12,13,14,15}
    };
   for(m=0;m<4;m++)
    for(n=0;n<4;n++)
      printf("\na[%d][%d]=%d",m,n,a[m][n]);
return 0;

}

2019年6月15日 星期六

TM1637 4位 共陽 7段顯示器模組

本模組是一個12腳的帶時鐘點4位共陽數碼管(0.36英寸)所組成的顯示模組,驅動晶片為TM1637,只需2根信號線即可使單片機控制48段數碼管。
註:本模組為"帶時鐘點:"顯示需要程式控制才能呈現鐘點功能,可參考所附(取貨後露露通索取)範例資料
參數
電壓:3.3V/5V (工作範圍3.3~5.5V)
電流:30~80mA
數碼管:40.36英寸共陽紅色
亮度: 8級亮度可調
尺寸:42*24*12mm
控制介面:共4個引腳(GNDVCCDIOCLK),GND為地,VCC為供電電源,DIO為資料登錄輸出腳,CLK為時鐘信號腳;
定位孔:4M2螺絲定位孔,孔徑為2.2mm,使模組便於安裝定位,實現模組間組合;
範例簡介:
1. ClockDisplay:使用主板上MCUTimer1來計時,讓4位元數碼管顯示時間。
2. NumberFlow0~9,A,b,C,d,E,F16個字元從右往左流動。
3. StopWatch: 使用主板上MCUTimer1來計時,可配合按鍵實現碼錶功能。
模組與Arduino接線:
GND  ---  GND
VCC  ---  +5V
DIO  ---  D2
CLK  ---  D3

SOP(Small Out-Line Package小外形封裝)是一種很常見的元器件形式

CNY70























Arduino : CNY70

http://gsyan888.blogspot.com/2013/04/arduino-cny70.html

C9013


DC Motor Control With Arduino

https://www.youtube.com/watch?v=7bThJ-sm2eU






















const int potPin = A0;
const int fwdbuttonPin = 13;
const int bckbuttonPin = 12;
const int pin1 = 11;
const int pin2 = 10;

int potValue = 0;
int motorValue = 0;
int fwdbuttonState = 0;
int bckbuttonState = 0;

void setup()
{

 pinMode(fwdbuttonPin, INPUT_PULLUP);
 pinMode(bckbuttonPin, INPUT_PULLUP);
 pinMode (pin1, OUTPUT);
 pinMode (pin2, OUTPUT);

}

void loop()
{

 potValue = analogRead(potPin); 
 motorValue = map(potValue, 0, 1023, 0, 255);
 fwdbuttonState = digitalRead(fwdbuttonPin);
 bckbuttonState = digitalRead(bckbuttonPin);

 if (fwdbuttonState == LOW)
  {
 
   analogWrite(pin1, motorValue);
   digitalWrite (pin2, LOW); 
   
  }

 else if (bckbuttonState == LOW) 
  {
 
   analogWrite(pin2, motorValue);
   digitalWrite (pin1, LOW);
   
  }
 
  else
  {
 
   digitalWrite (pin1, LOW);
   digitalWrite (pin2, LOW); 

  }
 
}

Pot


2019年6月12日 星期三

relay + motor






































#define ctrlPin 8
int delaytime=1000;
void setup()
{
  // put your setup code here, to run once:
  pinMode(ctrlPin,OUTPUT);
}

void loop()
{
  // put your main code here, to run repeatedly:
  digitalWrite(ctrlPin,HIGH);delay(delaytime);
  digitalWrite(ctrlPin,LOW);delay(delaytime);
}
例:增加到130,馬達開始轉到255,0~125馬達不會轉
2.5V以下不會轉,2.5V才會轉
int relay_input_pin = 13;
int rotate = 0;   
int fadeAmount = 5;   
void setup()
{
  pinMode(relay_input_pin, OUTPUT);
  Serial.begin(9600);
}
void loop()
{
  analogWrite(relay_input_pin, rotate);
  Serial.println(rotate);
  rotate = rotate + fadeAmount;
  if (rotate >= 255) rotate = 0;
  delay(1000);
}

2019年6月10日 星期一

fan


#define IN1 6       // 宣告馬達控制信號輸入端1
#define IN2 7       // 宣告馬達控制信號輸入端2
void setup()
{
   pinMode(IN1, OUTPUT);     // 設定馬達控制腳1為輸出
   pinMode(IN2, OUTPUT);     // 設定馬達控制腳2為輸出
}
void loop()
 {
   digitalWrite(IN1,HIGH);     // 風扇正轉( 送風)
   digitalWrite(IN2,LOW);
   delay(5000);
   digitalWrite(IN1,HIGH);     // 風扇煞車
   digitalWrite(IN2,HIGH);
   delay(3000);
   digitalWrite(IN1,LOW);     // 風扇反轉( 吸風)
   digitalWrite(IN2,HIGH);
   delay(5000);
   digitalWrite(IN1,LOW);     // 風扇停止
   digitalWrite(IN2,LOW);
   delay(3000);
}

buzzer無源蜂鳴器























































int sp=2;
int i,j;
int notes[]={262,294,329,349,392,440,493,523};
int delaytime1=300;
int delaytime2=50;
void setup()
{
  for(i=0;i<8;i++)
  {
    tone(sp,notes[i],300);
    delay(delaytime1);
    noTone(sp);
    delay(delaytime2);
  }
}
void loop()
{
}

Power Jack


RELAY繼電器

const int relayPin=13; // 繼電器(Relay)
const int IN1=12; //IN1 輸入
int relayState=0; // 繼電器狀態
void setup() 
{
  Serial.begin(9600); // 開啟Serialport, 通訊速率為9600bps
  pinMode(relayPin,OUTPUT); // 把relayPin 設置成OUTPUT
}
void loop()
{
  Serial.println(“FAN is on”); // 風扇啟動
  digitalWrite(relayPin,HIGH);
  digitalWrite(IN1,HIGH);
  delay(5000);
  Serial.println(“FAN is off”); // 風扇停止
  digitalWrite(relayPin,LOW);
 digitalWrite(IN1,LOW);
  delay(5000);
}





2019年6月9日 星期日

TCRT 5000

int ledPin=8;
int inPin=2;
int val=0;
void setup()
{
 pinMode(ledPin,OUTPUT);
 pinMode(inPin,INPUT);
}
void loop()
 val=digitalRead(inPin);
 if(val==HIGH)
 {
  digitalWrite(ledPin,HIGH);
 } 
 else 
 {
  digitalWrite(ledPin,LOW); 
 }
}

2019年6月8日 星期六

紅外線接收器

#include <IRremote.h>
int RECV_PIN = 7;
byte led2Pin = 2;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
  Serial.begin(9600);
  Serial.println("Enabling IRin");
  irrecv.enableIRIn(); // Start the receiver
  Serial.println("Enabled IRin");
  pinMode(led2Pin,OUTPUT);
  digitalWrite(led2Pin,LOW);
}

void loop()
{
  if (irrecv.decode(&results))
  {
    if(results.value==0XFF30CF)
    {
      digitalWrite(led2Pin,!digitalRead(led2Pin));
    }
    Serial.println(results.value,HEX);
    irrecv.resume(); // Receive the next value
  }
  delay(100);
}
touch不良,開發板最好用公的 ,因為母的用久會鬆掉


七段顯示器








































例:按一下加1
byte seg7Pin[]={2,3,4,5,6,7,8};
byte seg7Table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
byte btnPin = 9 ;
int i,j;
int k=-1;
//boolean state = 0;//一開始沒有人按
boolean buttonUp = 1;//按鈕放開過了

void setup()
{
  for(i=0;i<7;i++)
  {
   pinMode(seg7Pin[i],OUTPUT);
   digitalWrite(seg7Pin[i],LOW);
  }
  pinMode(btnPin, INPUT);
  digitalWrite(btnPin, HIGH);
  Serial.begin(9600);
}
void outPort(byte data)
{
  for(j=0;j<7;j++)
  {
    if(data % 2 == 1)digitalWrite(seg7Pin[j],HIGH);
    else
    {
      digitalWrite(seg7Pin[j],LOW);
    }
    data=data/2;
  }
}
void loop()
{
//有人按了這顆按鈕 且 按鈕曾經被放開過即切換LED狀態
  if(digitalRead(btnPin) == 0 && buttonUp == 1)
  {
    buttonUp = 0;
    k=k+1;
    if(k==10)k=0;
    Serial.println(k);
    outPort(~seg7Table[k]);
  }
//有人按了這顆按鈕 且 按鈕被放開過
  else if(digitalRead(btnPin) == HIGH && buttonUp == 0)
  {
    buttonUp = 1;
  }
  delay(10); // Delay 時間可視情況調整(開關抖動)
}


例:兩個開關加減1
byte seg7Pin[]={2,3,4,5,6,7,8};
byte seg7Table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
byte btnPin1 = 9 ;
byte btnPin2 = 10 ;
int i,j;
int k=-1;
//boolean state = 0;//一開始沒有人按
boolean buttonUp1 = 1;//按鈕放開過了
boolean buttonUp2 = 1;//按鈕放開過了
void setup()
{
  for(i=0;i<7;i++)
  {
   pinMode(seg7Pin[i],OUTPUT);
   digitalWrite(seg7Pin[i],LOW);
  }
  pinMode(btnPin1, INPUT);
  pinMode(btnPin2, INPUT);
  digitalWrite(btnPin1, HIGH);
  digitalWrite(btnPin2, HIGH);
  Serial.begin(9600);
}
void outPort(byte data)
{
  for(j=0;j<7;j++)
  {
    if(data % 2 == 1)digitalWrite(seg7Pin[j],HIGH);
    else
    {
      digitalWrite(seg7Pin[j],LOW);
    }
    data=data/2;
  }
}
void loop()
{
//有人按了這顆按鈕 且 按鈕曾經被放開過即切換LED狀態
  if(digitalRead(btnPin1) == 0 && buttonUp1 == 1)
  {
    buttonUp1 = 0;
    k=k+1;
    if(k==10)k=0;
    Serial.println(k);
    outPort(~seg7Table[k]);
  }
//有人按了這顆按鈕 且 按鈕被放開過
  else if(digitalRead(btnPin1) == HIGH && buttonUp1 == 0)
  {
    buttonUp1 = 1;
  }
  if(digitalRead(btnPin2) == 0 && buttonUp2 == 1)
  {
    buttonUp2 = 0;
    k=k-1;
    if(k<0)k=9;
    Serial.println(k);
    outPort(~seg7Table[k]);
  }
//有人按了這顆按鈕 且 按鈕被放開過
  else if(digitalRead(btnPin2) == HIGH && buttonUp2 == 0)
  {
    buttonUp2 = 1;
  }
  delay(10); // Delay 時間可視情況調整(開關抖動)
}

algorithm

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