C++, C and a Parallel port

About a month ago I went out and purchased a book on C++. I went through it and learned the basics. Yesterday I began work on a program to control inputs and outputs on the LPT port. The interesting part about this is I plan to wire some relays to control back yarded lighting, a few pond pumps and mabey even pool pump and heater.

In this testing stage I just wired some resisters and LED’s onto pins 0 to 7 on the parallel connector. The LED’s I used were 1.5V so I used some resisters to bring down the 5V. If you need any more detail on this feel free to e-mail me.

It’s fairly simple to make the program to run the parallel port. You just have to do some math to figure what values you send out to make certen LED’s light up. The following code is what I used to make sure all 8 outputs are working. It justs randomly flashes the LED’s wired to the parallel plug.

#include <stdio.h>
#include <stdlib.h>
#include <sys/io.h>

#define PORT 0x378 /* use your port address here */

int main(int argc, char *argv[])
{
	int r;
	double l;

	if (iopl(3)) /* you could also use ioperm(PORT, 1, 1) */
	{
		fprintf(stderr, "couldn't get ports,\ntry running as root\n");
		exit(1);
	}

	srand(time(NULL));
	while(1) /*use Ctrl+C to exit */
	{
		l = (double)rand()/(double)RAND_MAX*256;
		r = (int)l;
		outb(r, PORT);
		usleep(500000);
	}

	return 0;
}

I saved the following as test_lpt.c. The compiled the file with the fowloing command.

gcc -O test.c -o test_lpt

Then I executed the new test_lpt that I just compiled and made into a program.

sudo ./test_lpt

Now I had to use sudo because the parallel port only works if your logged in as root in the terminal. The program is a continues loop so to escape out of the loop just press Ctrl + C to shut down the program.

The next program shows how to light up LED’s one and two. We will be sending out a value of 3 to do this.

#define PORT 0x378 /* use your port address here */

int main(int argc, char *argv[])
{
if (iopl(3)) /* you could also use ioperm(PORT, 1, 1) */
{
fprintf(stderr, "couldn't get ports,\ntry running as root\n");
exit(1);
}
outb(3, PORT);
usleep(500000);
return 0;
}

The one peace of code we will be looking at is outb(). The first value, 3, is what value we are sending out to the parallel port. Now this value will tern the LED on Pin 0 and Pin 1. I recommend learning about binary if you plan on building this your self. The second part, PORT, is just specifying what parallel port to use. This was defined at the beginning of the code.

#define PORT 0x378 /* use your port address here */

Once compiled this program should execute then bring you back to the command line. Your LED’s should have changed.

To conclude, I plan on eventually building a board that will hold 8 relay that will let me control multiple devices executed via cronjobs in Ubuntu Linux. I will update this as the project continues.

Stereo to Mono Adapter

As most of you know I have a auxiliary amp that is also used as a second sub woofer. I have found lately that I don’t always want to plug my laptop into the 5.1 surround sound and blast the bass and every thing else through the house. So I have made an adapter for anything with a stereo controlled out put to be converter to Mono. (one channel)

Now here are the specs on the amp.

  • 1/4 inch input
  • 100 watts (single Channel)

This is what you going to need.

  • Stereo head phone cable (Left, Right and Ground)
  • 1/4 inch mono plug male
  • 1/4 inch stereo plug female (can be 3.5mm if you want to plug in a CD player or something
  • two 10k resistors
  • solder iron

Okay now I’ll lay out the rest of the process into steps.

Step 1

Cut a length of the head phone cable about 1 foot long. (or how ever long you need to go). Then strip the first jacket of insulation off then you should see one with no insulation, one with red insulation then another with white insulation. You will want to twist the ground (no insulation) together so there tight. Next strip the insulation off the red one then twist it together. Do the same for the white. Then do the other end.

Step 2

We are going to do the easy part first. Take the female stereo end and solder it to one end of the cable. Make sure the ground (no insulation) goes to the ground of the plug. Some are tabs right off the metal case.

Step 3

Next will be the complicating part. Take one of the 10k resistors and solder it to the red lead at the other end of the cable. Next take the second 10k resistor and solder it to the white lead. Then twist the ends of the resistors together. Make sure that before the resistors aren’t touching. I taped myna very well so they wouldn’t touch.

Step 4

Next solder you ground to the out side shield of the 1/4 inch male end of the plug. Then solder your twisted 10k resistors to the tip tab on the plug. Slip the covers over top of them. (or tape them 😛 )

Step 5 (only if you have the equipment)

Get out a voltmeter and set it to Ohms. Next you will want to check the grounds to make sure there not shorting out the audio.

Finally

Now if every thing checks out and you are confident, plug the adapter into your amps Line level input. Next tern the volume down on you devise. (CD player, Laptop, Tape deck, etc.) Remember to use the headphones output so you can control the volume. Then plug the other end into you devise. Now tern on the devise then the amp. Now run away just encase it ignites. 😛 Now insert a CD or whatever your using that as a part where it uses only the left or the right. In this case I used Queens – Fat Bottom Girlss .

That should be it. If you have any problems, feel free to post them on the forum. Here are a few pictures of the final product in use.

p1010002.JPG

p10100022.JPG

p1010006.JPG

p10100012.JPG

How to Clean your Keyboard… The fast way!

My dad finally toled me to clean the keyboard for the computer. Needles to say there was allot of dust around the keys… and in the keys. I came up with a very good idea to clean all 100 and sum keys.

First I started by popping off all the keys with a screw driver. Simple enough.
keyboard1.jpg
Next was taking out all the screws on the back so the top plate, that held all the keys in place, would come off.

Next, I cleaned the plate with water and some paper towels. I then began to clean the actual keys. After about the fifth key I got tired of cleaning the underneath with cu tips. So I filled the laundry tub up with water and through the all the keys in. I sat there with my hand for a wile trying to circulate them around, after about two minutes of that my arm fell asleep. So then I went into the crawly space for some idea. I came out with a pump that my parents use to pump the water in our pond out front. I through in a little orange soap then put the pump in the water, plugged it in and there we have in circulation.

keyboard2.jpg keyboard3.jpg

I left them for about an hour or so then I grabbed a few of them out of the tub and had a look at the under side. Good as new. No gunk build up, it was like the keyboard was brand new. Next I took out the keys and let them dry. Then a reassembled the keyboard.

To sum up. The easy way, is the best way!