Thread: Reaction Game: How to create randomize algorithm which gets exponentially faster

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    4

    Question Reaction Game: How to create randomize algorithm which gets exponentially faster

    I am creating a reaction game using a series of lasers and LRDs and need to make a function which waits for a specific pin in portb to be triggered high before it can move onto waiting for the next pin to be triggered. The time lasped before the "game loses" needs to get exponentially faster as the game goes on.


    I am using statements such as if(input(PIN_B0)==1 generate_tone(G_note[2], 1000); but unsure how to write in c "wait x amount of time, if trigger move onto next random pin or no trigger go to lose function.


    Any help would be greatly appreciated
    Angela

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Functions to suspend program execution (i.e. to pause) are not part of the C standard. You need to look up a relevant function for your system. Under most unix, there are functions like sleep() and usleep(), which sleep for specified intervals (in seconds and microseconds) respectively. They are often declared in a header like <unistd.h>. Under windows, there is a Sleep() function (note uppercase first letter) in the win32 API.

    As to " if trigger move onto next random pin or no trigger go to lose function" - that is excruciatingly unclear. I assume you mean wait for a specified period and, if pin hasn't gone high, lose.

    Assuming the pin stays high until you reset it low, that might be done with some logic like this (note: not real code)
    Code:
    sleep(allowed_trigger_time);
    if (input(PIN_B0) == 1)    /*   I assume this is your test for triggered high */
    {
          /*  check next pin */
    }
    else
    {
          /* lose */
    }
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    [crosspost]
    c - Reaction game, unaware of how to write function - Stack Overflow

    "PIN_B0" -> Would you be using the CCS compiler, by chance?

    If so, have a look at the generate tone here CCS :: View topic - 4" 7-segment LED display in 3-digit with 2x7W amplifier

    Also, the CCS compiler has a delay function CCS :: View topic - How to generate delay in ms
    Fact - Beethoven wrote his first symphony in C

  4. #4
    Registered User
    Join Date
    Mar 2014
    Posts
    4
    Thank you but my question was referring more particularly to creating the randomize function and the exponential time before going to the lose funtion. its pic16f819 and using css. I'm using a series of lasers and strips of leds. i want the program to 1)leds change from red to green 2)program to wait a certain amount of time for the related pin to be triggered high (ie, LEDstrip1 is related to pin_B0) 3)if not triggered during that amount of time go to the lose function or 4)if it is triggered within time then light up different LEDstrip and wait for its corresponding pin_b to be triggered.


  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Okay so...

    Lasers and LED strips - What are you using to drive that sort of current?

    In microcontroller world, getting random numbers is hard. However, because you have a system running and waiting for input at any time, you can use that to create a random number.

    You can have an integer being incremented until input is found. Having random input from a user is gold when it comes to creating a random number. However, if you are finding that the numbers generated this way are not random enough, you can use something like an Xorshift - Xorshift - Wikipedia, the free encyclopedia - I'd probably start with the Xorshift function, because you may find that the reaction time could be close to constant, and this might be a visible pattern that the users could get used to. (And having the subroutine written and tested for other projects is also good to have).

    The easiest way to reduce the time exponentially is to continuously reduce the time by a percentage (i.e. loopTime -= (loopTime*0.05)). The 0.05 corresponds to 5%, but I'm sure that you can find a nice percentage experimentally.

    Keep us updated on how you go
    Last edited by Click_here; 03-30-2014 at 05:07 AM.
    Fact - Beethoven wrote his first symphony in C

  6. #6
    Registered User
    Join Date
    Mar 2014
    Posts
    4
    hi i did look up the Xorshift but someone recommended using a rand(); but unclear on how to use it in my case from the tutorials ive read online. This is what ive managed so far:

    Code:
    unsigned char y;
    int main(){
    int y, n;
    for (y=0; y<7; y++);
    rand[%7+1]=%u;
    if y=1{
    if(input(PIN_B0==1)fputc(BS1Grn, Slave1);
    else lose();
    }
    if y=2{
    if input(PIN_B1==1)fputc(BS2Grn, Slave1);
    else lose();
    }


    but still not sure how to incorporate exponentially faster wait time

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It is not possible to speed up instructions. It is only possible to add delays between operations of interest.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Registered User
    Join Date
    Mar 2014
    Posts
    4
    Code:
    unsigned char y;
    Code:
    int main(){
    int y, n;
    for (y=0; y<7; y++);
    rand[%7+1]=%u;
    if y=1{
    if(input(PIN_B0==1)fputc(BS1Grn, Slave1);
    delay_ms(x amount of time);
    else lose();
    }
    if y=2{
    if input(PIN_B1==1)fputc(BS2Grn, Slave1);
    delay_ms(x amount of time less then previous);
    else lose();
    }
    

    ok so how do you make the delay time exponentially less then the previous?

  9. #9
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    "rand()" might work if you make sure you can vary the seed for the start of each turn

    i.e.
    Code:
    #include ...
    #include <stdlib.h>
    
    #use blah... blah...
    
    
    void playGame(void)
    {
        int randomNumber;
    
        // You must have a starting point for your
        // Pseudo random number generator
        srand(get_rtcc()); 
        
        randomNumber = rand();
        /* Do something */
    
        randomNumber = rand();
        /* Do something */
    
        randomNumber = rand();
        /* Do something */
    
        ...
    }
    
    /* It's "void main()" or just "main()" for CCS */
    void main()
    {
        setup();
    
        for(;;)
        {
            if( startButton == PRESSED)
            {
                playGame();
            }
        }
    }
    ok so how do you make the delay time exponentially less then the previous?
    As I said earlier, it goes down at a constant percentage

    i.e.
    Code:
    unsigned int16 delayTime;
    
    for(delayTime = 60000; ; delayTime -= (delayTime * 0.05))
    {
        output_toggle(LED);
        delay_ms(delayTime);
    }
    Last edited by Click_here; 03-30-2014 at 04:29 PM.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A faster algorithm
    By arka.sharma in forum C Programming
    Replies: 17
    Last Post: 04-28-2012, 03:28 AM
  2. "meeting point" - help with faster algorithm
    By cuber in forum Tech Board
    Replies: 1
    Last Post: 11-01-2011, 08:24 PM
  3. time reaction game
    By fatcecil in forum C++ Programming
    Replies: 3
    Last Post: 05-12-2003, 06:25 PM
  4. Over-reaction
    By RobS in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-15-2002, 02:35 AM
  5. Stupid over reaction
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 09-30-2001, 08:03 PM

Tags for this Thread