I was coding a MineSweeper game (In C), and I wanted to add a fade effect in the background by adjusting the pallet colors, which I ran using the code below. It worked fine while plugged into my computer and the fade effect went at a slower speed as I intended. Once I unplugged my calculator, the fade effect went crazy and started flashing through all of them in half a second. Plugging in the calculator again showed the slower fade, but at varying speeds each time, going faster or slower than what I intended for the fade to take.

Why does this happen? and is there a way to get timers or something similar to work to display the "Fading" effect consistently. I don't check the battery in the code, and this still happens when I plug the calculator into a wall outlet instead of a computer.


Code:

#include <math.h>
#include <ti/getcsc.h>
#include <keypadc.h>
#include <stdio.h>
#include <stdlib.h>
...
unsigned char cycle = 0;
unsigned char delay = 0;
char pause = 0;
while (key != 49) { //Key 49 or F5 quits the game
        do {
            key = os_GetCSC();
            delay++;
            if (delay > 24) { //Waits for 24 loops to pass
                if (pause < 0) {
                    unsigned short *fP = (int*)0xE30200; //Memory address of pallet
                    unsigned short bright[7] = {528,495,462,429,396,363,330};
                    fP = fP + (cycle / 13) + 18; //Sets color pallet 18,19,20, or 21
                    if (cycle % 13 < 7) {
                        *fP = bright[cycle % 13]; //Goes from 330 to 528
                    } else {
                        *fP = bright[12 - (cycle % 13)]; //Goes from 528 to 330
                    }
                    cycle = (cycle + 1) % 56; //Repeats after running 56 times
                    if (cycle % 13 == 7) {
                        pause = 20; //Pauses for 20 "Loops" (Showing a symbol)
                    } else if (cycle % 13 == 0) {
                        pause = 8; //Pauses for 8 "Loops" (Showing nothing)
                    }
                }
                pause--;
                delay = 0; //Resets the delay thing
            }
        } while(!key);
//Code related to key presses and game logic
}

Video showing the Fading effect being affected by plugging or unplugging the Calculator (Using version 5.4):
Who cares, you are using magic numbers rather that the toolchain defines.
The OS does some work in the background when USB is connected using interrupts.

You should use the clock() function instead of manually counting cycles, that ticks at the same speed regardless of what interrupts are doing.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement