Sunday, April 6, 2014

Batch Script: Delete all GIFs over 1 MB

Introduction:
Tumblr is a fun place, filled with pure insanity, madness and obsessing over minute changes in characters' facial expressions in a scene. Animated GIFs are a staple part of the Tumblr experience, and if you've ever tried your hand at their creation, you'll know the pain of getting the file to meet the size limitation. 

If you're anything like me, you have, on more than one occassion, ended up with a folder filled with a mixture of GIFs under and over that 1 MB threshold. Wouldn't it be nice to have an easy way to purge the useless ones, without resorting to mandrolic acts?

Now you can with this handy batch script I created!
Well, you can if you are using windows.

Files:
Batch Script: cleanup.bat
Text File: cleanup.txt

Usage: cleanup.bat <relative folder path>

How to use:
There are two ways to use this.
  1. Put cleanup.bat into the folder you want to clean up and double click to run. 
  2. Run it from command prompt, which allows you to specify a folder to delete from.
Both methods will create a log file in the working directory called "log.txt". You can change this by modifying the log variable in the batch file.

How it works:
The script is actually really simple and could be done in a single line using forfiles. I just wanted to add logging to it because it is nice to know what's going on. Unfortunately making nice logging means many extra lines and various error checks. But whatever, it is good scripting practice. I might mod it to make it show the total amount of files deleted at the end, but for now it just reports whether or not files were deleted and which ones.

Anyway, without logging, the code is simply:
forfiles /P . /M *.gif /C "cmd /c if @fsize gtr 1048576 (del @path)"
forfiles is a pretty darn powerful tool that scans through all files in a certain path, finds the ones that meet the search criteria (masks, date created etc), then performs a command of your choice on it. It even gives you nice variables to play with, like @fsize, which is the file size, and the @path, which is the path of the file. It's an awesome command, and I suggest you play around with it.

Conclusion:
I might tweak this code a little bit, including a bit more error checking and better logging. The important thing is that it works. Well, at least it works on my computer. 

Tuesday, January 18, 2011

What I Have Learnt

My parts for my clock finally arrived on Monday, so I have been quite busy prototyping my base-15 design. From this, I have learnt/remembered a few things, and I would like to share them here:


1. TTL 555 timers' rise and fall times sometimes aren't fast enough for 74LS393s. It is best to put the output through a Schmitt Trigger (74LS14) first, so to clean up the edges.


2. 74LS393's reset is high when inactive. You need to ground this pin if you are planning on letting the counter reset itself


3. Some ICs don't like clamping circuits


4. RGB LEDs need to be diffused if you want to use a mixture of colours.


5. Staring directly at non-diffused LEDs hurts the eyes.


No doubt I will learn/remember more things as I go along, but this is just what I learnt in the past two days. Hopefully they'll be no more annoyances.

Sunday, January 9, 2011

Base 15 Clock Design

My previous post outlined (sort of) my original idea for my digital clock. After practicing some woodwork, I realised that I am totally incapable of building what I wanted, so I decided to change my plans to work with my strengths, not my weaknesses. 


My "BCD" Design
So, a few minutes of pondering, I decided to make my own BCD display, but using symbols instead of numbers. After thinking some more, I decided that base 10 is boring, and base 15 would be a lot more interesting for time. 


I can already hear the cries of protest: Why base 15? Wouldn't 12 be better? Isn't base 12 awesome?


Well, when we think about minutes, the most commonly used times are quarter to, half-past, quarter past. These are all linked into fifteen minute intervals, hence base 15.


But, what would be an interesting way to show the most significant digit? Should I have unique symbols for them? No. How about colours?


That's right, each interval of 15 is represented in its own colour. Haven't decided which is which though, that is something I can play around with once my parts FINALLY arrive.


Base 15 symbols
I was originally going to work out 15 different "symbols" for 0 - 14, then work out the logic for it to work, but then I realised a quicker method. Hooking up each "panel" (each panel contains 3 LEDs) to a different output on the 74LS393 would generate symbols from 0 - 14 with little effort, and it would be very easy to implement.




Parts:
24 x RGB tri-colour LEDs (common anode)
12 x Dual colour LEDs (common anode)
1 x 74LS21
3 x 74LS393
5 x NPN transistors
3 x momentary on push buttons
1 x power button
1 x reset button
Various resistors
12 x 1000 uF capacitors
2 x 74LS08
1 x 5.1 V Zener Diode
1 x 9V battery








Tuesday, December 28, 2010

Binary Clock

