1 minute read

Software (Flashing

Next Article
Index

Index

CHAPTER 6: Hacking Arduino 123

Figure 6-21 Schematic for an LED, Arduino, and a variable resistor

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 voltsInPin = 3; int gndPin = A2; int plusPin = A4; int 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); }

This article is from: