Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- float dutyCycle = 0.4;
- float currentDutyCycle = 0;
- boolean filterOpen = false;
- boolean newWave = true;
- int inputPin = 6;
- int outputPin = 5;
- int lastInputValue = 0;
- int inputValue = 0;
- unsigned long inputStartTime = 0;
- unsigned long inputSwitchTime = 0;
- unsigned long inputEndTime = 0;
- void setup() {
- Serial.begin(9600);
- pinMode(inputPin, INPUT);
- pinMode(outputPin, OUTPUT);
- //Wait for first signal
- while ( digitalRead(inputPin) != HIGH ) {};
- Serial.println("Filter initializing!");
- }
- void loop() {
- inputValue = digitalRead(inputPin);
- //Check if the signal has changed
- if (inputValue != lastInputValue) {
- if (inputValue == 1) {
- //Signal is HIGH
- inputEndTime = millis();
- //Calculates the duty cycle
- currentDutyCycle = ( (float)inputSwitchTime - inputStartTime) / (inputEndTime - inputStartTime);
- inputStartTime = inputEndTime;
- } else {
- //Signal is low
- inputSwitchTime = millis();
- }
- lastInputValue = inputValue;
- }
- //Checks if filter is open or not
- if (currentDutyCycle >= dutyCycle) {
- filterOpen = true;
- } else {
- filterOpen = false;
- }
- //Determines output signal
- if (filterOpen) {
- digitalWrite(outputPin, inputValue);
- } else {
- digitalWrite(outputPin, LOW);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement