Arduino 温湿度センサ

Groveの温湿度センサと7セグLEDを組み合わせてみた。

簡単過ぎて勉強にはならない(笑)

 

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

#include "DHT.h"
#include "TM1637.h"
#define CLK 4//pins definitions for TM1637 and can be changed to other ports       
#define DIO 3
TM1637 tm1637(CLK,DIO);
#define DHTPIN 2     // what pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT11   // DHT 11 
//#define DHTTYPE DHT22   // DHT 22  (AM2302)
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

DHT dht(DHTPIN, DHTTYPE);

void setup() 
{
    Serial.begin(9600); 
    Serial.println("DHTxx test!");
 tm1637.init();
  tm1637.set(BRIGHT_TYPICAL);//BRIGHT_TYPICAL = 2,BRIGHT_DARKEST = 0,BRIGHTEST = 7;
    dht.begin();
}

void loop() 
{
    // Reading temperature or humidity takes about 250 milliseconds!
    // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
    float h = dht.readHumidity();
    float t = dht.readTemperature();

    // check if returns are valid, if they are NaN (not a number) then something went wrong!
    if (isnan(t) || isnan(h)) 
    {
        Serial.println("Failed to read from DHT");
    } 
    else 
    {
        Serial.print("Humidity: "); 
        Serial.print(h);
        Serial.print(" %¥t");
        Serial.print("Temperature: "); 
        Serial.print(t);
        Serial.println(" *C");
        tm1637.display(0,(int)(h/10));
        tm1637.display(1,(int)(h-(int)(h/10)*10));
        tm1637.display(2,(int)(t/10));
        tm1637.display(3,(int)(t-(int)(t/10)*10));
    }
}

ジャジャガッチ | Arduino | 23:35 | comments(0) | trackbacks(0) |

Arduino 7セグメントLED(Grove)

この前電子部品屋で買ってきたものの一つが7セグメントLED。

Arduino向けのモジュールなのできっと簡単に使えるんだろうということで買ってみた。

どうもGroveというArduino向けモジュール群のひとつらしい。

 

メーカHPでTM1637向けライブラリをダウンロードし、IDEにインクルードする。

サンプル(IDE中でスケッチ例という項目にいろいろある)を参考に、数値をランダムに表示してみた。

 

#include "TM1637.h"
#define CLK 2//pins definitions for TM1637 and can be changed to other ports       
#define DIO 3
TM1637 tm1637(CLK,DIO);
void setup()
{
  tm1637.init();
  tm1637.set(BRIGHT_TYPICAL);//BRIGHT_TYPICAL = 2,BRIGHT_DARKEST = 0,BRIGHTEST = 7;
}
void loop()
{
  
  delay(150);
  for(int i=0; i<4; i++)
  {
    tm1637.display(i,random(16));
  }
  delay(300);
}

 

すごい楽!!

 

 

ジャジャガッチ | Arduino | 00:20 | comments(0) | trackbacks(0) |

Arduboy 自由落下

今日気がついたんだけど、Arduboyって左がAボタンなのな。

ファミコンと逆で混乱したわ。

 

今日はキャラクターを自由落下させてみた。

さらに、ボタンで落下に抗うことが出来る。

 

#include "Arduboy.h"

// make an instance of arduboy used for many functions
Arduboy arduboy;

PROGMEM const unsigned char player[] = {
0x00, 0x00, 0x00, 0xc0, 0x20, 0x10, 0xc8, 0x08,
0x08, 0xc8, 0x10, 0x20, 0xc0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x03, 0x04, 0x08, 0x14, 0x14,
0x14, 0x14, 0x08, 0x04, 0x03, 0x00, 0x00, 0x00,
};

 

// This function runs once in your game.
// use it for anything that needs to be set only once in your game.
void setup() {
  // initiate arduboy instance
  arduboy.begin();

}

double g = 1;
double dt = 0.1;

void loop() {
  // pause render until it's time for the next frame
  if (!(arduboy.nextFrame()))
    return;

  // first we clear our screen to black
  arduboy.clear();
  
 

 //ここにロジック書く////////////////////////////
  static double y = 10;//, y = 10;
  static double v = 0;
  static double t = 0;
  t+=dt;
  if( arduboy.pressed(A_BUTTON) == true ) 
  {
    v = 5;
    t = 0;
  }
  y = g * t * dt - v*dt + y;
  arduboy.drawBitmap(16,y,player,16,16,1);

  // then we finaly we tell the arduboy to display what we just wrote to the display
  arduboy.display();
}

 

loop関数が呼ばれるごとに時刻tを進めてるけど、位置は前回更新時との差分を足しこむ方が都合がよいので、高校物理で習う自由落下の式x = ...ではなく

dx = gtdt+vdt

を使っている。

 

ジャジャガッチ | Arduboy | 07:51 | comments(0) | trackbacks(0) |

画像を表示する -Arduboy-

まずは表示する画像を用意しなくてはならない。

ペイントで適当に絵を描いて、Arduboy Game Image Converterというページに画像をコピペすれば配列形式のコードが得られるので、これをソースコードにコピペすればok。

適当にこういう画像作ってみた。

 arduboy.drawBitmap(16,16,player,16,16,1);

とすれば画像を表示できる。

最初の二つが位置、3つ目が配列へのポインタ。その次のふたつが解像度、最後がよくわからない。色ということらしいが・・・?

