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 » Moving object - possibility to make games, menus etc with

August 05, 2012
by TheFed
TheFed's Avatar

So here I was wondering how to make a moving object on the lcd screen without having to code every possible position of the object. It turned out to be rather easy. To show how it works and if it works i made a '-' move right across the screen whenever i pressed the button and then when it reached the end of the screen to go back to the begining. This is only what i did with it to see if it worked. You could easily make something that could jump, move left, right etc.

My circuit is really basic, the only thing you need to attach is the button switch. I attached it to the PB5 pin.

And heres the code (i just edited the initialload):

// initialload.c
// for NerdKits with ATmega168

#define F_CPU 14745600

#include <stdio.h>

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

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

// PC4 -- LED anode

uint8_t button;
int position;

void scr_clear_and_position() {

  position = position + 1;

 lcd_set_type_command(); //Clear the screen
  lcd_write_byte(0x01);  // clear the screen


int main() {

  // LED as output
  DDRC |= (1<<PC4);

  DDRB &= ~(1<<PB5); //set button as input

  PORTB |= (1<<PB5);

  // turn on LED
  PORTC |= (1<<PC4);

position = 0;

  // fire up the LCD

  // print message to screen
  //             20 columns wide:
  //                     01234567890123456789

  while(1) {
  if (button == 0) 

  if (position > 19)
    position = 1;

    button = (PINB & (1<<PB5)) >> PB5;

  lcd_write_string(PSTR("Button: "));
  lcd_write_string(PSTR("Position: "));

  return 0;

I plan to make a game with this principle and maybe some joysticks if i get around to it.

Note: The important parts of the code (the ones that make the object move) are the ones that concern the position variable, the button variable and that pretty much it. I hope you can use the idea and that it isnt already common knowledge that i happen to not know about yet.

August 05, 2012
by TheFed
TheFed's Avatar

Note: I also display the position on the screen and if the button is pressed or not. This is shown on line 2 of the lcd and line 3.

  • Fedde 'The Fed'

Post a Reply

Please log in to post a reply.

Did you know that you can build a giant scrolling LED array with our microcontroller kit? Learn more...