Guest User

Untitled

a guest
Nov 24th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. #include <Servo.h>
  2. #include <CapacitiveSensor.h>
  3.  
  4. /*
  5. * CapitiveSense Library Demo Sketch
  6. * Paul Badger 2008
  7. * Uses a high value resistor e.g. 10M between send pin and receive pin
  8. * Resistor effects sensitivity, experiment with values, 50K - 50M. Larger resistor values yield larger sensor values.
  9. * Receive pin is the sensor pin - try different amounts of foil/metal on this pin
  10. */
  11.  
  12.  
  13. 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
  14. CapacitiveSensor cs_4_6 = CapacitiveSensor(4,6); // 10M resistor between pins 4 & 6, pin 6 is sensor pin, add a wire and or foil
  15. CapacitiveSensor cs_4_8 = CapacitiveSensor(4,8); // 10M resistor between pins 4 & 8, pin 8 is sensor pin, add a wire and or foil
  16.  
  17. Servo myservo; // create servo object to control a servo
  18.  
  19. #define ServoM 12 //Connected to the servo motor.
  20. #define Bright 11 //servo library disable PWM on pins 9 and 10.
  21. #define Exit 9 //Pin connected to the EXIT button.
  22. #define In 8 //Pin connected to the IN button.
  23.  
  24. #define BarLow 177 //Low position of the barrier.
  25. #define BarUp 95 //Up position of the barrier.
  26. #define CAPACITY 8 //Capacity of the parking lot.
  27. #define INTEN 80 //Display intensity %
  28.  
  29. //Pins conections to segments (cathodes).
  30. #define segA 0
  31. #define segB 1
  32. #define segC 2
  33. #define segD 3
  34. #define segE 4
  35. #define segF 5
  36. #define segG 6
  37.  
  38. //Array with the segments to represent the decimal numbers (0-9).
  39. byte segments[10] = {
  40. // pgfedcba <--- segments
  41. B00111111, // number 0
  42. B00000110, // number 1
  43. B01011011, // number 2
  44. B01001111, // number 3
  45. B01100110, // number 4
  46. B01101101, // number 5
  47. B01111101, // number 6
  48. B00000111, // number 7
  49. B01111111, // number 8
  50. B01101111 // number 9
  51. };
  52.  
  53. void setup(){
  54. myservo.attach(ServoM); // attaches the servo.
  55.  
  56. pinMode(Exit, INPUT); // set "EXIT" button pin to input
  57. pinMode(In, INPUT); // set "IN" button pin to input
  58. digitalWrite(Exit, HIGH); // Connect Pull-Up resistor.
  59. digitalWrite(In, HIGH); // Connect Pull-Up resistor.
  60. pinMode(segA,OUTPUT);
  61. pinMode(segB,OUTPUT);
  62. pinMode(segC,OUTPUT);
  63. pinMode(segD,OUTPUT);
  64. pinMode(segE,OUTPUT);
  65. pinMode(segF,OUTPUT);
  66. pinMode(segG,OUTPUT);
  67. pinMode(Bright,OUTPUT);
  68. analogWrite(Bright,255*INTEN/100);
  69. myservo.write(BarLow); //Barrier in the low position
  70. // delay(1000);
  71. cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 1 - just as an example
  72. Serial.begin(9600);
  73. }
  74.  
  75. int Available= 9; // Number of places available.
  76.  
  77. //================================================================
  78. void loop(){
  79. Display(Available);
  80. if(digitalRead(In)==0)
  81. {
  82. if(Available != 0){
  83. Available--;
  84. myservo.write(BarUp);
  85. delay(3000);
  86. myservo.write(BarLow);
  87. }
  88. }
  89. if(digitalRead(Exit)==0)
  90. {
  91. if(Available != CAPACITY){
  92. Available++;
  93. myservo.write(BarUp);
  94. delay(3000);
  95. myservo.write(BarLow);
  96. long start = millis();
  97. long total1 = cs_4_2.capacitiveSensor(30);
  98. long total2 = cs_4_6.capacitiveSensor(30);
  99. long total3 = cs_4_8.capacitiveSensor(30);
  100.  
  101. Serial.print(millis() - start); // check on performance in milliseconds
  102. Serial.print("t"); // tab character for debug windown spacing
  103.  
  104. Serial.print(total1); // print sensor output 1
  105. Serial.print("t");
  106. Serial.print(total2); // print sensor output 2
  107. Serial.print("t");
  108. Serial.println(total3); // print sensor output 3
  109.  
  110. delay(10); // arbitrary delay to limit data to serial port
  111. }
  112. }
  113. }
  114.  
  115. /*-------------------------------------------------------------------
  116. Put the segments according to the number.
  117. --------------------------------------------------------------------*/
  118. void Display(int number){
  119. byte segs = ~segments[number]; //"~" is used for commom anode.
  120.  
  121. digitalWrite(segA, bitRead(segs, 0) );
  122. digitalWrite(segB, bitRead(segs, 1) );
  123. digitalWrite(segC, bitRead(segs, 2) );
  124. digitalWrite(segD, bitRead(segs, 3) );
  125. digitalWrite(segE, bitRead(segs, 4) );
  126. digitalWrite(segF, bitRead(segs, 5) );
  127. digitalWrite(segG, bitRead(segs, 6) );
  128. }
Add Comment
Please, Sign In to add comment