[TriEmbed] IR sensor vs trigger

Ken carver_dude at hotmail.com
Sat Aug 2 08:11:38 CDT 2014


For the trigger pull you can easily check and debounce in software. 
I had wrote the following for a toy gun I retro fitted to make noise and blink, which gives good feedback for trigger pulls, but could easily be changed to also give feedback for hits also. It is written to start and play through a tigger pull sequence when the trigger is pulled before the next detect of the trigger, but could be changed to enable the trigger detection quicker. It was written to allow multiple sequences of pin changes to run based on events. I used a tiny84 to get more pins. The speaker I used was an old one from an over the ear type headphones, just connected to a pin and ground. The tiny did not have a sound library when written, maybe not still, so I just bit banged the pin, and came up with a spacey sound so it worked, just not quite as loud as the Aurdino with the sound library. Driving all the leds and the speaker did require a cap on the power supply pin. Might give you some ideas. 

#define tonePin 8
#define pushPin 10
#define num_lights 9
#define num_notes 12

unsigned long last = 0;

// tone stuff
typedef struct tones
{
    int freq;
    int dur; 
};
int   note_count = 0;
int   note_index = 0;
tones notes[num_notes];

// led stuff
typedef struct leds
{
    unsigned long pattern;
    unsigned long trig_pattern;
    int           pin;
};

unsigned int count = 0;
unsigned int pos = 0;
leds lights[num_lights];

// button stuff
boolean triggered = false;

void check_button()
{
    // triggered is cleared when the tone stops a cycle
    if ( ! triggered)
    {
        triggered = digitalRead(pushPin);
        if (triggered) pos = 0;
    }
}

void process_lights()
{
    count++;
    if (count < 10) return;
      
    count = 0;
    pos++;
    if (pos >= 32) pos = 0;
    unsigned long tmp = 1; 
    tmp = tmp << pos;
    
    for(int i = 0; i < num_lights; i++)
    {      
        unsigned long state = lights[i].pattern & tmp;
        if (triggered)
        {
            state = lights[i].trig_pattern & tmp;
        }
        
        if (state != 0)
        {
            digitalWrite(lights[i].pin, HIGH);
        }
        else
        {
            digitalWrite(lights[i].pin, LOW);
        }
    }    
}

void process_tone()
{
    if ( ! triggered)
    {
        note_count = 0;
        note_index = 0;
    }
    else
    {
        if (note_count%(2*notes[note_index].freq) < notes[note_index].freq)
        {
            digitalWrite(tonePin, HIGH);
        }
        else
        {
            digitalWrite(tonePin, LOW); 
        }
        note_count++;
        if (note_count >= notes[note_index].dur)
        {
            note_index++;
            note_count = 0;
            if (note_index >= num_notes)
            {
                triggered = false;
                digitalWrite(tonePin, LOW);
                note_count = 0;
                note_index = 0;
            }
        }
    }
}

void setup()
{    
    unsigned long one = 1;
    lights[0].pattern = one;
    lights[0].trig_pattern = 0xfc000000;
    lights[0].pin = 0;
    
    lights[1].pattern = one << 5;
    lights[1].trig_pattern = 0x03f00000;
    lights[1].pin = 1;
 
    lights[2].pattern = one << 10;
    lights[2].trig_pattern = 0x000fc000;
    lights[2].pin = 2;
    
    lights[3].pattern = one << 16;
    lights[3].trig_pattern = 0x000fc000;
    lights[3].pin = 3;
    
    lights[4].pattern = one << 21;
    lights[4].trig_pattern = 0x000003f00;
    lights[4].pin = 4;
    
    lights[5].pattern = one << 27;
    lights[5].trig_pattern = 0x000000ff;
    lights[5].pin = 5;
    
    lights[6].pattern = 0xf0f0f0f0;
    lights[6].trig_pattern = 0x00ffff00;
    lights[6].pin = 6;
    
    lights[7].pattern = 0x0f0f0f0f;
    lights[7].trig_pattern = 0xffff0000;
    lights[7].pin = 7;
    
    lights[8].pattern = 0xffffffff;
    lights[8].trig_pattern = 0x0000ffff;
    lights[8].pin = 9;
    
    for(int i = 0; i < num_lights; i++)
    {
        pinMode(lights[i].pin, OUTPUT);
        digitalWrite(lights[i].pin, HIGH);
    }
    delay(500);
    for(int i = 0; i < num_lights; i++)
    {
        digitalWrite(lights[i].pin, LOW);
    }
 
    notes[0].freq = 9;
    notes[0].dur  = 50;
    notes[1].freq = 3;
    notes[1].dur  = 60;
    notes[2].freq = 5;
    notes[2].dur  = 100;
    notes[3].freq = 7;
    notes[3].dur  = 80;    
 
    notes[4].freq = 9;
    notes[4].dur  = 70;
    notes[5].freq = 10;
    notes[5].dur  = 60;
    notes[6].freq = 1;
    notes[6].dur  = 50;
    notes[7].freq = 3;
    notes[7].dur  = 50;    
 
    notes[8].freq = 5;
    notes[8].dur  = 50;
    notes[9].freq = 7;
    notes[9].dur  = 50;
    notes[10].freq = 9;
    notes[10].dur  = 50;
    notes[11].freq = 1;
    notes[11].dur  = 60;  
    
    pinMode(tonePin,OUTPUT);
    
    pinMode(pushPin,INPUT);
}

