Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.33 KB | None | 0 0
  1. float dutyCycle = 0.4;
  2. float currentDutyCycle = 0;
  3.  
  4. boolean filterOpen = false;
  5. boolean newWave = true;
  6.  
  7. int inputPin = 6;
  8. int outputPin = 5;
  9.  
  10. int lastInputValue = 0;
  11. int inputValue = 0;
  12.  
  13. unsigned long inputStartTime = 0;
  14. unsigned long inputSwitchTime = 0;
  15. unsigned long inputEndTime = 0;
  16.  
  17.  
  18. void setup() {
  19.   Serial.begin(9600);
  20.  
  21.   pinMode(inputPin, INPUT);
  22.   pinMode(outputPin, OUTPUT);
  23.  
  24.   //Wait for first signal
  25.   while ( digitalRead(inputPin) != HIGH ) {};
  26.  
  27.   Serial.println("Filter initializing!");
  28. }
  29.  
  30.  
  31. void loop() {
  32.   inputValue = digitalRead(inputPin);
  33.  
  34.   //Check if the signal has changed
  35.   if (inputValue != lastInputValue) {
  36.     if (inputValue == 1) {
  37.       //Signal is HIGH
  38.       inputEndTime = millis();
  39.       //Calculates the duty cycle
  40.       currentDutyCycle = ( (float)inputSwitchTime - inputStartTime) / (inputEndTime - inputStartTime);
  41.       inputStartTime = inputEndTime;
  42.     } else {
  43.       //Signal is low
  44.       inputSwitchTime = millis();
  45.     }
  46.     lastInputValue = inputValue;
  47.   }
  48.  
  49.   //Checks if filter is open or not
  50.   if (currentDutyCycle >= dutyCycle) {
  51.     filterOpen = true;
  52.   } else {
  53.     filterOpen = false;
  54.   }
  55.  
  56.   //Determines output signal
  57.   if (filterOpen) {
  58.     digitalWrite(outputPin, inputValue);
  59.   } else {
  60.     digitalWrite(outputPin, LOW);
  61.   }
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement