arduinoのデジタルピンの状態をモニターとledで確認するためのスケッチです。
--------------------------------------------------
// this constant won't change:
const int TusinSokudo = 9600;
// Variables will change:---------------------------------
const int dPin = 2; // the pin that the pushbutton is attached to
const int lPin = 13; // the pin that the LED is attached to
boolean pState;
int mati0 = 0;
//--------------------------------------------
void setup() {// setup ++++++++++++
pinMode(dPin, INPUT);
pinMode(lPin, OUTPUT);
Serial.begin(TusinSokudo);
}
void loop() {// loop ++++++++++++
pState = digitalRead(dPin);
Serial.println(pState);
digitalWrite(lPin, pState);
delay(mati0)
--------------------------------------------------------
デジタルセンサーなどの状態でarduinoのピンがオン・オフが出来る。ここでは2ピンの状態で13ピンをオン・オフする。led以外のものを5vなら動かせる。ただしDCモーターはリレーをかませる必要がある。
パソコンとの通信速度以外は可変です。
センサーピンを増やしたスケッチです。
--------------------------------
// this constant won't change:
const int TusinSokudo = 9600;
// Variables will change:--------------------------------
const int PinMax = 5;// the quantity of pins.
const int dPin[PinMax] = {2, 3, 4, 5, 6}; // the pin number that the dejitalsensor is attached to
const int lPin[PinMax] = {13, 12, 11, 10, 9};
boolean pState[PinMax];
int mati0 = 0;
//--------------------------------------------
void setup() {// setup ++++++++++++
for (int i = 0; i <= PinMax; i++) {
pinMode(dPin[PinMax], INPUT);
pinMode(lPin[PinMax], OUTPUT);
Serial.begin(TusinSokudo);
}
}
void loop() {// loop ++++++++++++
for (int i = 0; i <= PinMax; i++) {
pState[i] = digitalRead(dPin[PinMax]);
Serial.println(pState[i]);
digitalWrite(lPin[i], pState[i]);
delay(mati0);
}
}
-----------------------------------------------
センサーピンを5つにしたもので、2, 3, 4, 5, 6ピンをセンサーピンとして使います。
13, 12, 11, 10, 9ピンのスイッチとなります。6つまでは出来ます。