体験的マイコン学習(Arduino編)


目次



第11回 タイマーを作ってみよう


タイマーを作って動かしてみよう


準備するもの

  1. 圧電ブザー: 1個
  2. タクトスイッチ: 2個
  3. 抵抗(330Ω): 10本
  4. ジャンパー線: 24本
  5. 4桁デジタル表示管:1個

ページトップ

ブレッドボードへの接続

(1)ブレッドボードへの接続を行っていきます (※ タクトスイッチなど、しっかりと接続ができていないと正常に動作しません)


frizing画像
ページトップ

プログラムの作成

(1)プログラムを作成します

//入力スイッチ

const int pinSwCnt = 13;

const int pinSwCnt2 = A0;


//7セグピン

const int pin7segA = 1;

const int pin7segB = 3;

const int pin7segC = 5;

const int pin7segD = 7;

const int pin7segE = 8;

const int pin7segF = 2;

const int pin7segG = 4;


//7セグ桁制御

const int pin7segDig1 = 12;

const int pin7segDig2 = 11;

const int pin7segDig3 = 10;

const int pin7segDig4 = 9;


//時間操作スイッチエッジ検出

int swCntPrevState = 0; //1つ前のスイッチ状態

int swCntCurrState = 0; //現在のスイッチ状態

int swCntUpEdge = 0; //立ち上がりエッジ検出

int pushSingle = 0; //1回押された判定

int pushDouble = 0; //2回押された判定


//時間設定スイッチ関連

int swSetPrevState = 0; //1つ前のスイッチ状態

int swSetCurrState = 0; //現在のスイッチ状態

long timeStartSet; //時間設定

long timePrevCtUp;


//時間

long timeNow; //現在時刻

long timeStart; //タイマー開始時刻

long timeStop; //一時停止時刻

long timeUpEdge; //立ち上がりエッジを検出した時間

int numUpEdge = 0;//立ち上がりエッジを検出した回数

int setTime = 300; //設定時刻(秒単位)

int timeSet = setTime;//スイッチ設定時刻

int timeDisp = setTime; //表示時刻

int numDisp[4]; //LEDに表示する数字


//制御

int i;

int timeState=0; //タイマー状態 0:初期 1:動作 2:完了 3:停止

void up_time_disp() {
    if( timeDisp == 6000 )
        timeDisp = 60;
    else
        timeDisp += 60;
}

void setup() {
    //入出力の初期化
    pinMode(pinSwCnt, INPUT);
    pinMode(pinSwCnt2, INPUT);
    pinMode(pin7segA, OUTPUT);
    pinMode(pin7segB, OUTPUT);
    pinMode(pin7segC, OUTPUT);
    pinMode(pin7segD, OUTPUT);
    pinMode(pin7segE, OUTPUT);
    pinMode(pin7segF, OUTPUT);
    pinMode(pin7segG, OUTPUT);
    pinMode(pin7segDig1, OUTPUT);
    pinMode(pin7segDig2, OUTPUT);
    pinMode(pin7segDig3, OUTPUT);
    pinMode(pin7segDig4, OUTPUT);
}