This here is my current draft of my binary clock design, as requested by my friend a while ago (I'm slack).

This binary clock counts seconds, minutes and hours in binary and uses two different colours for AM and PM. 

Parts List:
3 x 74LS393 
2 x 74LS21
Various resistors
18 x 1000 uF capacitors
1 x 10 uF capacitor
1 x 0.1 uF capacitor
2 x NPN BJTs
4 x Momentary ON button switches (for changing the time)
1 x switch (for on and off)
1 x switch (for clearing time)
18 x LEDs of AM colour
18 x LEDs of PM colour
1 x 9 V battery

The battery and the switches are the most expensive part of this circuit (provided you don't buy ridiculously expensive LEDs). You could probably pick them up for a reasonable price on eBay, but the switches will probably be at least 50c each, and the battery is likely to cost you around $2. All up, it should cost around $10, not including wires, solder or circuit board, so all up, probably around $13. Buying the ICs, transistors, diodes and LEDs in bulk will make it cheaper in the long run, and then you'll have lots of spare parts in case you screw up, want to build another one, or if you want to make something else. The diodes in particular will come in handy.


Pins 3, 4, 5, 6, 10 and 11 of the first and second 74LS393 and pins 3, 4, 5 and 6 of the last 74LS393 are each connected to the following circuit:




The AM LED connects to the AM lead in the main schematic, and it is the same with the PM LED.


This schematic is liable to change at any moment, but if you have any questions, suggestions or queries, please leave a comment.

Saturday, November 6, 2010

Battery Powered ICs

All integrated circuits (ICs) that I have come across run off a specific voltage. Powering these with a power supply is as easy as choosing the correct voltage and plugging it in. But what if you want to use ICs with batteries?  How are you supposed to supply 5 V using a 9 V battery? Will 4 x AA batteries be able to run an IC specified to work at 5 V?


The first thing you want to do is look at the datasheet for your IC. These can be found online by simply typing in the IC code, for example a low-powered NOT gate is 74LS04, then the word "datasheet". There will be a section of maximum and minimum voltages. If you're lucky your batteries will be within this range, but most of the time it will fall outside. A pretty common operating range is 4.5 - 5.5 V. Whilst you can go ahead and put in 3 x AA batteries, to make 4.5 V, that would only power the circuit for a short time before it falls out of the threshold range (remember, batteries lose voltage as they are used). It is much better to go over the threshold range and use a voltage divider or clamp diodes to keep the voltage within the operating range.


Voltage dividers consist of two resistors. The voltage in to the IC is taken from the middle connection of the two resistors and the ground is, well, the ground. This means that the IC is in parallel to the second resistor, meaning that its voltage has to be the same as the voltage on that resistor. 
Because the voltage over series components is divided up (similar to currents in parallel), only a certain fraction of the total voltage exists at R2. This voltage can be calculated by Vs * R2/(R1+R2), or the supply voltage multiplied by R2 over the total resistance. In the circuit above, Vout would be equal to half of Vs.


So, if you had a 9 V battery and a IC which ran at 5 V, you could have R1 as 4k and R2 as 5k. 


5/9 * 9 = 5 V

Simple enough, but there is one major issue. What happens when the voltage of the battery drops? Say, down to 8 V...

5/9 * 8 = 4.44 V

After a drop of 1 V, this voltage divider cannot be used. Seems a waste of a perfectly good battery, no? This is where the magic of zener diodes come in. Simply replace R2 with a zener diode with a voltage within the operating range in reverse bias, and the circuit will work until the voltage of the battery drops below the zener diode's operating voltage. So, if you have a 5.1 V zener diode, your IC will run until the battery loses 3.9 V. Much better. R1 can be any value  you wish but it has to be there to drop the left over voltage. It does help limit the current, so chose a value that suits your needs.

Thursday, November 4, 2010

AND Gate Attempt

So, after designing a NOT Gate, I thought it'd be fun trying other gates, so this next attempt is for an AND Gate. AND Gates are only on if both inputs are on. This gave me the idea of using one of the transistors as a switch, and using that output to drive the base of the second transistor.
It does work. If either input is grounded the output is low. There is a problem though. Unless input 2 is actually grounded it gives a high result. TTL inputs are natural high though, but it is the inconsistency I am concerned about. Sure, this circuit works, but it isn't the best it could be. There's no doubt another way I could design this that would work exactly to my expectations. 


EDIT: This circuit was drawn incorrectly. Instead of ground, the emitter of Q1 should be connected to Vs. I will redraw this circuit at some point.

Improved NOT Gate

So after taking a break by drawing up flowcharts for my lab report, I was drawn back to the NOT Gate problem. I realised I could probably isolate the power supply by using another transistor, with the supply connected to the base and the emitter of the transistor. The collector would be connected to the output and to the collector of the second transistor. 


It works essentially the same way as the previous NOT gate, but it doesn't short the whole power supply. 


There you have it. A working NOT Gate. And it does actually work. I did test it.