Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2015
2,048
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.98 KB | None | 0 0
  1.  
  2. #define send_pin 12
  3. #define float_pin 14
  4.  
  5. #define NONE     0
  6. #define PRESS    1
  7. #define HOLD     2
  8. #define RELEASE  3
  9.  
  10. uint8_t count = 0;
  11. uint8_t decount = 0;
  12. uint8_t stat = NONE;
  13. uint8_t last_stat = NONE;
  14. long timer = 0;
  15.  
  16. #define DEBUG_CAPSENSE 1
  17.  
  18.  
  19. void setup(){
  20.       Serial.begin(115200);
  21.       pinMode(float_pin,INPUT);
  22.       pinMode(send_pin,OUTPUT);
  23.       analogWrite(send_pin, 512); //I don't know if this is the right value
  24.       Serial.println("Start");
  25. }
  26.  
  27. void loop(){
  28.  
  29. Serial.print(CapSense(float_pin, send_pin, 3, 2500, 1));
  30. delay(50);
  31.  
  32. }
  33.  
  34. uint8_t CapSense(uint8_t receive_p, uint8_t send_p, uint8_t thresold, uint8_t holdtime, boolean holdrepeat) {
  35.    
  36.     boolean Read = digitalRead(receive_p);
  37.        
  38.     if(DEBUG_CAPSENSE){
  39.         if(millis()%300==0) Serial.println("Capsense\tstat\tDRead\tcount\tdecount");
  40.         Serial.print(stat);
  41.         Serial.print("\t");
  42.         Serial.print(digitalRead(float_pin));
  43.         Serial.print("\t");
  44.         //Serial.print(CapSense(float_pin, send_pin, 3, 2500, 1));
  45.         //Serial.print("\t");
  46.         Serial.print(count);
  47.         Serial.print("\t");
  48.         Serial.print(decount);
  49.         Serial.print("\t");
  50.         Serial.println(timer);
  51.     }
  52.    
  53.     if(Read){// && stat == NONE){
  54.        count++;
  55.        decount = 0;
  56.     }else count=0;
  57.    
  58.     if(!Read && (stat == PRESS || stat == HOLD)){
  59.         decount++;
  60.     }
  61.        
  62.     if(count >= thresold && stat == NONE){
  63.         count = 0;
  64.         decount = 0;
  65.         stat = PRESS;
  66.         timer = millis();
  67.         return PRESS;      
  68.     }
  69.     if(stat == PRESS && millis()-timer >= holdtime){
  70.         stat = HOLD;
  71.         return HOLD;
  72.     }  
  73.    
  74.     if(stat == RELEASE){
  75.         stat = NONE;
  76.         return NONE;
  77.     }
  78.     if(decount >= 15){
  79.         decount = 0;
  80.         stat = RELEASE;
  81.         return RELEASE;
  82.     }
  83.    
  84.     if(stat == HOLD && holdrepeat){
  85.         return HOLD;  
  86.     }
  87.    
  88.     return NONE;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement