NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Simple Controlling Devices With PC

August 15, 2009
by Nerdful_com
Nerdful_com's Avatar

Here I have made a simple project that only turns on/off 5 LEDs. I have tried to make it as simple as possible, almost like a template to start off a new project.

/* 
Made by www.Nerdful.com and visit www.FotoTrix.com
Turn on 5 LEDs using keyboard keys 1,2,3,4,5
Turn off 5 LEDs using keyboard keys q,w,e,r,t
*/

#define F_CPU 14745600
#include <avr/io.h>
// #include <avr/pgmspace.h>  // needed for PSTR with printf_P
#include "../libnerdkits/uart.h"

int main() {
DDRB |= ((1<<PB1) | (1<<PB2) | (1<<PB3) | (1<<PB4) | (1<<PB5)); // set PB1 to PB5 as output
uart_init();
FILE uart_stream = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
stdin = stdout = &uart_stream;
while(1) {
char input = uart_read();
switch (input) {
case '1' : 
PORTB |=(1<<PB1); // turn LED on
break;
case '2' : 
PORTB |=(1<<PB2); // turn LED on
break;
case '3' : 
PORTB |=(1<<PB3); // turn LED on
break;
case '4' : 
PORTB |=(1<<PB4); // turn LED on
break;
case '5' : 
PORTB |=(1<<PB5); // turn LED on
break;
case 'q' : 
PORTB &= ~(1<<PB1); //turn LED off
break;
case 'w' : 
PORTB &= ~(1<<PB2); //turn LED off
break;
case 'e' : 
PORTB &= ~(1<<PB3); //turn LED off
break;
case 'r' : 
PORTB &= ~(1<<PB4); //turn LED off
break;
case 't' : 
PORTB &= ~(1<<PB5); //turn LED off
break;
// default :
// printf_P(PSTR("Unknown command - www.FotoTrix.com\r\n"));
}
}
return 0;
}
August 15, 2009
by Nerdful_com
Nerdful_com's Avatar

In this demo I put 5 LEDs in PB1 to PB5 (MCU pins 19, 18, 17, 16, 15) and then assign numbers 1 to 5 on PC keyboard to turn on each individual LED and keys q,w,e,r,t to turn off each of the lights. This project communicates with your PC through a RS232 serial interface that came with your NerdKit AVR kit and all parts used came with the kit too!

Next I am creating a Visual Basic 6 application using MSComm for Windows where you can click a button to send the commands (will post a link to VB6 project when I am done). I could even make it so there was a web based interface on my web server so people could turn the LEDs on and off over the internet with ASP, PHP or Perl.

August 15, 2009
by Nerdful_com
Nerdful_com's Avatar

Ok, I promised I would post the source code for a Visual Basic (VB6) project. Also, included is a compiled version (used COM port 5).

Download VB6 5 LED Controller for Windows users.

Simply click a simple button to send the commands (turn each individual light on or off, turn all the lights off/on at once) to our NerdKit. You may have to change the COM port (I am using COM5 in my example).

August 16, 2009
by Nerdful_com
Nerdful_com's Avatar

Here is a VIDEO of it in action.

alt image text By Nerdful.com

August 17, 2009
by jbremnant
jbremnant's Avatar

nice! that's hot. ;-)

August 26, 2009
by Nerdful_com
Nerdful_com's Avatar

I found a way for an old PC like a 286 or 386 to control the PC as long as the old clunker has MSDOS 6.22 (Micrososft DOS) or greater (even works in command prompt in Windows XP). Here is a sample DOS batch file that will force the 5 LEDs to become a light chaser (lights go on and off and chase each other back and forth, loops infinitely until you close the cmd.exe window). Good way to put some of those old obsolete PCs back into use instead of the garbage bin or waste dumps (better than recycling I think).

Download DOS-Control-LED-on-off-with-PC.bat

August 26, 2009
by wayward
wayward's Avatar

Hey Nerdful,

that's nice! But, are you sure it will work on a pure DOS machine? Is there a PL2303 driver for DOS? USB is covered, but I couldn't find a DOS driver for PL2303.

Sounds like a fun project, though. Too bad I left my 8088 XT compatible back home. :)

August 27, 2009
by Nerdful_com
Nerdful_com's Avatar

Our kit has serial interface, couldn't that plug in to a serial port directly without the USB dongle? Most old PCs I have seen have a serial port (how most of my mice were before ps2/usb).

I am not sure, I was just assuming that this would work. I do not have any PCs around that old to test on, my oldest is a Pentium 4 and it does not even have a serial port.

Thanks for bringing it up though, I wish I knew 100%.

August 27, 2009
by kostelacj
kostelacj's Avatar

Does anyone have any ideas about how one might use a MAC OSX to do the controlling part? The 168 part is so clear and simple, but talking with the chip programming using MAC is not so clear. Esp. since I still lose contact with the chip quite frequently. More times than not during the programming of the chip I lose connection just after the download, but before the verification has completed. So I am still looking for a good way to establish and maintain communications with the chip via the PL2303 that came with the kit.

Thanks,

John

August 27, 2009
by wayward
wayward's Avatar

facepalm

Yes, you are right.

August 27, 2009
by Nerdful_com
Nerdful_com's Avatar

Control AVR Over Internet:

I forgot to mention that I came up with a basic active server page (ASP) script that runs on WIndows server OS (code simply turns on LED1). Here it is:

<%
'Simple ASP script to turn LED 1 on.
'Control AVR microcontroller Atmel Atmega168 VIA web site.
'You may have to change the COM port.
'Your web host/server should have ASP classic enable of ASP.net.
'Name file with extension *.asp (not *.aspx).
'Made by www.NERDFUL.com