配列の具体的内容は次のとおり。

PROGMEM const unsigned char player[] = {
0x00, 0x00, 0x00, 0xc0, 0x20, 0x10, 0xc8, 0x08, 
0x08, 0xc8, 0x10, 0x20, 0xc0, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x03, 0x04, 0x08, 0x14, 0x14, 
0x14, 0x14, 0x08, 0x04, 0x03, 0x00, 0x00, 0x00, 

};

 

PROGMEMキーワードはArduino特有のもので、Flashメモリに領域確保するらしい。大きなデータはここに置くとのこと。

ジャジャガッチ | Arduboy | 07:22 | comments(0) | trackbacks(0) |

ボタン判定

#include "Arduboy.h"

// make an instance of arduboy used for many functions
Arduboy arduboy;

 

// This function runs once in your game.
// use it for anything that needs to be set only once in your game.
void setup() {
  // initiate arduboy instance
  arduboy.begin();

  // here we set the framerate to 15, we do not need to run at
  // default 60 and it saves us battery life
  arduboy.setFrameRate(15);
}


// our main game loop, this runs once every cycle/frame.
// this is where our game logic goes.
void loop() {
  // pause render until it's time for the next frame
  if (!(arduboy.nextFrame()))
    return;

  // first we clear our screen to black
  arduboy.clear();

  // we set our cursor 5 pixels to the right and 10 down from the top
  // (positions start at 0, 0) 
  arduboy.setCursor(4, 9);

  // then we print to screen what is in the Quotation marks ""
  if( arduboy.pressed(A_BUTTON) == true ) 
  {
    arduboy.print(F("A!"));
  }
  else if(arduboy.pressed(B_BUTTON) == true)
  {
    arduboy.print("B!");
  }
  else arduboy.print("xxx");
    

  // then we finaly we tell the arduboy to display what we just wrote to the display
  arduboy.display();
}

 

ジャジャガッチ | Arduboy | 07:20 | comments(0) | trackbacks(0) |

雛形

サンプルファイル見て、雛形用意した。

 

#include "Arduboy.h"

// make an instance of arduboy used for many functions
Arduboy arduboy;

// This function runs once in your game.
// use it for anything that needs to be set only once in your game.
void setup() {
  // initiate arduboy instance
  arduboy.begin();

}

 

void loop() {
  // pause render until it's time for the next frame
  if (!(arduboy.nextFrame()))
    return;

  // first we clear our screen to black
  arduboy.clear();

 

 //ここにロジック書く////////////////////////////

 

  // then we finaly we tell the arduboy to display what we just wrote to the display
  arduboy.display();
}

ジャジャガッチ | Arduboy | 22:24 | comments(0) | trackbacks(0) |

Arduboy買いました

久しぶりに電子部品を買いに行った。子供のおもちゃを直すために。

かなり久しぶりにこういう店に来たので、テンション上がっていろいろ買ってしまった。

 

それは・・・、Arduboy!

調べてみると、Arduinoベースで作られているゲームボーイのような携帯ゲームらしい。

面白いのは、自分でゲームを作れること!

最初からインストールされているゲームはバージョン等によって異なるらしいが、僕の場合はシューティングゲームが入っていた。

 

 

見た目も中々かっこいい!

このサンプル、結構よく出来ていて、自機のパワーアップ等も実装されている。

 

とりあえずサンプルプログラムを書き込むために、Arduboy用のライブラリをzip形式でダウンロード、IDEでライブラリを追加後、Hello worldというサンプルを書き込んでみた。

 

 

しばらく楽しめそうだ。

ジャジャガッチ | Arduboy | 21:16 | comments(0) | trackbacks(0) |

Arduino始めました

昨年からなんだけど、Arduino始めました。

きっかけは、PICマイコンをusb制御しようと思ったときに、コンパイラの種類とかライブラリのバージョンによってコンパイルできたり出来なかったり安定しなくて不満が出たこと。

特にコスト面、サイズ面の制約もなかったので、試しに、ということでArduino。

 

一応知らない人のためにArduinoとは何ぞやということを説明しておく。

Arduinoは有名なマイコンボードで、USB接続することでピンの0 / 1を制御出切る。

ただ、ものすごくお手軽。

ハードウェア的にはUSBでPCと接続すればそれでok。それだけでプログラムの書き込み、電源供給が出来る。

ソフトウェアも非常に簡単。

例えばLチカ(LEDチカチカ)ならば

 

void setup() {
  // put your setup code here, to run once:
pinMode(2, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
digitalWrite(2, HIGH);   // LEDをオン
  delay(1000);                  // 1秒待つ
  digitalWrite(2, LOW);    // LEDをオフ
  delay(1000);
}

 

だけ。

関数もそれほど多くなく、日本語リファレンスもあるので容易にやりたいことを実現できる。

3000円くらいで買えるし、超オススメ。もう当分PICは触らない。

もちろん、きっちり最適化したいときはPICじゃないと無理な場合はあるから、そこは用途に応じて使い分けましょう。

 

ジャジャガッチ | Arduino | 23:14 | comments(0) | trackbacks(0) |
1/1PAGES | |

01
--
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
--
>>
<<
--
PR
RECOMMEND
RECENT COMMENT
MOBILE
qrcode
OTHERS
Since 2013/09/17
LATEST ENTRY
CATEGORY
ARCHIVE
LINKS
PROFILE
SEARCH