I had an Idea to write a second game for the device, but in doing so I had to modify how the interpreter process button presses.
Up until this point, when the keyword to wait for a button press was called "BG", the interpreter would wait indefinitely until a button was pressed. But in this new game, if there is no user input, I want the device to go into deep sleep.
Also, because the buttons are on a resistor ladder, only the RUN button will wake the device up from deep sleep. I had to address the following issues and fix them:
- If no button is pressed when BG is called, after 5 seconds or set timeout, interpreter will continue. This allows the user to let the device go to sleep if there is no user input.
- When RUN button is pressed to wake interpreter from deep sleep (D0-D8), if it was not released fast enough, it would also kill the interpreter. As the RUN button is also the button used to exit the interpreter in normal operation. The fix was to set the "Button Pressed" flag to true immediately after waking, so the interpreter would not read another button press until the RUN button has been released and a new press is done.
- There was an issue with device not waking from button interrupt (PCINT3) or from WDT after one or two sleep cycles, it took me quite a while to fix the issue, and to this point I am not entirely sure what was causing the issues. It is confusing enough for me dealing with one interrupt source, but trying to deal with two simultaneously was conflicting. I think the issue was not disabling interrupts while clearing the PCINT3 flag. I will have to experiment with different types of interrupts more to understand them better. But now it is fixed. If we call (D8) in the interpreter, the device will go into low power mode for 8 minutes, or until the RUN button is pressed.
The second game I have written is a "Virtual Pet" game. The concept is fairly simple.
- When running program, if health is greater than 31 (25%), LED 1 will be lit, otherwise LED 0 will be lit. This allows the player to get a quick check on if there is immediate attention needed to the Pet.
- "Feeding" Pet is done by placing device in sunlight. We can think of the animals hunger directly proportional to the voltage level of the super capacitor. If that drops below 2 volts and device goes into low power mode, we sure as hell better hope some of it's other stats are high enough or when we recharge the device it will wake up dead!
- We can give the pet "water" by pressing the "<" button on device. Each press will add 10 points to it's water stat.
- We can "Pet" the creature by pressing the ">" button, which adds 10 to it's "attention" stat. Watering the pet also adds 1 to this stat.
- The pet's overall health and happiness can be viewed by pressing the "T" button, which will blink out a number between 1 and 127. This number is decided by the following formulas. Due to the order of operations TGRK calculates math, I had to write it in this format.
* R2 is pets "hunger", determined by (VCC - 27) x 5* * 0 = starving [(27 - 27) x 5 = 0] * * 127 = full [(53 - 27) x 5 = 130 (rounds to 127) * R2 PAXX SUB 27 MUL 5 * R7 and R8 Are temporary variables to store "Thirst" and "Attention" * * We divide all by 3, then add together to get a number between 0-127 * R7 R3 DIV 3 R8 R4 DIV 3 * R1 is pets overall health and happiness * * 0 = dead, 127 = Fat and Happy * R1 R2 DIV 3 ADD R7 ADD R8
I haven't been able to test the game thoroughly due to the button processing and waking from sleep issues I have since fixed. I will load it up on device today and see how long I can keep this thing alive.
With no input from user (Petting or Watering), every 40 or so minutes 8 thirst and 4 attention is subtracted from stats, every 8 minutes the device wakes up momentarily and increases a counter if no user attention is given. If the device is not in sunlight during this time, the hunger will increase as well.
I need to modify game code so pet can also die if it's not hungry, but hasn't had water for x amount of time.
It would also be cool to keep track of how many days the pet has been alive (roughly as far as the accuracy of attiny85's WDT), which would be easy to do. Every 15 sleep cycles would equal roughly 2 hours. Increment a counter when that happens, then increment another counter when that one gets to 12 (for ~24 hrs).
Full game code:
* A simple "Virtual Pet" game * * Charging via solar panel "feeds" pet, Pressing < button gives pet water, Pressing > button "pets" the pet. Overall pet health viewed by pressing T button. S button to exit game while pet is awake. R button to wake pet up from sleep. Pet will "Sleep" for 8 minutes at a time, each time a pet wakes up, it's thirst and "attention" stats decrease. These can be increased by pressing > and < buttons. Pet's "hunger" increases as super capacitor voltage decreases, the only way to "feed" the pet is by placing pet in sunlight or directly under a very bright lamp. * F1 FUNC BG CE BV 0 R6 ADD 1 R5 ADD 1 CGE R5 7 R3 SUB 8 R4 SUB 4 R5 0 SEP CE BV 1 R6 0 R3 ADD 10 R4 ADD 1 SEP CE BV 2 R6 0 R4 ADD 10 SEP R2 PAXX SUB 27 MUL 5 R7 R3 DIV 3 R8 R4 DIV 3 R1 R2 DIV 3 ADD R7 ADD R8 CE BV 3 R6 0 BB R1 FUNC F2 FUNC CGE R1 31 L1H L0L SEP CL R1 31 L1L L0H FUNC F3 FUNC F2 F1 CGE R6 5 D8 R6 0 R5 ADD 1 S1 SEP CG R1 0 AND CNE BV 4 LPF FUNC F3 EOP
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.