3 Replies - 218 Views - Last Post: 19 November 2012 - 09:48 AM Rate Topic: -----

#1 pgug  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-November 12

Need help, programming processor for small game

Posted 19 November 2012 - 01:21 AM

Hey people

I am about to write a program for a little game. It should turn on an random led, then you should puss a bottom right under the led before the time runs out. I thing you will get the idea when you read the code.

I have writed down in my code, what I can't do. By the way, this is my first projekt in C, so I don't know everything yet.

And an other note, for some reason my compiler complaings if I try to include time.h (I am using CodeVisionAVR).


//#define XXXXXXX  PORTA.0    // This port is shown as RESET
//#define XXXXXXX  PORTA.1    // This port is shown as X-tal1
//#define XXXXXXX  PORTA.2    // This port is shown as X-tal2

#define BUTTON1   PINB.0
#define BUTTON2   PINB.1
#define BUTTON3   PINB.2
#define BUTTON4   PINB.3
#define BUTTON5   PINB.4
#define BUTTON6   PINB.5
#define SPEAK   PORTB.6

#define LED1    PORTD.0
#define LED2    PORTD.1
#define LED3    PORTD.2
#define LED4    PORTD.3
#define LED5    PORTD.4
#define LED6    PORTD.5

#include <delay.h>
#include <tiny2313.h>
#include <stdlib.h>

void main()
{

    //Mame var's
    int randomnumber;
    int timetoclick;    

    //Define BUTTON's as turned of (enable build in recistor)
    DDRB    =   0B00100000;
    DDRD    =   0B00111111;
    PORTB   =   0B00111111;    
                               
    //Assign var
    timetoclick = 200;       
    randomnumber = rand() % 6;
    
    //If random number is 0 turn on led1
    if(randomnumber == 0)
    {
        LED1 = 1;
        
        if(KNAP1 == 1) {
            tid = tid - 2;
            //Here you should return to start, but how in the world do I do that?
        }                                                              
        
        (if somethingidontknow == timetoclick) { //I dont know how I make that if work=
            //All led's blink (I can make that happen ;-)
            //Back to start (How do I do that?)
        }
        
    } else {
        LED1 = 0; //Of course, if not that number, keep turned off
    }

    //If random number is 1 turn on led2
    if(randomnumber == 1)
    {
        LED2 = 1;
    } else {
        LED2 = 0;
    }
    
    if(randomnumber == 2)
    {
        LED3 = 1;
    } else {
        LED3 = 0;
    }            
    
    if(randomnumber == 3)
    {
        LED4 = 1;
    } else {
        LED4 = 0;
    }
    
    if(randomnumber == 4)
    {
        LED5 =  1;
    } else {
        LED5 = 0;
    }
    
    if(randomnumber == 5)
    {
        LED6 = 1;
    } else {
        LED6 = 0;
    }             
    
}




Is This A Good Question/Topic? 0
  • +

Replies To: Need help, programming processor for small game

#2 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Need help, programming processor for small game

Posted 19 November 2012 - 02:29 AM

For the places where you have commented "How to get Back to start? " , you can do that if you use loops. There are basically 3 types of loops. FOR, WHILE and DO-WHILE.

I am guessing that the DO-WHILE loop will be the best one for solving your problem.

regards,
Raghav

This post has been edited by raghav.naganathan: 19 November 2012 - 02:30 AM

Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon


Reputation: 4098
  • View blog
  • Posts: 12,682
  • Joined: 25-December 09

Re: Need help, programming processor for small game

Posted 19 November 2012 - 06:11 AM

Quote

And an other note, for some reason my compiler complaings if I try to include time.h (I am using CodeVisionAVR).

That's because your compiler has not implemented the items in that header.

Jim
Was This Post Helpful? 0
  • +
  • -

#4 pgug  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-November 12

Re: Need help, programming processor for small game

Posted 19 November 2012 - 09:48 AM

Thank you

So far I am came to a part, where my program seems to work, but for some reason the random number is not random, first it choase LED1 then LED5, and then LED1 again... Everything else work


/*****************************************************/
/*                                                   */
/* Gruppe: Gruppe pisse nice!                        */
/*                                                   */
/*                                                   */
/*                                                   */
/* Erstat XXXXXXX med navnet på jeres lysdiode/knap  */
/* Bemært at der hvor der er tale om INput - altså   */
/* knapper, ændres tillige "PORT" til "PIN"          */
/* F.eks.                                            */
/*                                                   */
/* #define LED1      PORTB.0                         */
/* #define SPEAKER   PORTD.4                         */
/* #define STARTKNAP PIND.0                          */
/*                                                   */
/* Er der ubenyttede portben, slettes disse linjer   */
/*                                                   */
/*****************************************************/

//#define XXXXXXX  PORTA.0    // This port is shown as RESET
//#define XXXXXXX  PORTA.1    // This port is shown as X-tal1
//#define XXXXXXX  PORTA.2    // This port is shown as X-tal2

#define BUTTON1   PINB.0
#define BUTTON2   PINB.1
#define BUTTON3   PINB.2
#define BUTTON4   PINB.3
#define BUTTON5   PINB.4
#define BUTTON6   PINB.5
#define SPEAK     PORTB.6

#define LED6    PORTD.0
#define LED5    PORTD.1
#define LED4    PORTD.2
#define LED3    PORTD.3
#define LED2    PORTD.4
#define LED1    PORTD.5

#include <delay.h>
#include <tiny2313.h>
#include <stdlib.h>

int timetoclick;

void error()
{
   timetoclick = 1000;
   LED1 = 1;
   LED2 = 1;
   LED3 = 1;
   LED4 = 1;
   LED5 = 1;
   LED6 = 1;

   delay_ms(200);

   LED1 = 0;
   LED2 = 0;
   LED3 = 0;
   LED4 = 0;
   LED5 = 0;
   LED6 = 0;
}

void main()
{

    //Make var's
    int randomnumber;
   
    int seed = 0;
    int timer;

    //Define BUTTON's as turned off (enable build in recistor)
    DDRB    =   0B00100000;
    DDRD    =   0B00111111;
    PORTB   =   0B00111111;

    //Make sure BUTTON's turned off?
    PORTB = 0xb00011111;

    LED1 = 1;
    while(BUTTON1 > 0) {
        seed++;
    }
    LED2 = 1;

    //delay_ms(1000);

    timetoclick = 1000;
    srand(seed);

    while(1) {

 
    randomnumber = rand() % 6;  
  

    //If random number is 0 turn on LED1
    if(randomnumber == 0)
    {

        timer = timetoclick;
        LED1 = 1;

        while(timer > 0 && BUTTON1 == 1)
        {
            timer--;
            delay_ms(1);
        }

        if(timer == 0)   // Timeout
          error();
        else  // If button pushed.
        {
            timetoclick = timetoclick - 2;
            LED1 = 0;
        }

    } else {
        LED1 = 0; //Of course, if not that number, keep turned off
    }

    //If random number is 1 turn on led2
    if(randomnumber == 1)
    {

        timer = timetoclick;
        LED2 = 1;

        while(timer > 0 && BUTTON2 == 1)
        {
            timer--;
            delay_ms(1);
        }

        if(timer == 0)   // Timeout
          error();
        else  // Buttton
        {
            timetoclick = timetoclick - 2;
            LED2 = 0;
        }

    } else {
        LED2 = 0;
    }

    if(randomnumber == 2)
    {

        timer = timetoclick;
        LED3 = 1;

        while(timer > 0 && BUTTON3 == 1)
        {
            timer--;
            delay_ms(1);
        }

        if(timer == 0)   // Timeout
        {
           error();
        }
        else  // Trykkede på knappen
        {
            timetoclick = timetoclick - 2;
            LED3 = 0;
        }

    } else {
        LED3 = 0;
    }

    if(randomnumber == 3)
    {

        timer = timetoclick;
        LED4 = 1;

        while(timer > 0 && BUTTON4 == 1)
        {
            timer--;
            delay_ms(1);
        }

        if(timer == 0)   // Timeout
        {
          error();
        }
        else  // Trykkede på knappen
        {
            timetoclick = timetoclick - 2;
            LED4 = 0;
        }

    } else {
        LED4 = 0;
    }

    if(randomnumber == 4)
    {

        timer = timetoclick;
        LED5 = 1;

        while(timer > 0 && BUTTON5 == 1)
        {
            timer--;
            delay_ms(1);
        }

        if(timer == 0)   // Timeout
        {
          error();
        }
        else  // Trykkede på knappen
        {
            timetoclick = timetoclick - 2;
            LED5 = 0;
        }

    } else {
        LED5 = 0;
    }

    if(randomnumber == 5)
    {
        timer = timetoclick;
        LED6 = 1;

        while(timer > 0 && BUTTON6 == 1)
        {
            timer--;
            delay_ms(1);
        }

        if(timer == 0)   // Timeout
        {
           error();
        }
        else  // Trykkede på knappen
        {
            timetoclick = timetoclick - 2;
            LED6 = 0;
        }

        }
    else {
        LED6 = 0;
    }

    }

}




View Postjimblumberg, on 19 November 2012 - 06:11 AM, said:

Quote

And an other note, for some reason my compiler complaings if I try to include time.h (I am using CodeVisionAVR).

That's because your compiler has not implemented the items in that header.

Jim


My teacher told my later that it was because time.h only works on computers, not microprocessors... But thats not importen I found my way around that ;-)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1