mbed在途中的時候,腦筋壞掉買了Arduino Nano...
我還以為會是第二個MADE IN ITALY
隨便焊焊,用Blink踹一下...
/*
* SHT-15 - 4
*/
#include <SHT1x.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd( 7, 8, 9, 10, 11, 12 );
SHT1x sht1x( 6, 5 );
void setup() {
lcd.begin( 16, 2 );
lcd.print( "SHT-15 - 4" );
Serial.begin( 9600 );
}
String getWeather() {
float temp_c, temp_f, humidity;
String result = "";
temp_c = sht1x.readTemperatureC();
temp_f = sht1x.readTemperatureF();
humidity = sht1x.readHumidity();
result.concat( (long) temp_c );
result += "C,";
result.concat( (long) temp_f );
result += "F,";
result.concat( (long) humidity );
result += "%";
return result;
}
void loop() {
lcd.setCursor( 0, 1 );
lcd.print( getWeather() );
Serial.println( "Running..." );
delay( 1000 );
}
#include <SD.h>
#include <SHT1x.h>
/*
* SHT 15 - 3
*/
#define DataPin 6 // DATA
#define ClockPin 5 // SCK
#define CSPIN 10 // SD CS pin
SHT1x sht1x( DataPin, ClockPin );
File myLogFile;
void setup() {
Serial.begin( 9600 );
Serial.print( "Initializing SD card..." );
pinMode( 10, OUTPUT );
if( !SD.begin( CSPIN ) ) {
Serial.println( "initialization faild!" );
return;
}
Serial.println( "initialization done." );
}
void loop() {
float temp_c, temp_f, humidity;
String log_msg = "";
temp_c = sht1x.readTemperatureC();
temp_f = sht1x.readTemperatureF();
humidity = sht1x.readHumidity();
log_msg.concat( (long) temp_c );
log_msg += ",";
log_msg.concat( (long) temp_f );
log_msg += ",";
log_msg.concat( (long) humidity );
myLogFile = SD.open( "sht15.txt", FILE_WRITE );
if( myLogFile ) {
Serial.println( "Writing..." );
myLogFile.println( log_msg );
myLogFile.close();
Serial.println( log_msg );
Serial.println( "done." );
} else {
Serial.println( "error opening sht15.txt" );
}
delay( 1000 );
}
#include/* * SHT 15 - 1 */ #define DataPin 11 // DATA #define ClockPin 12 // SCK SHT1x sht1x( DataPin, ClockPin ); void setup() { Serial.begin( 9600 ); } void loop() { float temp_c, temp_f, humidity; temp_c = sht1x.readTemperatureC(); temp_f = sht1x.readTemperatureF(); humidity = sht1x.readHumidity(); Serial.print( "Temperature: " ); Serial.print( temp_c, 2 ); Serial.print( "C / " ); Serial.print( temp_f, 2 ); Serial.print( "F. Humidity: " ); Serial.print( humidity ); Serial.println( "%" ); delay( 1000 ); }
/** * IRRemote 2 */ #include#include #define BtnPin 2 int buttonState = 0; // IRremote的IR發射器只能裝在pin 3 IRsend irsend; void setup() { pinMode( BtnPin, INPUT ); Serial.begin( 9600 ); } void loop() { buttonState = digitalRead( BtnPin ); if( buttonState == HIGH ) { Serial.println( "GoGoGo" ); irsend.sendNEC( 0x2FF00FF, 32 ); } }
![]() |
| Arduino小書兩枚 |
![]() |
| 兩顆Shift Register控制10 pin LED Bar 一 |
![]() |
| 兩顆Shift Register控制10 pin LED Bar 二 |
highByte()和lowByte這兩個method,順序是high byte部分先送,再送low byte。void loop() {
// 1024 = 2^10
for( int numberToDisplay = 0; numberToDisplay < 1024; numberToDisplay++ ) {
byte high_byte = highByte( numberToDisplay );
byte low_byte = lowByte( numberToDisplay );
Serial.println( numberToDisplay );
digitalWrite( LATCH_PIN, LOW );
// bitOrder: which order to shift out the bits; either MSBFIRST or LSBFIRST.
// (Most Significant Bit First, or, Least Significant Bit First)
shiftOut( DATA_PIN, CLOCK_PIN, MSBFIRST, high_byte );
shiftOut( DATA_PIN, CLOCK_PIN, MSBFIRST, low_byte );
digitalWrite( LATCH_PIN, HIGH );
delay( 500 );
}
}
analogRead()的值透過map()把比例顯示在LED Bar上。/*
* Keypad 1
*/
#define NumRows 4
#define NumCols 3
#define DebounceTime 20
const char keymap[NumRows][NumCols] = {
{ '1', '2', '3' },
{ '4', '5', '6' },
{ '7', '8', '9' },
{ '*', '0', '#' }
};
// pins
const int rowPins[ NumRows ] = { 5, 4, 3, 2 };
const int colPins[ NumCols ] = { 8, 7, 6 };
/Applications/Arduino.app/Contents/Resources/Java/libraries,也可以看上面Finder的截圖。pulseIn()取得Echo pin HIGH pulse的時間;另一個是距離的計算根據datasheet說明的Echo back pulse width(這裡是Echo Pin由LOW -> HIGH -> LOW的時間) microseconds除上58就是距離(cm),除上148就是inch的距離。/**
* Ultrasonic 1: 超音波測距 秀在Serial Port 9600
*/
#define TrigPin 10
#define EchoPin 13
long ping() {
// PING
digitalWrite( TrigPin, LOW );
delayMicroseconds( 2 );
digitalWrite( TrigPin, HIGH );
delayMicroseconds( 10 );
digitalWrite( TrigPin, LOW );
return pulseIn( EchoPin, HIGH );
}
void setup() {
pinMode( TrigPin, OUTPUT );
pinMode( EchoPin, INPUT );
Serial.begin( 9600 );
}
void loop() {
long duration, cm;
String result = "Distance: ";
duration = ping();
cm = duration / 58;
result += cm;
result += " (cm).";
Serial.println( result );
delay( 1500 );
}
| Analog Input | Analog Output | |
|---|---|---|
| Resolution | 10 bits (0~1023) | 8 bits (0~254) |
| Votage Range | 0~5 (Volts) | 0~5 (Volts) |
| Arduino Pins | A0~A5 | Digit Pins 3, 5, 6, 9. 10, 11 |
| Arduino Mega Pins | A0~A15 | Digit Pins 0~13 |
digitWrite(pin, value)第二個參數是HIGH/LOW;analogWrite(pin, value)。然後很無聊的做兩個練習,分別針對digit pin 13/11使用analogWrite()。/**
* Analog out on Digit Pin 13 test
*/
const int ledPin = 13;
void setup() {
pinMode( ledPin, OUTPUT );
}
void loop() {
for( int i=0; i<10; i++ ) {
analogWrite( ledPin, 54+(i*20) );
delay( 1000 );
}
}
/**
* Serial Library: Read 2 - Control LED
*/
int incomingByte = 0;
const int ledPin = 13;
void setup() {
pinMode( ledPin, OUTPUT );
Serial.begin( 9600 );
Serial.println( "'L' turn on, 'l' turn off" );
}
void loop() {
if( Serial.available() > 0 ) {
incomingByte = Serial.read();
// input 'L'
if( incomingByte == 76 ) {
Serial.println( "LED on!!" );
digitalWrite( ledPin, HIGH );
}
// inpit 'l'
if( incomingByte == 108 ) {
Serial.println( "LED off!!" );
digitalWrite( ledPin, LOW );
}
}
}
今年的新玩具~~玩不起樂高機器人就先玩這個。自己動手做看得到的東西,即使是簡單的LED, 可變電阻的組合的作業都覺得特別有fu。基礎先玩好,再來玩其他sensor和玩具...Make雜誌真是讚~~
PS. 看了一堆shield之後,覺得玩這個也不便宜 哈