]> git.piffa.net Git - sketchbook_andrea/blob - advanced_projects/capacitance/led_pwm/led_pwm.ino
Capacitance fir st commit,
[sketchbook_andrea] / advanced_projects / capacitance / led_pwm / led_pwm.ino
1 #include <CapacitiveSensor.h>
2
3 /*
4  * CapitiveSense Library Demo Sketch
5  * Paul Badger 2008
6  * Uses a high value resistor e.g. 10M between send pin and receive pin
7  * Resistor effects sensitivity, experiment with values, 50K - 50M. Larger resistor values yield larger sensor values.
8  * Receive pin is the sensor pin - try different amounts of foil/metal on this pin
9  */
10
11
12 CapacitiveSensor   cs_4_2 = CapacitiveSensor(4, 2);       // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired
13 // pin 8 is sensor pin, add a wire and or foil
14 const int led = 9;        // pin that the Piezo is attached to
15 int sensorMin = 50;        // minimum sensor value
16 int sensorMax = 3200;           // maximum sensor value
17 void setup()
18 {
19   cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF);     // turn off autocalibrate on channel 1 - just as an example
20   Serial.begin(9600);
21   pinMode(led, OUTPUT);
22 }
23
24 void loop()
25 {
26   long start = millis();
27   long total1 =  smoothRead(cs_4_2.capacitiveSensor(30));
28
29   int value = map(total1, sensorMin, sensorMax, 0, 255);
30
31   value = constrain(value, 0, 255);
32   analogWrite(led, value);
33
34   Serial.print(millis() - start);        // check on performance in milliseconds
35   Serial.print("\t Capacitance: ");                    // tab character for debug windown spacing
36
37   Serial.print(total1);                  // print sensor output 1
38   Serial.print("  \t   Luminosita: ");
39   Serial.println(value);
40   delay(1);                             // arbitrary delay to limit data to serial port
41
42 }
43
44 // Funzioni
45
46 int smoothRead(int value) {
47 //  Legge 10 valori dal sensore e ritorna il valore medio tra questi.
48   int total = 0;
49   for (int i = 0; i < 10; i++) { 
50     total = total + value;
51   }
52   return(total / 10);
53 }
54