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.

Project Help and Ideas » My simple DIP switch + LED project

December 29, 2011
by Drwish
Drwish's Avatar

I just wanted to say thank you NerdKits for introducing me to the wonderful world of electronics. I was reading through the guide and decided to add a little bit of spice to the LED blink program. I just wanted to share my completed circuit as a result of studying how I/O on an MCU works. Again, thanks for introducing me to this field and enabling me to understand at least some parts of a Datasheet =).

Video: http://youtu.be/0Ymioj7f7tc

Source, for anyone interested:

// DIP Switch + LED circuit
//based on NerdKits' led_blink.c
//Omar Darwish

#define F_CPU 14745600

#include <avr/io.h>
#include <avr/pgmspace.h>

#include "../libnerdkits/delay.h"
#include "../libnerdkits/lcd.h"

// PIN DEFINITIONS:
//
// PC5 -- PB1 yellow LED switch
// PC4 -- PB2 yellow LED switch
// PC3 -- PB3 red LED switch
// PC2 -- PB4 green LED switch
//
// PB1 -- 1st yellow LED
// PB2 -- 2nd yellow LED
// PB3 -- red LEd
// PB4 -- green LED

int main() {
  //By default, all pins on Port C are set as input, so no need to specify.
  //turn on pull up resistors for the inputs
  PORTC = (1<<PORTC5) | (1<<PORTC4) | (1<<PORTC3) | (1<<PORTC2);
  //set LED pins as outputs
  DDRB = (1<<DDB1) | (1<<DDB2) | (1<<DDB3) | (1<<DDB4);

  lcd_init();

  // loop keeps looking forever
  while(1) {
    if((PINC &(1<<PINC5)) == 0){
        //if switch 1 is on, write LCD, and 
        //blink first yellow LED
        lcd_home();
        lcd_write_string(PSTR("SWITCH 1 ON "));
        PORTB |= (1<<PORTB1);
        delay_ms(100);
        PORTB &= ~(1<<PORTB1);
        delay_ms(100);
    } else{
        lcd_home();
        lcd_write_string(PSTR("SWITCH 1 OFF"));
        //don't do anything to the light if the switch is off
    }
    if((PINC & (1<<PINC4)) == 0){
        //if switch 2 is on write to LCD and
        //blink 2nd yellow LED
        lcd_line_two();
        lcd_write_string(PSTR("SWITCH 2 ON "));
        PORTB |= (1<<PORTB2);
        delay_ms(100);
        PORTB &= ~(1<<PORTB2);
        delay_ms(100);
    } else{
        lcd_line_two();
        lcd_write_string(PSTR("SWITCH 2 OFF"));
        //don't do anything to thelight if the switch is off
    }
    if((PINC & (1<<PINC3)) == 0){
        //if switch 2 is on, write to LCD
        //and blink red LED
        lcd_line_three();
        lcd_write_string(PSTR("SWITCH 3 ON "));
        PORTB |= (1<<PORTB3);
        delay_ms(100);
        PORTB &= ~(1<<PORTB3);
        delay_ms(100);
    } else{
        lcd_line_three();
        lcd_write_string(PSTR("SWITCH 3 OFF"));
        //don't do anything to the light if the switch is off
    }
    if((PINC & (1<<PINC2)) == 0){
        //if switch is on, write to LCD and
        //blink green LED.
        lcd_line_four();
        lcd_write_string(PSTR("SWITCH 4 ON "));
        PORTB |= (1<<PORTB4);
        delay_ms(100);
        PORTB &= ~(1<<PORTB4);
        delay_ms(100);
    } else{
        lcd_line_four();
        lcd_write_string(PSTR("SWITCH 4 OFF"));
        //don't do anything to the light if the switch is off
    }

  }

  return 0;
}
December 29, 2011
by SpaceGhost
SpaceGhost's Avatar

You're off to a great start. You could be amazed at what ideas you may start having while playing with the simple basics such as how the I/Os work. Start adding in counters, timers, PWM outputs for motor controls (that's just the quick list), etc... It's possible to build some pretty unique devices!

Heck, you're even reading the data sheet. I think you're in for a lot more fun. Looks like you're having a lot of fun already.

Dave

December 30, 2011
by SpaceGhost
SpaceGhost's Avatar

Hey, I just looked at your YouTube video - Pretty sweet!

Here's a link to a forum post I made almost a year ago (about a month after I got my kit) that you might find interesting. Scrolling menu selection

And this guy did it quite differently than I did - Menus and sub menus

I have since both simplified and improved the code I first posted, just ever got around to posting an update. I'll bet you will notice the ways my code could be modified and improved. The outputs in my code do not blink, but I know that you already know how to make them do that.

Anyway, after seeing your video I thought that you might find these topics interesting.

December 30, 2011
by Drwish
Drwish's Avatar

Thanks SpaceGhost,

The scrolling menu idea is a good one that I think I'll use with this "jukebox" I'm working on xD.

December 30, 2011
by Ralphxyz
Ralphxyz's Avatar

Hey Drwish, that video is so cool.

I remember having problems comprehending what was happing with the Dip Switch project, turned out I was looking at it backwards.

But I think your led lighting would be a good addition to the Nerdkit Dip Switch project. It really illustrates using a dip switch, I hope you will add it to the Nerdkit Community Library which I would hope someone who is into designing web pages would give us a fresh clean cool look, I did the initial screen just to get started, it really needs some cool graphic insight by someone with artistic/design capabilities.

Ralph

Post a Reply

Please log in to post a reply.

Did you know that our customers love us? Learn more...