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);
- if (inputValue != lastInputValue) {
- if (inputValue == 1) {
- inputEndTime = millis();
- Serial.println("HIGH");
- currentDutyCycle = ( (float)inputSwitchTime - inputStartTime) / (inputEndTime - inputStartTime);
- Serial.print("Duty cycle: ");
- Serial.print(" inputStartTime: ");
- Serial.print(inputStartTime);
- Serial.print(" inputSwitchTime: ");
- Serial.print(inputSwitchTime);
- Serial.print(" inputEndTime: ");
- Serial.print(inputEndTime);
- Serial.print("Duty cycle: ");
- Serial.println((inputSwitchTime - inputStartTime) / (inputEndTime - inputStartTime));
- inputStartTime = inputEndTime;
- } else { //Switch
- inputSwitchTime = millis();
- Serial.println("LOW");
- }
- lastInputValue = inputValue;
- }
- if (currentDutyCycle >= dutyCycle) {
- filterOpen = true;
- } else {
- filterOpen = false;
- }
- if (filterOpen) {
- digitalWrite(outputPin, inputValue);
- } else {
- digitalWrite(outputPin, LOW);
- }
- /*
- Serial.print("Input is: ");
- Serial.print(inputValue);
- Serial.print(" Reading: ");
- Serial.print(digitalRead(inputPin));
- Serial.print(" Duty: ");
- Serial.print(currentDutyCycle);
- Serial.print(" Time data: ");
- Serial.println( inputEndTime - inputStartTime );
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement