Lightning Trigger

From Pinguino
Jump to: navigation, search


TODO :

write some background info.  For now it's just the listing.
add circuit diagram
add some pictures and screenshots (Output of processing sketch)  

This is the part that goes into the Pinguino (18F2550 base)

// Lightning Trigger for Pinguino (18F2550 base)
// By Kurt Schatteman - 2010
// All comments are in English.  This way the growing Pinguino community can benifit.
// Feel free to experiment.

// TODO 
//		Support for LCD to show levels. (4bit mode)


#define AMBLIGHT 13		// Pin to which Phototransistor/resistor network is connected
#define TRIGGER 0		// Outputpin to connect to the Camera
#define BUT_SELUD 1		// button to select up or down
#define BUT_THRES 2		// Button to inc/dec Threshold (uses SELUD)
#define BUT_TLEN 3		// Button to inc/dec Triggerpuls Length (uses SELUD)
#define SW_FASTSLOW 5		// Switch to select Fast or Slow Mode (Fast mode means, no buttoninteraction)
#define BUT_DELAY 4		// Button to inc/dec Delay (uses SELUD)

unsigned Current,Prev,TriggerLen,TriggerDelay;
int THRESHOLD;

uchar USBtodo,USBcodif,USBbuffer[6];
int USBLen;
int USBendstring;
//unsigned char 


void setup()
{
  pinMode(TRIGGER, OUTPUT);
  pinMode(BUT_SELUD, INPUT);
  pinMode(BUT_THRES, INPUT);
  pinMode(BUT_TLEN, INPUT);
  pinMode(BUT_DELAY, INPUT);
  pinMode(SW_FASTSLOW, INPUT);
  digitalWrite(TRIGGER, LOW);
  THRESHOLD = 15;
  TriggerLen = 100;
  TriggerDelay = 0;
  
  Current = analogRead(AMBLIGHT);		// Pin13 is hereby defined as Analog.  As a consequence Pin13 till Pin17 are now analog.
}

void usbwrite()					// for debug purposes ONLY (slows down operation)
{
  if (USB.available())		
  {
    USBcodif=USB.read();
    if(USBcodif=='M')			
    {
      USBtodo=USB.read();
      if(USBtodo=='T')		
      {
	USBLen=USB.read(); 			//length of text
	USBbuffer[0]=Current;
	USBbuffer[1]=Current/256;
	USBbuffer[2]=Prev;
	USBbuffer[3]=Prev/256;
	USBbuffer[4]=THRESHOLD;
	USBbuffer[5]=THRESHOLD/256;
	USB.send(  USBbuffer, 6 );
	USBLen=USB.read(); 			//length of text
      }
    }
    USBendstring=USB.read(); 			// read end string byte ( 0 ) =stop byte
  }
}


void loop()
{
  Current = analogRead(AMBLIGHT);
  usbwrite();
  if (Current >= Prev + THRESHOLD)
  {
   if (TriggerDelay>0) delay(TriggerDelay);
   digitalWrite(TRIGGER, HIGH);
   delay(TriggerLen); 
   digitalWrite(TRIGGER, LOW);
  }
  Prev = Current;

  if (digitalread(SW_FASTSLOW)==HIGH)			// Skip Buttonreading when in FAST mode 
  {
    if (digitalread(BUT_THRES)==LOW) 
    {
      (digitalread(BUT_SELUD) == HIGH)?THRESHOLD++:THRESHOLD--;
      delay(100);
    }
    if (THRESHOLD < 5) THRESHOLD=5;
		
    if (digitalread(BUT_TLEN)==LOW) 
    {
      (digitalread(BUT_SELUD) == HIGH)?TriggerLen++:TriggerLen--;
      delay(100);
    }
    if (TriggerLen < 5) TriggerLen=5;
		
    if (digitalread(BUT_DELAY)==LOW) 
    {
      (digitalread(BUT_SELUD) == HIGH)?TriggerDelay++:TriggerDelay--;
      delay(100);
    }		
  }
}

And here is the sketch for Processing. (Only meant for debug)

// Debug for LightningTrigger 
// Kurt Schatteman - 2010
// This is my first sketch in Processing so probably far from efficient.



import hypermedia.pinguino.*;       // import library

Pinguino board;                     // pinguino object

void setup() 
{
  size( 800, 180 );                 // size the window application
  board = new Pinguino( this );     // connect to Pinguino board
 
}

void draw() 
{
  background(0);
  int timeout = 1000;
  int C,P,T,DIFF;
 
  byte[] request = new byte[] {'M','T',6,0};
  byte[] response = new byte[6];
  
  board.write(request);
  response=board.read(response,timeout);
  if ( response!=null ) 
  {
    C = response[0];
    if ( C < 0 ) C=256+C;
    C+=256*response[1];
      
    P = response[2];
    if ( P < 0 ) P=256+P;
    P+=256*response[3];
      
    T = response[4];
    if ( T < 0 ) T=256+T;
    T+=256*response[5];
      
    DIFF = C-P;
      
    rect(100,30,C,10);
    rect(100,50,P,10);
    rect(100,70,DIFF,10);
    rect(100,90,T,10);
    text( "Curr "+C,40,40 );
    text( "Prev "+P,40,60 );
    text( "Diff "+DIFF,40,80);
    text( "Thres"+T,40,100);
  }
}