Close

Multiple Interrupt Sources and Getting Button Input

A project log for Tiny Bit Machine

An 8-bit solar powered gadget.

g0730ng0730n 09/17/2024 at 18:490 Comments

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:

  1. 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.
  2. 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.
  3. 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.

 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