Advertisement
Guest User

Capacitive Sensor, Arduino Code

a guest
May 21st, 2015
422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1. // code patched together from examples
  2. // by Les Hall
  3. // started Wed May 20 2015
  4. //
  5.  
  6.  
  7. #include <CapacitiveSensor.h>
  8.  
  9.  
  10. CapacitiveSensor   cs1 = CapacitiveSensor(4,3);
  11. CapacitiveSensor   cs2 = CapacitiveSensor(4,5);
  12.  
  13.  
  14. int inByte = 0;  // incoming serial byte
  15. long total1;  // Capacitance sense value
  16. long total2;  // Capacitance sense value
  17.  
  18.  
  19. void setup()
  20. {
  21.   //cs1.set_CS_AutocaL_Millis(0xFFFFFFFF);     // turn off autocalibrate on channel 1 - just as an example
  22.   pinMode(9, OUTPUT);
  23.   Serial.begin(9600);
  24.   establishContact();  // send a byte to establish contact until receiver responds
  25. }
  26.  
  27. void loop()
  28. {
  29.   IO();
  30.   // if we get a valid byte, transmit:
  31.   if (Serial.available() > 0) {
  32.     // get incoming byte:
  33.     inByte = Serial.read();
  34.     delay(10);
  35.     Serial.print(total1);
  36.     Serial.print(",");
  37.     Serial.println(total2);
  38.   }
  39. }
  40.  
  41.  
  42. void establishContact() {
  43.   while (Serial.available() <= 0) {
  44.     Serial.println("0,0");   // send an initial string
  45.     IO();
  46.     delay(300);
  47.   }
  48. }
  49.  
  50.  
  51. void IO()
  52. {
  53.   // read the analog inputs:
  54.   total1 =  cs1.capacitiveSensor(1000);
  55.   total2 =  cs2.capacitiveSensor(1000);
  56.   tone(9, total1);
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement