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 » Function help

April 19, 2011
by missle3944
missle3944's Avatar

Hey guys, Im making a new project that is a game that when you push a button on pc3 I want the character to move down to the line below it. I attempted to create a function for everytime it is on a line it adds one to it so it should move down. But I cannot retrieve the int ans to tell it to move down one line. Here is my function:

ans(int y)
{
    int ans;    //holds the answer that will be returned
    ans = 1 + y;    //calculate the sum
    return ans;     //return the answer
}

y is the constant and ans is the sum when I add 1 to y. im not too expienced on functions let alone pointers. I used the

lcd_goto_position

function.

This is were I try to recall the function sum and I try to put it in goto_position:

if (counterb == 1){
      lcd_line_one();
      lcd_goto_position(ans, x);
      fprintf_P(&lcd_stream, PSTR(" test"));

Here is the rest of my code. It is still in improvement with a few to many if statements.

    #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"
    #include "../libnerdkits/delay.h"
    #include "../libnerdkits/lcd.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:
    //
    // PC4 -- LED anode

    ans(int y)
    {
        int ans;    //holds the answer that will be returned
        ans = 1 + y;    //calculate the sum
        return ans;     //return the answer
    }

    int main() {
    uint8_t mode_selectb;
    uint8_t mode_select;

         lcd_init();
      FILE lcd_stream = FDEV_SETUP_STREAM(lcd_putchar, 0, _FDEV_SETUP_WRITE);
      lcd_write_string(PSTR("Dan's Racing GAME"));
      delay_ms(1000);
      // init LCD

    // Set the pin to input mode for Pushbutton
      DDRC &= ~(1<<PC3); // set PC2 as input

      // turn on the internal resistor for the pin
      PORTC |= (1<<PC3); // turn on internal pull up resistor

    // Set the pin to input mode for Pushbutton
      DDRC &= ~(1<<PC2); // set PC2 as input

      // turn on the internal resistor for the pin
      PORTC |= (1<<PC2); // turn on internal pull up resistor

    // declare the variables to represent pushbutton input

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

    mode_selectb = (PINC & (1<<PC3)) >> PC3;

     if (mode_selectb == 1) // Pushbutton switch OPEN**************************

    {
     lcd_write_string(PSTR(" P2 PRESS TO PLAY"));
        delay_ms(2000);
        }

    if (mode_select == 1) // Pushbutton switch OPEN**************************

    {
       lcd_line_three();
     lcd_write_string(PSTR(" P1 PRESS TO PLAY"));
        delay_ms(2000);
        } 
      int x = 1;
      int y = 1;

      int jumpup = 1;
      int counter = 0;
      int counterb = 0;
      //WHHHHHHHHHHHHHHHHHHHHHIIIIIIIIIIIIIIIIIIIIIIILLLLLLLLLLLLLLLLLLLLLLLLEEEEEEEEEEEEEEEE***************
      while(1) {

      lcd_init();
      lcd_home();

      uart_init();
      FILE uart_stream = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
      stdin = stdout = &uart_stream;

    mode_selectb = (PINC & (1<<PC3)) >> PC3;
    mode_select = (PINC & (1<<PC2)) >> PC2;

    if (mode_selectb == 0) // Pushbutton switch "******************OPEN*******************" (tied to GND) 
    {

     counterb += 1;
      }
     if (counterb == 1){
      lcd_line_one();
      lcd_goto_position(ans, x);
      fprintf_P(&lcd_stream, PSTR(" test"));

    }

     if (counterb == 2){
      lcd_line_three();
      lcd_write_string(PSTR("   car2"));//5
             delay_ms(500);
            }   
       if (counterb == 3){
      lcd_line_three();
      lcd_write_string(PSTR("     car2"));//5 
      delay_ms(500);

    }
    if (counterb == 4){
      lcd_line_three();
      lcd_write_string(PSTR("     car2"));//5 
      delay_ms(500);

    }

    if (counterb == 5){
      lcd_line_three();
      lcd_write_string(PSTR("     Car2 WINS"));//5 
      delay_ms(500);
            }

    if (counterb == 5){
      lcd_line_three();
      lcd_goto_position(y, x);
      fprintf_P(&lcd_stream, PSTR("--"));
            }

    if (mode_select == 0) // Pushbutton switch "******************OPEN*******************" (tied to                    GND) 
    {

     counter += 1;
      }

      if (counter == 1){
      lcd_clear_and_home();
      lcd_line_two();
      lcd_write_string(PSTR(" car1"));//5 
          delay_ms(500);
         }

     if (counter == 2){
      lcd_line_two();
      lcd_write_string(PSTR("  car1"));//5
             delay_ms(500);
            }   
       if (counter == 3){
      lcd_line_two();
      lcd_write_string(PSTR("   Car1"));//5 
      delay_ms(500);

            }
        if (counter == 4){
      lcd_line_two();
      lcd_write_string(PSTR("   Car1"));//5 
      delay_ms(500);

            }

            if (counter == 5){
      lcd_line_two();
      lcd_write_string(PSTR("   Car1 WINS"));//5 
      delay_ms(500);

        }

       // Print the current servo position to the serial port.
        printf_P(PSTR(" %d\r\n"), counter);
      }

      return 0;
    }
April 19, 2011
by missle3944
missle3944's Avatar

A

April 20, 2011
by 6ofhalfdozen
6ofhalfdozen's Avatar

Heya Missle, I am not that good at C programming, but until one of the gurus can chime in, here are a couple things that seem a little outta place to me. I could be entirely wrong, but these are things that jump out at me.. hopefully there is something usefull to you in this post.

your declaration looks a little muddy to me. almost like you are confusing defining a variable with declaring a function.. So your code that looks like this, and somehow it doesn't seem right. And it looks like it never gets updated, run once and forgotten, and possibly the compiler is setting static values and mulching it.

ans(int y) {
 int ans;    //holds the answer that will be returned
 ans = 1 + y;    //calculate the sum     
 return ans;     //return the answer 
 }

As best I recall, for it to be a function you need something like this to start your function declaration.the preceding int to tell it what kind of thing it will actually return..

   int ans(int y) {

And then you would want to call it as a function and not a variable, like so...

  lcd_goto_position(ans(y), x);

But then there are a couple other things that show up.. for instance, if ans is to be a function called by events inside the main, the "y" needs to be updated at some point in time or else it will always be the same number output.

April 20, 2011
by missle3944
missle3944's Avatar

Hi 6ofhalfdozen,

Thanks for your reply.I never took that into consideration and I'll try your suggestion.

-missle3944

April 21, 2011
by missle3944
missle3944's Avatar

Finally I got it to work! THANK YOU YOUTUBE!

Post a Reply

Please log in to post a reply.

Did you know that a motor's no-load current at a given voltage is much less than it's resistance would suggest? Learn more...