Int Sensorpin = A0; Select the Input Pin for the Photosensor

Int Sensorpin = A0; Select the Input Pin for the Photosensor

Program Laser_target2

#include "LowPower.h"

long int high;

long int low;

float period;

float duty;

int average=500;

int sensorpin = A0; // select the input pin for the photosensor

int sensorValue = 0; // variable to store the value coming from the sensor

char i=0;

int j=0;

int p=0;

int k=0;

int m=0;

int n=0;

int Q;

unsigned char puls_cnt=0;

int delayT[]={125,125,125,125,125,125};

int dspnum[]={3,3,3,2,2,2}; // these are numner pins each state

int dspval[]={2,3,4,5,6,7,2,3,4,5,2,3,6,4,7};

//

void setup()

{

for (i=2;i<21;i++)

{

digitalWrite(i,LOW);

}

pinMode(2, OUTPUT); // dsp outputs

pinMode(3, OUTPUT); // display outputs

pinMode(4, OUTPUT);

pinMode(5, OUTPUT);

pinMode(6, OUTPUT);

pinMode(7, OUTPUT);

pinMode(8, OUTPUT);

pinMode(9, OUTPUT); // set to some value

pinMode(10, OUTPUT);

pinMode(11, OUTPUT);

pinMode(12, OUTPUT);

pinMode(13, OUTPUT);

pinMode(15, OUTPUT);

pinMode(16, OUTPUT);

pinMode(17, OUTPUT); // set to some value

pinMode(18, OUTPUT);

pinMode(19, OUTPUT);

pinMode(20, OUTPUT);

delay(1000);

digitalWrite(5,HIGH);

delay(5000);

digitalWrite(5,LOW);

}

void loop()

{

j=0;

i=0;

k=0;

LowPower.powerDown(SLEEP_30MS, ADC_OFF,BOD_OFF);

digitalWrite(8,HIGH); // power to sensor

delayMicroseconds(40);

int Q=analogRead(sensorpin);

if (average>150)

{

j=.15*average;

if (Q>average | (average-Q)<j)

{

average=(Q+4*average)/5;

}

else

{

while(i<1)

{

low=micros();

j= 85; // 1250

k=.85*average;

while (analogRead(sensorpin)<k & j>0)

{

j--;

delayMicroseconds(500); // 42.5ms

}

if (j>0)

{

high=micros();

low=high-low;

j= 50; // 1250

while (analogRead(sensorpin)>=k & j>0) // assume ~8us

{

j--;

delayMicroseconds(500); // 35.5ms

}

if (j>0)

{

digitalWrite(8,LOW); // disable power to sensor

high=micros()-high;

period=high+low;

duty=low/period;

period=period/1000;

// qualify period and duty cycle for 3 consequentuive pulses (no gap)

if (period>35 & period<58) //17 /<28Hz

{

if (duty> .48 & duty < .67) // single pulse sequence

{

puls_cnt++;

if (puls_cnt>0)

{

digitalWrite(8,LOW); // disable power to sensor

setdisplay();

puls_cnt = 0;

i=1;

}

}

else

{

puls_cnt =0;

i=1;

}

}

else

{

puls_cnt =0;

//i=1; reset pulse cnt but loop to qualify a next pulse

}

}

else

{

puls_cnt =0;

i=1;

}

}

else

{

average = .95*average;

puls_cnt =0;

i=1;

}

}

}

}

digitalWrite(8,LOW); // disable power to sensor

}

//

// ************************************************

void setdisplay()

{ // execute display

n=1;

// exec sequence

for (i=0;i<6;i++) // 6 matches the # of display states

{

j=n-1;

m=dspnum[i]; // dspnum is number of items to display this state

for (k=0;k<m;k++)

{

digitalWrite(dspval[n-1],HIGH);

n++;

}

delay(delayT[i]);

for (k=0;k<m;k++)

{

digitalWrite(dspval[j],LOW);

j++;

}

}

}

********MicroChip PIC12F509 code for triggered pulsed sequence gun ******

Laser Drive (momentary, normally off)

;init F509 based

; 508/9 part

; P1,2 are laser high drive

; P0 is discrete switch, using explicate qualification/ !delta

; does not use WDT 3 each 30 on, 20 off, .254S gap

tmrcnt equ 0x10

pcnt equ 0x11

tmp equ 0x12

bits equ 0x13

#define bounce bits,0

#define done bits,1

#define ZERO 3,2

timage equ 0x14

image equ 0x15

;

clrf 6

movlw 0xF9 ; 1111 1001

TRIS 6

movlw 0x80 ; enb pullup(s)

OPTION

movlw 1

movwf image

top1:

clrf bits ; clr bounce and done

; ***************************************

top: ; wakeup on compare trip

movfw 6

movwf timage

xorwf image,w

andlw 1

btfsc ZERO

goto top2

; not same

movfw timage

movwf image

goto top1

;

top2: ; same

btfsc bounce

goto t4

bsf bounce

movlw .27 ; init tmr,set for 27ms (debounce)

movwf tmrcnt

t8:

call msdelay

decfsz tmrcnt ; wait

goto t8

goto top

t4:

btfsc done

goto top1

btfsc image,0

goto top1

bsf done

movlw 3 ; set for 3 pulse per trip

movwf pcnt

t1:

movlw .30 ; init tmr ; set for 30ms on time

movwf tmrcnt

movlw 0xFF ; assert lasr

movwf 6

t2:

call msdelay

decfsz tmrcnt ; wait

goto t2

movlw .20 ; init tmr ; set for 20ms off time

movwf tmrcnt

movlw 0xF9 ; 1111 1001 ; clr lasr

movwf 6 ; disable lasr

t3:

call msdelay

decfsz tmrcnt ; wait

goto t3

decfsz pcnt ; qual numbe of pulses

goto t1

movlw .250; init tmr ; set for 254ms gap

movwf tmrcnt

t5:

call msdelay

decfsz tmrcnt ; wait

goto t5

;

movlw .96; init tmr ; set for total .5s gap

movwf tmrcnt

t6:

call msdelay

decfsz tmrcnt ; wait

goto t6

goto top1

; **************************************

msdelay: ; subservice

movlw .249

movwf tmp

d1:

nop

decfsz tmp

goto d1

retlw 0

end