void loop() {
    timeNow = millis(); //現在時刻の取得
    //時間操作スイッチ立ち上がりエッジ検出
    swCntCurrState = digitalRead(pinSwCnt); //現在のスイッチ状態
    if(swCntCurrState==HIGH && swCntCurrState!=swCntPrevState) {   //現在のスイッチ状態と一つ前のスイッチ状態を比較
        swCntUpEdge = 1;
        if(numUpEdge == 0)
            timeUpEdge = timeNow;
        numUpEdge++;
    }
    else
        swCntUpEdge = 0;
    swCntPrevState = swCntCurrState;   //一つ前のスイッチ状態
     //スイッチの押された回数が1回か2回かの判定
    if( numUpEdge >= 1 ){
        if( numUpEdge >= 2 ){
            pushDouble = 1;
            numUpEdge = 0;
        }
        else if(timeNow-timeUpEdge > 600) {
            pushSingle = 1;
            numUpEdge = 0;
        }
    }
    //動作状態の判定
    switch(timeState){
        case 1:   //動作
            timeDisp = timeSet -( (timeNow-timeStart)/1000 ); //開始時刻と現在時刻の差が表示時刻
            if(pushSingle == 1){
                timeState = 3;
                timeStop = timeNow;
            }
            else if((timeNow-timeStart)/1000>=(timeSet)){
                timeState = 2;
            }
        break;
        case 2:   //完了
            timeDisp = 0;
            if(pushSingle==1 || pushDouble==1){
                timeState = 0;
            timeDisp = setTime;
            }
        break;
        case 3:   //停止
            timeDisp = timeSet -((timeStop-timeStart)/1000); //開始時刻と停止時刻の差が表示時刻
            if(pushSingle == 1){
                timeState = 1;
                timeStart = timeNow - (timeStop-timeStart);
            }
            else if(pushDouble == 1){
                timeState = 0;
                timeDisp = setTime;
            }
        break;
        default:   //初期
            if(pushSingle == 1){
                timeState = 1;
                timeSet = timeDisp;
                timeStart = timeNow;
            }
        break;
    }

    pushSingle = 0;
    pushDouble = 0;

    if(timeState == 2) //動作状態が完了ならば、音を鳴らす
        tone(A1, 1500);
    else
        noTone(A1);//動作状態が完了でないなら、音を鳴らさない
    //時間設定スイッチ立ち上がりエッジ検出
    swSetCurrState = digitalRead(pinSwCnt2); //現在のスイッチ状態
    if(timeState==0 && swSetCurrState==HIGH && swSetCurrState!=swSetPrevState){ //現在のスイッチ状態と一つ前のスイッチ状態を比較
        timeStartSet = timeNow;
        timePrevCtUp = timeNow;
        up_time_disp();
    }
    swSetPrevState = swSetCurrState; //一つ前のスイッチ状態

    //表示時刻をLEDの数字へ変更
    numDisp[0] = timeDisp % 10;
    numDisp[1] = (timeDisp % 60)/10;
    numDisp[2] = (timeDisp/60)%10;
    numDisp[3] = (timeDisp/600)%10;

    pushSingle = 0;

    //7セグLEDの制御
    for(i=0; i<4; i++){
        switch(i){
            case 1:
                digitalWrite(pin7segDig4 , HIGH );
                digitalWrite(pin7segDig3 , LOW );
                digitalWrite(pin7segDig2 , HIGH );
                digitalWrite(pin7segDig1 , HIGH );
                break;
            case 2:
                digitalWrite(pin7segDig4 , HIGH );
                digitalWrite(pin7segDig3 , HIGH );
                digitalWrite(pin7segDig2 , LOW );
                digitalWrite(pin7segDig1 , HIGH );
                break;
            case 3:
                digitalWrite(pin7segDig4 , HIGH );
                digitalWrite(pin7segDig3 , HIGH );
                digitalWrite(pin7segDig2 , HIGH );
                digitalWrite(pin7segDig1 , LOW );
                break;
            default:
                digitalWrite(pin7segDig4 , LOW );
                digitalWrite(pin7segDig3 , HIGH );
                digitalWrite(pin7segDig2 , HIGH );
                digitalWrite(pin7segDig1 , HIGH );
                break;
        }

        switch(numDisp[i]){
            case 1:
                digitalWrite(pin7segA , LOW );
                digitalWrite(pin7segB , HIGH );
                digitalWrite(pin7segC , HIGH );
                digitalWrite(pin7segD , LOW );
                digitalWrite(pin7segE , LOW );
                digitalWrite(pin7segF , LOW );
                digitalWrite(pin7segG , LOW );
                break;
            case 2:
                digitalWrite(pin7segA , HIGH );
                digitalWrite(pin7segB , HIGH );
                digitalWrite(pin7segC , LOW );
                digitalWrite(pin7segD , HIGH );
                digitalWrite(pin7segE , HIGH );
                digitalWrite(pin7segF , LOW );
                digitalWrite(pin7segG , HIGH );
                break;
            case 3:
                digitalWrite(pin7segA , HIGH );
                digitalWrite(pin7segB , HIGH );
                digitalWrite(pin7segC , HIGH );
                digitalWrite(pin7segD , HIGH );
                digitalWrite(pin7segE , LOW );
                digitalWrite(pin7segF , LOW );
                digitalWrite(pin7segG , HIGH );
                break;
            case 4:
                digitalWrite(pin7segA , LOW );
                digitalWrite(pin7segB , HIGH );
                digitalWrite(pin7segC , HIGH );
                digitalWrite(pin7segD , LOW );
                digitalWrite(pin7segE , LOW );
                digitalWrite(pin7segF , HIGH );
                digitalWrite(pin7segG , HIGH );
                break;
            case 5:
                digitalWrite(pin7segA , HIGH );
                digitalWrite(pin7segB , LOW );
                digitalWrite(pin7segC , HIGH );
                digitalWrite(pin7segD , HIGH );
                digitalWrite(pin7segE , LOW );
                digitalWrite(pin7segF , HIGH );
                digitalWrite(pin7segG , HIGH );
                break;
            case 6:
                digitalWrite(pin7segA , HIGH );
                digitalWrite(pin7segB , LOW );
                digitalWrite(pin7segC , HIGH );
                digitalWrite(pin7segD , HIGH );
                digitalWrite(pin7segE , HIGH );
                digitalWrite(pin7segF , HIGH );
                digitalWrite(pin7segG , HIGH );
                break;
            case 7:
                digitalWrite(pin7segA , HIGH );
                digitalWrite(pin7segB , HIGH );
                digitalWrite(pin7segC , HIGH );
                digitalWrite(pin7segD , LOW );
                digitalWrite(pin7segE , LOW );
                digitalWrite(pin7segF , HIGH );
                digitalWrite(pin7segG , LOW );
                break;
            case 8:
                digitalWrite(pin7segA , HIGH );
                digitalWrite(pin7segB , HIGH );
                digitalWrite(pin7segC , HIGH );
                digitalWrite(pin7segD , HIGH );
                digitalWrite(pin7segE , HIGH );
                digitalWrite(pin7segF , HIGH );
                digitalWrite(pin7segG , HIGH );
                break;
            case 9:
                digitalWrite(pin7segA , HIGH );
                digitalWrite(pin7segB , HIGH );
                digitalWrite(pin7segC , HIGH );
                digitalWrite(pin7segD , HIGH );
                digitalWrite(pin7segE , LOW );
                digitalWrite(pin7segF , HIGH );
                digitalWrite(pin7segG , HIGH );
                break;
            default : //0
                digitalWrite(pin7segA , HIGH );
                digitalWrite(pin7segB , HIGH );
                digitalWrite(pin7segC , HIGH );
                digitalWrite(pin7segD , HIGH );
                digitalWrite(pin7segE , HIGH );
                digitalWrite(pin7segF , HIGH );
                digitalWrite(pin7segG , LOW );
                break;
        }
        delay(5);
    }
}

ページトップ

実行する

(1) プログラムを実行します(スイッチを押して、タイマーが動作し0になったときブザーが鳴れば成功です)

実行1

プログラムについては、以下の記事を参考にさせていただきました。

ありがとうございました。

タイマー -第6回:スイッチでタイマー操作2(時間設定)

ページトップ