SSブログ

Maruduinoでラーメンタイマー、、、ただし6個いっぺんに作れます。 [アプリケーション]

※なんかイマイチ感がありありだったので、スケッチを修正しました。今度はいっぺんに6個までカップラーメンを作ることができます。

Img_2070.jpgいや、ほら、カップラーメンって全部3分と言うわけでなく、3分だったり、4分だったり、うどんは5分が多いかな。
逆に焼きそばは1分とか、もうまちまち。
一人なら問題無いけれど、多人数で一つのキッチンタイマーでは苦しすぎる。
で、5個6個いっぺんに計測可能なラーメンタイマーを作りましたヨ。

これでキッチンタイマーの奪い合いも無くなり、平和にラーメンをすすれますネ。

LCDの接続はスケッチをごらんいただくとして、スピーカー出力はDI13(DIGITAL13)をAUX1に接続します。2つのスイッチを使いますが、SW1はDI2、SW2はDI3に接続します。

※例の100円スピーカーを接続するのをお忘れなく。
※メロディ発生のスケッチはexamplesのdigitalのtoneMelodyから流用していますので、pitches.hをプロジェクトフォルダーにコピーして使ってください。
※色々と気になる点はありますが、まあ善しとしましょう。
※なんか気の抜けるネタばかりですいません。
/*
  LiquidCrystal Library - Hello World
 
 Demonstrates the use a 16x2 LCD display.  The LiquidCrystal
 library works with all LCD displays that are compatible with the 
 Hitachi HD44780 driver. There are many of them out there, and you
 can usually tell them by the 16-pin interface.
 
 This sketch prints "Hello World!" to the LCD
 and shows the time.
 
  The circuit:
 * LCD RS pin to digital pin 8
 * LCD Enable pin to digital pin 9
 * LCD D4 pin to digital pin 4
 * LCD D5 pin to digital pin 5
 * LCD D6 pin to digital pin 6
 * LCD D7 pin to digital pin 7

 * 10K resistor:
 * ends to +5V and ground
 
 Library originally added 18 Apr 2008
 by David A. Mellis
 library modified 5 Jul 2009
 by Limor Fried (http://www.ladyada.net)
 example added 9 Jul 2009
 by Tom Igoe
 modified 25 July 2009
 by David A. Mellis
 
 
 http://www.arduino.cc/en/Tutorial/LiquidCrystal
 */

// include the library code:
#include <LiquidCrystal.h>
#include "pitches.h"

int int0_count,int1_count;
unsigned char store_sw_status[3];  /*スイッチ状態の記録*/
/*SW1はDI2に接続し、割り込み0、SW2はDI3に接続し、割り込み1で処理する。*/
unsigned long guard1_time,guard2_time;
int timers;
struct RECORD 
{
  int minutes;
  unsigned long end_time;
} rec[6];

const struct SCORE
{
  int note;
  int dura;
} score[] =
{
  {NOTE_G4,4},
  {NOTE_A4,4},
  {NOTE_B4,2},
  {NOTE_A4,4},
  {NOTE_G4,8},
  {0,8},
  {NOTE_G4,4},
  {NOTE_A4,4},
  {NOTE_B4,4},
  {NOTE_A4,4},
  {NOTE_G4,4},
  {NOTE_A4,1},
};

const struct POSITION
{
  int x;
  int y;
} pos[] =
{
  {0,0},
  {5,0},
  {10,0},
  {0,1},
  {5,1},
  {10,1},
};

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

void setup()
{
  lcd.begin(16, 2);
  for( int i = 0; i < sizeof(pos) / sizeof(pos[0]); i++ )
  {
    lcd.setCursor(pos[i].x,pos[i].y);
    lcd.print("0:00");
  }

  attachInterrupt(0, sw1_int_handler, RISING);
  attachInterrupt(1, sw2_int_handler, RISING);
}

