<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://cni.su.domains/wiki/index.php?action=history&amp;feed=atom&amp;title=CNI_trigger_code_for_serial_port</id>
	<title>CNI trigger code for serial port - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://cni.su.domains/wiki/index.php?action=history&amp;feed=atom&amp;title=CNI_trigger_code_for_serial_port"/>
	<link rel="alternate" type="text/html" href="http://cni.su.domains/wiki/index.php?title=CNI_trigger_code_for_serial_port&amp;action=history"/>
	<updated>2026-10-30T12:32:04Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.3</generator>
	<entry>
		<id>http://cni.su.domains/wiki/index.php?title=CNI_trigger_code_for_serial_port&amp;diff=1658&amp;oldid=prev</id>
		<title>imported&gt;Wandell at 04:57, 7 June 2011</title>
		<link rel="alternate" type="text/html" href="http://cni.su.domains/wiki/index.php?title=CNI_trigger_code_for_serial_port&amp;diff=1658&amp;oldid=prev"/>
		<updated>2011-06-07T04:57:08Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 04:57, 7 June 2011&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l1&quot;&gt;Line 1:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 1:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;/*&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt; &lt;/ins&gt;/*&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;  * CNI_trigger sketch for Arduino.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;  * CNI_trigger sketch for Arduino.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;  *  &lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;  *  &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>imported&gt;Wandell</name></author>
	</entry>
	<entry>
		<id>http://cni.su.domains/wiki/index.php?title=CNI_trigger_code_for_serial_port&amp;diff=1657&amp;oldid=prev</id>
		<title>imported&gt;Wandell: Created page with &quot;/*  * CNI_trigger sketch for Arduino.  *   *   * A simple sketch to generate TTl pulses when told to do so through a serial  * port command.    *  * When running this sketch on a...&quot;</title>
		<link rel="alternate" type="text/html" href="http://cni.su.domains/wiki/index.php?title=CNI_trigger_code_for_serial_port&amp;diff=1657&amp;oldid=prev"/>
		<updated>2011-06-07T04:56:42Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;/*  * CNI_trigger sketch for Arduino.  *   *   * A simple sketch to generate TTl pulses when told to do so through a serial  * port command.    *  * When running this sketch on a...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;/*&lt;br /&gt;
 * CNI_trigger sketch for Arduino.&lt;br /&gt;
 * &lt;br /&gt;
 * &lt;br /&gt;
 * A simple sketch to generate TTl pulses when told to do so through a serial&lt;br /&gt;
 * port command.  &lt;br /&gt;
 *&lt;br /&gt;
 * When running this sketch on a Teensy, be sure to set the &amp;quot;USB Type&amp;quot; to&lt;br /&gt;
 * &amp;quot;Serial&amp;quot; (under the Arduino Tools menu). &lt;br /&gt;
 * &lt;br /&gt;
 * This sketch will also listen for input pulses and send out a serial port &lt;br /&gt;
 * character whenever one is detected. It uses an external interrupt, so it&lt;br /&gt;
 * can reliably detect very brief pulses. We use this to detect the GE 3.3v &lt;br /&gt;
 * scope trigger pulses (~4 usec pulse duration). &lt;br /&gt;
 * &lt;br /&gt;
 */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;Flash.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;Messenger.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 #define BAUD 57600&lt;br /&gt;
 &lt;br /&gt;
 #define DEFAULT_OUT_PULSE_MSEC 5&lt;br /&gt;
 #define DEFAULT_IN_PULSE_STATE 0&lt;br /&gt;
 &lt;br /&gt;
 // The approximate duration of the output pulse, in milliseconds.&lt;br /&gt;
 // Note that we don&amp;#039;t account for delays in the code, so it will &lt;br /&gt;
 // always be a bit longer than this.&lt;br /&gt;
 unsigned int g_outPulseDuration = DEFAULT_OUT_PULSE_MSEC;&lt;br /&gt;
 &lt;br /&gt;
 // The input pin should be the pin mapped to INT0, unless you change&lt;br /&gt;
 // the interrupt number below. The output pin can be any digital pin.&lt;br /&gt;
 // Teensy2.0 has LED on pin 11 and INT0 on pin 5 (D0)&lt;br /&gt;
 #define INTERRUPT 0&lt;br /&gt;
 const byte g_outPin = 11;&lt;br /&gt;
 const byte g_inPin = 5;&lt;br /&gt;
 &lt;br /&gt;
 byte g_inPulseEdge = FALLING;&lt;br /&gt;
 &lt;br /&gt;
 volatile byte g_outPinOn = false;&lt;br /&gt;
 volatile unsigned long g_outPulseStart;&lt;br /&gt;
 &lt;br /&gt;
 // Instantiate Messenger object used for serial port communication.&lt;br /&gt;
 Messenger g_message = Messenger(&amp;#039;,&amp;#039;,&amp;#039;[&amp;#039;,&amp;#039;]&amp;#039;);&lt;br /&gt;
 &lt;br /&gt;
 // Create the Message callback function. This function is called whenever a complete &lt;br /&gt;
 // message is received on the serial port.&lt;br /&gt;
 void messageReady() {&lt;br /&gt;
  int val[10];&lt;br /&gt;
  byte i = 0;&lt;br /&gt;
  if(g_message.available()) {&lt;br /&gt;
    // get the command byte&lt;br /&gt;
    char command = g_message.readChar();&lt;br /&gt;
    switch(command) {&lt;br /&gt;
    &lt;br /&gt;
    case &amp;#039;?&amp;#039;: // display help text&lt;br /&gt;
      Serial &amp;lt;&amp;lt; F(&amp;quot;CNI Trigger Device\n&amp;quot;);&lt;br /&gt;
      Serial &amp;lt;&amp;lt; F(&amp;quot;Sends TTL pulses out on pin &amp;quot;) &amp;lt;&amp;lt; (int)g_outPin &amp;lt;&amp;lt; F(&amp;quot;.\n&amp;quot;);&lt;br /&gt;
      Serial &amp;lt;&amp;lt; F(&amp;quot;Listens for pulses in on pin &amp;quot;) &amp;lt;&amp;lt; (int)g_inPin &amp;lt;&amp;lt; F(&amp;quot;.\n&amp;quot;);&lt;br /&gt;
      Serial &amp;lt;&amp;lt; F(&amp;quot;\nCommands:\n&amp;quot;);&lt;br /&gt;
      Serial &amp;lt;&amp;lt; F(&amp;quot;[t]   will send a trigger pulse. This also disables the input pulse\n&amp;quot;);&lt;br /&gt;
      Serial &amp;lt;&amp;lt; F(&amp;quot;      detection. Send a [p] command to re-enable it.\n\n&amp;quot;);&lt;br /&gt;
      Serial &amp;lt;&amp;lt; F(&amp;quot;[o,N] set the output pulse duration to N milliseconds. Send with\n&amp;quot;);&lt;br /&gt;
      Serial &amp;lt;&amp;lt; F(&amp;quot;      no argument to show the current pulse duration.\n\n&amp;quot;);&lt;br /&gt;
      Serial &amp;lt;&amp;lt; F(&amp;quot;[p]   enable input pulse detection. Send a [t] to disable.\n\n&amp;quot;);&lt;br /&gt;
      Serial &amp;lt;&amp;lt; F(&amp;quot;[r]   reset default state.\n\n&amp;quot;);&lt;br /&gt;
      break;&lt;br /&gt;
      &lt;br /&gt;
    case &amp;#039;o&amp;#039;: // Set out-pulse duration (msec)&lt;br /&gt;
      while(g_message.available()) val[i++] = g_message.readInt();&lt;br /&gt;
      if(i&amp;gt;1){&lt;br /&gt;
        Serial &amp;lt;&amp;lt; F(&amp;quot;ERROR: Set output pulse duration requires no more than one param.\n&amp;quot;);&lt;br /&gt;
      }else if(i==1){&lt;br /&gt;
          g_outPulseDuration = val[0];&lt;br /&gt;
      }&lt;br /&gt;
      Serial &amp;lt;&amp;lt; F(&amp;quot;Output pulse duration is set to &amp;quot;) &amp;lt;&amp;lt; g_outPulseDuration &amp;lt;&amp;lt; F(&amp;quot; msec.\n&amp;quot;);&lt;br /&gt;
      break;&lt;br /&gt;
  &lt;br /&gt;
    case &amp;#039;t&amp;#039;: // force output trigger&lt;br /&gt;
      // First force the pin low, in case it was already on. This will ensure that&lt;br /&gt;
      // we get a change on the pin no matter what state we were in.&lt;br /&gt;
      if(g_outPinOn) digitalWrite(g_outPin, LOW);&lt;br /&gt;
      triggerOut();&lt;br /&gt;
      // Automatically disable input pulse detection&lt;br /&gt;
      setInPulseState(0);&lt;br /&gt;
      break;&lt;br /&gt;
 &lt;br /&gt;
    case &amp;#039;p&amp;#039;: // turn on input pulse detection&lt;br /&gt;
      //Serial &amp;lt;&amp;lt; F(&amp;quot;Enabling input pulses\n&amp;quot;);&lt;br /&gt;
      setInPulseState(1);&lt;br /&gt;
      break;&lt;br /&gt;
 &lt;br /&gt;
    case &amp;#039;r&amp;#039;: // reset&lt;br /&gt;
      setInPulseState(DEFAULT_IN_PULSE_STATE);&lt;br /&gt;
      g_outPulseDuration = DEFAULT_OUT_PULSE_MSEC;&lt;br /&gt;
      digitalWrite(g_outPin, LOW);&lt;br /&gt;
      break;&lt;br /&gt;
 &lt;br /&gt;
    default:&lt;br /&gt;
      Serial &amp;lt;&amp;lt; F(&amp;quot;[&amp;quot;) &amp;lt;&amp;lt; command &amp;lt;&amp;lt; F(&amp;quot;]\n&amp;quot;);&lt;br /&gt;
      Serial &amp;lt;&amp;lt; F(&amp;quot;ERROR: Unknown command.\n\n&amp;quot;);&lt;br /&gt;
    } // end switch&lt;br /&gt;
  } // end while&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void setup(){&lt;br /&gt;
  Serial.begin(BAUD);&lt;br /&gt;
  Serial &amp;lt;&amp;lt; F(&amp;quot;*********************************************************\n&amp;quot;);&lt;br /&gt;
  Serial &amp;lt;&amp;lt; F(&amp;quot;* CNI Trigger firmware version &amp;quot;) &amp;lt;&amp;lt; VERSION &amp;lt;&amp;lt; F(&amp;quot;\n&amp;quot;);&lt;br /&gt;
  Serial &amp;lt;&amp;lt; F(&amp;quot;* Copyright 2011 Bob Dougherty &amp;lt;bobd@stanford.edu&amp;gt;\n&amp;quot;);&lt;br /&gt;
  Serial &amp;lt;&amp;lt; F(&amp;quot;* http://cniweb.stanford.edu/wiki/CNI_widgets\n&amp;quot;);&lt;br /&gt;
  Serial &amp;lt;&amp;lt; F(&amp;quot;*********************************************************\n\n&amp;quot;);&lt;br /&gt;
 &lt;br /&gt;
  pinMode(g_inPin, INPUT);&lt;br /&gt;
  pinMode(g_outPin, OUTPUT);&lt;br /&gt;
  digitalWrite(g_outPin, LOW);&lt;br /&gt;
  setInPulseState(DEFAULT_IN_PULSE_STATE);&lt;br /&gt;
 &lt;br /&gt;
  // Attach the callback function to the Messenger&lt;br /&gt;
  g_message.attach(messageReady);&lt;br /&gt;
 &lt;br /&gt;
  Serial &amp;lt;&amp;lt; F(&amp;quot;CNI Trigger Ready. Send the ? command ([?]) for help.\n\n&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void loop(){&lt;br /&gt;
  // Reset the output, if needed:&lt;br /&gt;
  if(g_outPinOn){&lt;br /&gt;
    // Turn off the output pin after the requested duration.&lt;br /&gt;
    // Detect and correct counter wrap-around:&lt;br /&gt;
    if(millis()&amp;lt;g_outPulseStart) g_outPulseStart += 4294967295UL;&lt;br /&gt;
    if(millis()-g_outPulseStart &amp;gt; g_outPulseDuration)&lt;br /&gt;
      digitalWrite(g_outPin, LOW);&lt;br /&gt;
  }&lt;br /&gt;
  // Handle Messenger&amp;#039;s callback:&lt;br /&gt;
  if(Serial.available())  g_message.process(Serial.read());&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void setInPulseState(byte state){&lt;br /&gt;
  // Turn on the internal pull-up resistor if we want to detect falling edges.&lt;br /&gt;
  if(g_inPulseEdge==FALLING)&lt;br /&gt;
    digitalWrite(g_inPin, HIGH);&lt;br /&gt;
  else&lt;br /&gt;
    digitalWrite(g_inPin, LOW);&lt;br /&gt;
  // Attach or detach the interrupt&lt;br /&gt;
  if(state)&lt;br /&gt;
    attachInterrupt(INTERRUPT, triggerIn, g_inPulseEdge);&lt;br /&gt;
  else&lt;br /&gt;
    detachInterrupt(INTERRUPT);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 // The following is an interrupt routine that is run each time the &lt;br /&gt;
 // a pulse is detected on the trigger input pin.&lt;br /&gt;
 void triggerIn(){&lt;br /&gt;
  Serial &amp;lt;&amp;lt; F(&amp;quot;p&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void triggerOut(){&lt;br /&gt;
    digitalWrite(g_outPin, HIGH);&lt;br /&gt;
    g_outPinOn = true;&lt;br /&gt;
    g_outPulseStart = millis();&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>imported&gt;Wandell</name></author>
	</entry>
</feed>