Charging and discharging a capacitor