Advertisement
learnelectronics

Dave_Moore

Oct 24th, 2017
556
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.09 KB | None | 0 0
  1. #include <Servo.h>
  2. Servo myservo;
  3.  
  4. int pos = 0;
  5. int trigger_rpm = 1000;
  6. int val;                                                              
  7. long last=0;                                                          
  8. int stat=LOW;                                                        
  9. int stat2;                                                            
  10. int counter=0;                                                        
  11. int sens=75;                                                                                                                
  12. int milisecs=500;
  13.  
  14. void open_valve(){
  15.  
  16.   for (pos = 0; pos <= 180; pos += 1) {
  17.     // in steps of 1 degree
  18.     myservo.write(pos);              
  19.     delay(15);                      
  20.   }
  21.   for (pos = 180; pos >= 0; pos -= 1) {
  22.     myservo.write(pos);              
  23.     delay(15);                      
  24.   }
  25. }
  26.  
  27. void setup() {
  28.  
  29. Serial.begin(9600);                                                
  30. pinMode(13,OUTPUT);
  31. myservo.attach(2);
  32. }
  33.  
  34. void loop() {
  35.  
  36.  val=analogRead(0);                                                  
  37.   if(val<sens)                                                        
  38.     stat=LOW;                                                        
  39.    else                                                              
  40.     stat=HIGH;                                                        
  41.    digitalWrite(13,stat);                                            
  42.                          
  43.  
  44.    if(stat2!=stat){                                                  
  45.                      
  46.      counter++;                                                      
  47.      stat2=stat;                                                      
  48.    }
  49.    if(millis()-last>=milisecs){                                      
  50.            
  51.      double rpm=((double)counter)/2.0*60000.0/(milisecs);      
  52.      Serial.println(rpm);
  53.      if (rpm > trigger_rpm){
  54.          open_valve();                                      
  55.      }
  56.      
  57.      counter=0;                                                      
  58.      last=millis();
  59.    }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement