「Arduino Mega 2560 LESSON:26 光抵抗素子 PhotoCell」の版間の差分
編集の要約なし |
編集の要約なし |
||
| 122行目: | 122行目: | ||
} | } | ||
updateShiftRegister(); | updateShiftRegister(); | ||
} | |||
</code></pre></div><script></freescript> | |||
LEDレベルメーターを残しながら、もっと具体的な電圧計的な表示をシリアルモニタで表示するなら以下のようになります。ただしAnalogピンが5Vちょうどを1024として受け取れていればの表示になりますので、誤差はあります。 | |||
<freescript></script><div class="hljs-wrap"><pre data-label="Arduino Code" class="pre-wrap"> | |||
<code class="cpp">int lightPin = A0; | |||
int latchPin = 11; | |||
int clockPin = 9; | |||
int dataPin = 12; | |||
byte leds = 0; | |||
void setup() | |||
{ | |||
pinMode(latchPin, OUTPUT); | |||
pinMode(dataPin, OUTPUT); | |||
pinMode(clockPin, OUTPUT); | |||
Serial.begin(9600); | |||
} | |||
void updateShiftRegister() | |||
{ | |||
digitalWrite(latchPin, LOW); | |||
shiftOut(dataPin, clockPin, LSBFIRST, leds); | |||
digitalWrite(latchPin, HIGH); | |||
} | |||
void loop() | |||
{ | |||
int reading = analogRead(lightPin); | |||
// A0の電圧を計算 | |||
float voltage = reading * 5.0 / 1023.0; | |||
// 光抵抗の抵抗値を計算(下側の固定抵抗は1kΩ) | |||
float resistance = 0; | |||
if (reading > 0) { | |||
resistance = 1000.0 * (1023.0 - reading) / reading; | |||
} | |||
// LED表示 | |||
int numLEDSLit = reading / 57; | |||
if (numLEDSLit > 8) numLEDSLit = 8; | |||
leds = 0; | |||
for (int i = 0; i < numLEDSLit; i++) { | |||
bitSet(leds, i); | |||
} | |||
updateShiftRegister(); | |||
// シリアルモニタに数値を表示 | |||
Serial.print("ADC: "); | |||
Serial.print(reading); | |||
Serial.print(" Voltage: "); | |||
Serial.print(voltage, 3); | |||
Serial.print(" V"); | |||
Serial.print(" Photoresistor: "); | |||
if (reading == 0) { | |||
Serial.println("Over range"); | |||
} else { | |||
Serial.print(resistance, 0); | |||
Serial.println(" ohm"); | |||
} | |||
delay(500); | |||
} | } | ||
</code></pre></div><script></freescript> | </code></pre></div><script></freescript> | ||
2026年11月6日 (金) 17:54時点における版
概要
74HC595 と8個の LEDと光抵抗素子を接続するレッスンです。
完成図は以下のとおりです。
[[|380px||none|完成図]]
PCB図は以下のようなものです。
[[|380px||none|PCB図]]
3Dプレビュー図は以下のようなものです。
[[|380px||none|3Dプレビュー図]]
コードは以下のようなものです。
//www.elegoo.com
//2016.12.9
int lightPin = 0;
int latchPin = 11;
int clockPin = 9;
int dataPin = 12;
int leds = 0;
void setup()
{
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}
void updateShiftRegister()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds);
digitalWrite(latchPin, HIGH);
}
void loop()
{
int reading = analogRead(lightPin);
int numLEDSLit = reading / 57; //1023 / 9 / 2
if (numLEDSLit > 8) numLEDSLit = 8;
leds = 0; // no LEDs lit to start
for (int i = 0; i < numLEDSLit; i++)
{
leds = leds + (1 << i); // sets the i'th bit
}
updateShiftRegister();
}
LEDレベルメーターを残しながら、もっと具体的な電圧計的な表示をシリアルモニタで表示するなら以下のようになります。ただしAnalogピンが5Vちょうどを1024として受け取れていればの表示になりますので、誤差はあります。
int lightPin = A0;
int latchPin = 11;
int clockPin = 9;
int dataPin = 12;
byte leds = 0;
void setup()
{
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
Serial.begin(9600);
}
void updateShiftRegister()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds);
digitalWrite(latchPin, HIGH);
}
void loop()
{
int reading = analogRead(lightPin);
// A0の電圧を計算
float voltage = reading * 5.0 / 1023.0;
// 光抵抗の抵抗値を計算(下側の固定抵抗は1kΩ)
float resistance = 0;
if (reading > 0) {
resistance = 1000.0 * (1023.0 - reading) / reading;
}
// LED表示
int numLEDSLit = reading / 57;
if (numLEDSLit > 8) numLEDSLit = 8;
leds = 0;
for (int i = 0; i < numLEDSLit; i++) {
bitSet(leds, i);
}
updateShiftRegister();
// シリアルモニタに数値を表示
Serial.print("ADC: ");
Serial.print(reading);
Serial.print(" Voltage: ");
Serial.print(voltage, 3);
Serial.print(" V");
Serial.print(" Photoresistor: ");
if (reading == 0) {
Serial.println("Over range");
} else {
Serial.print(resistance, 0);
Serial.println(" ohm");
}
delay(500);
}