| 1 |
/* |
| 2 |
Arduino Starter Kit example |
| 3 |
Project 13 - Touch Sensor Lamp |
| 4 |
|
| 5 |
This sketch is written to accompany Project 13 in the Arduino Starter Kit |
| 6 |
|
| 7 |
Parts required: |
| 8 |
- 1 megohm resistor |
| 9 |
- metal foil or copper mesh |
| 10 |
- 220 ohm resistor |
| 11 |
- LED |
| 12 |
|
| 13 |
Software required : |
| 14 |
- CapacitiveSensor library by Paul Badger |
| 15 |
https://www.arduino.cc/reference/en/libraries/capacitivesensor/ |
| 16 |
|
| 17 |
created 18 Sep 2012 |
| 18 |
by Scott Fitzgerald |
| 19 |
|
| 20 |
https://store.arduino.cc/genuino-starter-kit |
| 21 |
|
| 22 |
This example code is part of the public domain. |
| 23 |
*/ |
| 24 |
|
| 25 |
// import the library (must be located in the Arduino/libraries directory) |
| 26 |
#include <CapacitiveSensor.h> |
| 27 |
|
| 28 |
// create an instance of the library |
| 29 |
// pin 4 sends electrical energy |
| 30 |
// pin 2 senses senses a change |
| 31 |
CapacitiveSensor capSensor = CapacitiveSensor(4, 2); |
| 32 |
|
| 33 |
// threshold for turning the lamp on |
| 34 |
int threshold = 1000; |
| 35 |
|
| 36 |
// pin the LED is connected to |
| 37 |
const int ledPin = 12; |
| 38 |
|
| 39 |
|
| 40 |
void setup() { |
| 41 |
// open a serial connection |
| 42 |
Serial.begin(9600); |
| 43 |
// set the LED pin as an output |
| 44 |
pinMode(ledPin, OUTPUT); |
| 45 |
} |
| 46 |
|
| 47 |
void loop() { |
| 48 |
// store the value reported by the sensor in a variable |
| 49 |
long sensorValue = capSensor.capacitiveSensor(30); |
| 50 |
|
| 51 |
// print out the sensor value |
| 52 |
Serial.println(sensorValue); |
| 53 |
|
| 54 |
// if the value is greater than the threshold |
| 55 |
if (sensorValue > threshold) { |
| 56 |
// turn the LED on |
| 57 |
digitalWrite(ledPin, HIGH); |
| 58 |
} |
| 59 |
// if it's lower than the threshold |
| 60 |
else { |
| 61 |
// turn the LED off |
| 62 |
digitalWrite(ledPin, LOW); |
| 63 |
} |
| 64 |
|
| 65 |
delay(10); |
| 66 |
} |