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 » GEE handheld system program help......

June 07, 2010
by Master_Programmer
Master_Programmer's Avatar

Okay, i've made a game system out of the nerdkits, and i have made a program......and i need someone to point out what all i did wrong in my program.

(DOORS.C) // dip_arithmetic.c // for NerdKits with ATmega168 // hevans@mit.edu

 #define F_CPU 14745600

 #include <stdio.h>
 #include <math.h>

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

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

 // PIN DEFINITIONS:
 // 
 // PC0 -- DIP SWITCH DP1
 // PC1 -- DIP SWITCH DP2
 // PC2 -- DIP SWITCH DP3
 // PC3 -- DIP SWITCH DP4
 // PC4 -- DIP SWITCH DP5
 // PC5 -- DIP SWITCH DP6
 //
 // NOTE: (switches connects to GND when closed, otherwise
 // the pin is internally pulled up through a pull-up resistor to VCC)

int main() {
  // start up the LCD
  lcd_init();
  lcd_home();

  // Set the 6 pins to input mode - Two 3 bit numbers  
  DDRC &= ~(1<<PC0); // set PC0 as input
  DDRC &= ~(1<<PC1); // set PC1 as input
  DDRC &= ~(1<<PC2); // set PC2 as input
  DDRC &= ~(1<<PC3); // set PC3 as input
  DDRC &= ~(1<<PC4); // set PC4 as input
  DDRC &= ~(1<<PC5); // set PC5 as input

  // turn on the internal resistors for the pins

  PORTC |= (1<<PC0); // turn on internal pull up resistor for PC0
  PORTC |= (1<<PC1); // turn on internal pull up resistor for PC1
  PORTC |= (1<<PC2); // turn on internal pull up resistor for PC2
  PORTC |= (1<<PC3); // turn on internal pull up resistor for PC3
  PORTC |= (1<<PC4); // turn on internal pull up resistor for PC6
  PORTC |= (1<<PC5); // turn on internal pull up resistor for PC7

  // declare the variables to represent each bit, of our two 3 bit numbers
  uint8_t a1;
  uint8_t a2;
  uint8_t a3;

  uint8_t b1;
  uint8_t b2;
  uint8_t b3;

  uint8_t score;
  uint8_t level;

  uint8_t dead;
  uint8_t action;

 while(1) {

   while(a1 < 1) {

      // (PINC & (1<<PC0)) returns 8 bit number, 0's except position PC0 which is
      // the bit we want
      // shift it back by PC0 to put the bit we want in the 0th position.

  a1 = (PINC & (1<<PC0)) >> PC0;
  a2 = (PINC & (1<<PC1)) >> PC1;
  a3 = (PINC & (1<<PC2)) >> PC2;

  b1 = (PINC & (1<<PC3)) >> PC3;
  b2 = (PINC & (1<<PC4)) >> PC4;
  b3 = (PINC & (1<<PC5)) >> PC5;

  lcd_line_one();
  lcd_write_string(PSTR("     DOOR MASTER    "));

  lcd_line_two();
  lcd_write_string(PSTR("  SWITCH 1 = START  "));

  lcd_line_three();
  lcd_write_string(PSTR("PAULSOFT -  6/7/2010"));
  delay_ms(5000);

   }
      lcd_home();
    while(dead == 0) {
  delay_ms(5000);
      a1=a2=a3=b1=b2=b3 = 0;
      lcd_clear_and_home();
     while(action == 0) {

  a1 = (PINC & (1<<PC0)) >> PC0;
  a2 = (PINC & (1<<PC1)) >> PC1;
  a3 = (PINC & (1<<PC2)) >> PC2;

  b1 = (PINC & (1<<PC3)) >> PC3;
  b2 = (PINC & (1<<PC4)) >> PC4;
  b3 = (PINC & (1<<PC5)) >> PC5;

      lcd_home();
  lcd_line_one();
  lcd_write_string(PSTR("Score:"));
  lcd_write_int16(score);

  lcd_line_two();
  lcd_write_string(PSTR("Level:"));
  lcd_write_int16(level);

  lcd_line_three();
  lcd_write_string(PSTR(" Door 1? "));

  lcd_line_four();
  lcd_write_string(PSTR(" Door 2? "));

     if(a1 == 1 && a2 == 0) {
     a1 = 0;
     a2 = 0;
     action = 1;
     }

     if(a1 == 0 && a2 == 1) {
     a1 = 0;
     a2 = 0;
     action = 2;
     }

      }
  if(score < 10000){
   score = score + 10;
   level = level +1;
   } else {
   dead = 1;
   }

     }

 }

   return 0;
 }

i'm okay at using c++, but when it comes to programming an ATMEGA168, i have no clue of what i'm doing, and just need pointed "in the right direction" as some would say. :D

June 07, 2010
by Master_Programmer
Master_Programmer's Avatar

and yes, i modified another 2 programs to make this one......having the no clue that i do.......

June 07, 2010
by Master_Programmer
Master_Programmer's Avatar

DOORS.c: In function 'main': DOORS.c:102: warning: 'dead' is used uninitialized in this function DOORS.c:59: warning: 'a1' may be used uninitialized in this function DOORS.c:71: warning: 'action' may be used uninitialized in this function DOORS.c:68: warning: 'level' may be used uninitialized in this function DOORS.c:67: warning: 'score' may be used uninitialized in this function

was the warnings i got. the game compiles and goes to the MCU just fine, but i get a blank screen, or the dip switch does nothing either way.

June 07, 2010
by hevans
(NerdKits Staff)

hevans's Avatar

Hi Master_Programmer,

I think the problem might actually be that you are not initializing your local variables at all. In general you want to make sure you have set the value of the variable before trying to use it. Although most of the time you will be lucky and they get initialized to 0, I think in this case since they are local variables on the stack they actually have a non 0 value. Since a1 and dead are both non 0 your program is just zipping around the main while loop. At least that is what I think is happening. Try setting all your local variables to 0, and let us know what happens.

I also like to write "Initializing" or something similar to the the LCD towards the beginning of my code, just so I can be sure the program is running at all.

Humberto

June 07, 2010
by Master_Programmer
Master_Programmer's Avatar

thanks a lot! i'll give your idea a try, and hope it works, caus if it does, my dad will be really proud of me!!!!!

June 07, 2010
by Master_Programmer
Master_Programmer's Avatar

OH MY GOSH!!!! OH MY GOSHOH MY GOSHOH MY GOSHOH MY GOSHOH MY GOSHOH MY GOSHOH MY GOSHOH MY GOSHOH MY GOSHOH MY GOSHOH MY GOSHOH MY GOSHOH MY GOSHOH MY GOSHOH MY GOSHOH MY GOSHOH MY GOSHOH MY GOSHOH MY GOSHOH MY GOSHOH MY GOSH it almost worked(i have a while loop messing up still) but it almost worked fully!!!!!!!!! the first game system i have ever made, and the first game for it!!!! my project is now 80% done!!!!! thank you SOOOOOOOO much hevans!!!!!!! THANK YOU!!!! hands you a happy cookie XD

Post a Reply

Please log in to post a reply.

Did you know that you can build an analog amplifier with one transistor and a few resistors? Learn more...