void loop()
{
    unsigned long now = millis();

    if (now >= last + 1)
    {
        last = now;
        check_button();
        process_lights();
        process_tone();
    } 
}


From: Joe Fair 
Sent: Thursday, July 31, 2014 7:14 PM
To: Jon Wolfe 
Cc: triembed at triembed.org 
Subject: Re: [TriEmbed] IR sensor vs trigger

I think you are right, they kind of each need a chip.  I was hoping to be smart enough to not to need more hardware. 
Does the tiny sport two interrupts?  Or maybe i can tie them together?

I guess i need to learn more about days sheets, too.
Joe

On Jul 30, 2014 10:09 PM, "Jon Wolfe" <jonjwolfe at yahoo.com> wrote:

  I agree with the others, it sounds like the trigger should get the interrupt.


  Like Jeff said, you want your interrupt to execute in as short of a time as possible. One approach might be to just set a flag on trigger presses then process the action in the main loop. That could still leave you with 1 sec latency if you are detecting IR hits, which would probably be pretty noticeable.


  I have never tried this on a tiny, but I'm making it a personal mission to find a use case for it, but you could use an RTOS, like http://www.femtoos.org/  Looks promising. I've used ChibiOS on Cortex M3/4's and found it pretty nice to work with, YMMV. 



  The other alternative approaches might be to try to reduce the 1 second detection loop. Or, to try to architect your own "cooperative multitasking" or coroutines. In short, write your detect loop so that instead of looping for 1 second, it just does a little bit of work and returns to the main loop, remembering where is was in the loop between each call. I use this approach a lot.


  I will say that you should try to handle debouncing the trigger in hardware as much as you can. I've tried software debouncing mechanical encoders in interrupt driven routines, and I can attest that it is maddening.


  Lastly, you *could* always just buy a separate tiny for the detection, but I have no ulterior motive or bias there ;)








------------------------------------------------------------------------------
  From: Joe Fair <joe at fairanswers.com>
  To: triembed at triembed.org 
  Sent: Saturday, July 26, 2014 4:09 PM
  Subject: [TriEmbed] IR sensor vs trigger


  Ok.  I'm pretty close on getting my home made laser tag with at tiny 85s going, but now i have a question about design.
  The loop I'm using detects a trigger pull, then a hit on the sensors.  however, The pulse in function I'm using to sense IR ha a timeout of 1 second.  If there is no inbound signal it is only checking got a trigger pull once a second.
  So i need to use an interrupt, but does it matter if i use one on the trigger pull or the ir sensor? 
  Joe


  _______________________________________________
  Triangle, NC Embedded Computing mailing list
  TriEmbed at triembed.org
  http://mail.triembed.org/mailman/listinfo/triembed_triembed.org
  TriEmbed web site: http://TriEmbed.org





--------------------------------------------------------------------------------
_______________________________________________
Triangle, NC Embedded Computing mailing list
TriEmbed at triembed.org
http://mail.triembed.org/mailman/listinfo/triembed_triembed.org
TriEmbed web site: http://TriEmbed.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.triembed.org/pipermail/triembed_triembed.org/attachments/20140802/0038f0dc/attachment.htm>


More information about the TriEmbed mailing list