HowTo-Color (8) / Hacking Electronics / Simon Monk / 236-3 / Chapter 6
Figure 6-21 Schematic for an LED, Arduino, and a variable resistor
CHAPTER 6: Hacking Arduino
123
To avoid confusion, put the resistor in the anode (positive and longer) lead and keep the combined lead the longer lead, so you know it is the positive end of the combo. The schematic diagram for the arrangement is shown in Figure 6-21. We will use pin 9 as a digital output for the LED. The other end of the LED combo being connected to a convenient GND connection. Keep this Arduino-friendly LED combo. You will use it again in several later sections.
Software (Flashing) You will use two different sketches with this arrangement of hardware. The first uses the variable resistor to control the speed of the flashing, while the second will control the brightness of the LED. Attach the LED resistor combo, as shown in Figure 6-19, and load the sketch “variable_led_flash” onto your Arduino board. You should find that turning the knob controls the rate at which the LED flashes. // variable_led_flash int int int int
voltsInPin = 3; gndPin = A2; plusPin = A4; ledPin = 9;
void setup() { pinMode(gndPin, OUTPUT); digitalWrite(gndPin, LOW); pinMode(plusPin, OUTPUT); digitalWrite(plusPin, HIGH); pinMode(ledPin, OUTPUT); } void loop() { int rawReading = analogRead(voltsInPin); int period = map(rawReading, 0, 1023, 100, 500); digitalWrite(ledPin, HIGH); delay(period); digitalWrite(ledPin, LOW); delay(period); }
06-ch06.indd 123
1/2/13 11:55 AM