set NeRdFuL = CreateObject("WScript.Shell") 
NeRdFuL.run "%COMSPEC% /C echo 1 > com5", 0, TRUE 
set NeRdFuL = nothing 
%>
September 09, 2009
by Nerdful_com
Nerdful_com's Avatar

VOICE RECOGNITION FOR AVR:

Here is a quick blog about using speech recognition to control your projects over UART, visit nerdful.com/nerd/blog.asp?avr=26

October 01, 2009
by Farmerjoecoledge
Farmerjoecoledge's Avatar

Nerful, i'm missing a piece. This has got the potential to be the start of something good. So i've got the project.c loaded and pushing the buttons and nothing's happening. The download you give for the VB6 controller is missing (component mscomm32.ocx or one of it's dependantcies is not correctly registered, a file is missing or invalid) Before that the 1,2,3,4,5, should just work, no? I just saw your post and jumped right in so i probably missed something, If you could clear it up that would be great.

farmerjoe

October 01, 2009
by Farmerjoecoledge
Farmerjoecoledge's Avatar

That's Nerdful not Nerful! This is good, i got them on with the echo 1 > com3, now i can't find the off command. ">" equals on, what's for off?

fjc

October 01, 2009
by mcai8sh4
mcai8sh4's Avatar

Farmerjoecoledge : This is just a guess (I haven't tried this), but "echo 1 > com3" means send '1' to com3. The > char redirects the output from the previous command ('echo 1') and sends it elsewhere (to com3 in this case).

Have you tried 'echo 0 > com3'? This is assuming that the program recognises 1==ON and 0==OFF.

I may have done what you did - just had a quick read then jumped in, so I also may have missed something :)

October 01, 2009
by mcai8sh4
mcai8sh4's Avatar

sorry - after looking at the code (which I should have done initially : -10 points for me) if '1' turns LED1 on then 'q' turns LED1 off!! so...

echo q > com3
October 01, 2009
by Farmerjoecoledge
Farmerjoecoledge's Avatar

This is sweet. mcai8sh4 you got good eyes, i guess it helps where to look and what to look for. I'm blind in one eye and can't see with the other. It totally makes sence when you think about it though. So Great idea, lots of potencial. It would be alot faster with the key board as described, don't quite understand why it doesn't work that way, yet. If you know, give a shout.

fjc

October 01, 2009
by mcai8sh4
mcai8sh4's Avatar

Glad you got it working.

When you say "faster with the keyboard" do you mean by just pressing 1 or q...?

If thats the case then there should be a number of ways of doing it.

Depending on operating system (lets assume windows, as most people use this) one method would be to write a batch script, like Nerdfuls above, but one that accepts inputs.

Something along the lines of this (note I've stole this from a random website and altered it - batch files where never my forte)

@echo off
:menu
cls
echo.
echo       1 - LED ON
echo       q - LED OFF
echo       X - End
echo.
choice /c:1qx > nul
if errorlevel 3 goto end
if errorlevel 2 goto OFF
if errorlevel 1 goto A
echo Error... choice not installed
goto end
:ON
echo 1 > com3
pause
goto menu
:OFF
echo q > com3
pause
goto menu
:end

Please note, I haven't a clue if this would work, but hopefully you get the idea. Also I'm sure there are some users out there who will curse me for that sloppy method - but it's years since I touched dos.

If you want to try this (don't hold your breath expecting it to work though), then simply copy the above code into a text file, with the extension ".bat"

October 02, 2009
by Farmerjoecoledge
Farmerjoecoledge's Avatar

Nerdful, where did you go? Your the one who started this. Your above code is writen for the key board, but it don't work. Is there a catch? mcai8sh4 thanks for your code but the Nerdful's original code should work. And like i said the vb6 controller don't work either.

October 02, 2009
by mcai8sh4
mcai8sh4's Avatar

fjc : No worries. I haven't seen Nerdul around for a while. He used to be in the IRC channel most nights - but hasn't been on for over 2 weeks.

I can't help with VB (never tried to program in that).

Best of luck to you.

October 02, 2009
by dangraham
dangraham's Avatar

I haven't tested the code yet, are you sending the keys through a terminal session to the serial port?

I am on vista, MS dropped hyper terminal when they went to vista (was included in XP) you can download a trial version from Hillgrave but only lasts 30 days. Or alternatively you can use hypertm.exe & .dll from XP and copy to vista PC. Then you can use on vista. This is how I am using but is not 100% programming is ok but tx/rx with running app can drop data.

The web server is a good idea because I have a server running 24/7 for business. on my final application I will connect nerdkit to this and use the web interface to monitor / control :-)

October 03, 2009
by Farmerjoecoledge
Farmerjoecoledge's Avatar

I fail to see how hyperterminal will work for this. Hyperterminal is for a phone is all i can see. This is turning out to be another waste. Now the command line echo don't work anymore, the little light on the programmer will light quickly but the lights don't come on anymore. So i've had enough of this one.

January 16, 2010
by treymd
treymd's Avatar

A little late to the show, but if you need a terminal emulator ala hyperterminal, puTTY is a great piece of software, and it is open-source, so no need to pay. Just google putty and the first hit is what you want. Putty has versions for both windows and linux, and presumably can be built for the Mac as well.

Putty is all you need to send/recieve to/from the serial port, and communicate with your nerdkit.

January 20, 2010
by saleem
saleem's Avatar

Hi Nerdful_com,

I used thay same VB 6 program to send data to the LED Marquee. I didn't need to change any part of your code. The only thing I changed was lower case letters to upper case, and the com port.

It is good. thanks.

Saleem

Post a Reply

Please log in to post a reply.

Did you know that microcontrollers have two different kinds of memory, program space (flash) and SRAM? Learn more...