void loop()
{
  int i;
  char buffer[16];
  static int count1,count2;

  for( i = 0; i < sizeof(pos) / sizeof(pos[0]); i++ )
  {
    /*表示位置の設定*/
    lcd.setCursor(pos[i].x,pos[i].y);

    if( rec[i].end_time != 0 )
    {
      if( rec[i].end_time <= millis() )
      {
        rec[i].minutes = 0;
        rec[i].end_time = 0;
        play_melody();
        lcd.print(millis2min(buffer,0));
      }
      else
        lcd.print(millis2min(buffer,rec[i].end_time - millis()));
    }
  }

  if( count1 != int0_count )  /*割り込みカウントと不一致が検出された時*/
  {
    count1 = int0_count;  /*更新*/

    /*レコードが空いているかの判断*/
    for( i = 0; i < sizeof(pos) / sizeof(pos[0]); i++ )
    {
      if( rec[ timers ].end_time == 0 ) break;
      if( ++timers >= sizeof(pos) / sizeof(pos[0]) ) timers = 0;
    }

    /*書き込み可能な場合*/
    if( i != sizeof(pos) / sizeof(pos[0]) )
    {
      rec[ timers ].minutes = rec[ timers ].minutes + 1;
      rec[ timers ].minutes %= 10;
      lcd.setCursor(pos[ timers ].x,pos[ timers ].y);
      lcd.print(millis2min(buffer,rec[ timers ].minutes * 60 * 1000UL));
    }
  }

  if( count2 != int1_count )  /*割り込みカウントと不一致が検出された時*/
  {
    count2 = int1_count;  /*更新*/

    /*レコードが空いているかの判断*/
    for( i = 0; i < sizeof(pos) / sizeof(pos[0]); i++ )
    {
      if( rec[ timers ].end_time == 0 ) break;
      if( ++timers >= sizeof(pos) / sizeof(pos[0]) ) timers = 0;
    }

    if( i != sizeof(pos) / sizeof(pos[0]) )
    {
      rec[ timers ].end_time = (rec[ timers ].minutes * 60 * 1000UL) + millis();
      if( ++timers >= sizeof(pos) / sizeof(pos[0]) ) timers = 0;  /*レコード番号の更新*/
    }
  }

  delay( 50 );  /*表示更新時間*/
}

char *millis2min( char *dest, unsigned long milli )
{
  int minute,second,temp;
  
  if( milli > 10 * 60 * 1000UL ) return 0;
  temp = (int)(milli / 1000UL);
  minute = temp / 60;
  second = temp % 60;

  dest[0] = (minute % 10) + '0';
  dest[1] = ':';
  dest[2] = (second / 10) + '0';
  dest[3] = (second % 10) + '0';
  dest[4] = '\0';

  return dest;
}

void play_melody()
{
  for (int thisNote = 0; thisNote < sizeof(score) / sizeof(score[0]); thisNote++)
  {
    int noteDuration = 1000/score[thisNote].dura;
    tone(13, score[thisNote].note,noteDuration);
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
  }
}

void sw1_int_handler()
{
  if( millis() > guard1_time )  /*現在時刻がガードタイムを越した時のみ有効とする*/
  {
    int0_count++;  /*有効割り込み数*/
    guard1_time = millis() + 300UL;  /*不感帯時間の再設定*/
  }
}

void sw2_int_handler()
{
  if( millis() > guard2_time )  /*現在時刻がガードタイムを越した時のみ有効とする*/
  {
    int1_count++;  /*有効割り込み数*/
    guard2_time = millis() + 300UL;  /*不感帯時間の再設定*/
  }
}


ITRONプログラミング入門―H8マイコンとHOSで始める組み込み開発

ITRONプログラミング入門―H8マイコンとHOSで始める組み込み開発

  • 作者: 濱原 和明
  • 出版社/メーカー: オーム社
  • 発売日: 2005/04/25
  • メディア: 単行本



Prototyping Lab ―「作りながら考える」ためのArduino実践レシピ

Prototyping Lab ―「作りながら考える」ためのArduino実践レシピ

  • 作者: 小林 茂
  • 出版社/メーカー: オライリージャパン
  • 発売日: 2010/05/27
  • メディア: 大型本



nice!(0)  コメント(0)  トラックバック(0) 

nice! 0

コメント 0

コメントを書く

お名前:
URL:
コメント:
画像認証:
下の画像に表示されている文字を入力してください。

トラックバック 0

この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。