Sol Engine – RPI code C

soleng.c
Code for the Solenoid Engine for a Raspberry PI save as soleng.c

<br />
/*<br />
#######################################################<br />
# Sol engine v1                                       #<br />
# compile with    gcc soleng.c -o soleng -lwiringPi   #<br />
# Ext Kits 4/11/16                                    #<br />
#######################################################<br />
*/</p>
<p>#include &lt;stdio.h&gt;<br />
#include &lt;wiringPi.h&gt;</p>
<p>//define pins<br />
// WiringPi 7 = gpio 4<br />
#define NOTEYE  7<br />
//wiringPi 0 = gpio 17<br />
#define SOL     0<br />
#define MAXSPEED 1000</p>
<p>int main (void)<br />
{</p>
<p>  int ison; // asking for a power stroke<br />
  int last_ison; // saved state of is on<br />
  int ready=1; // ready for next pulse<br />
  int pulsetime; //time of last pulse</p>
<p>  wiringPiSetup () ;</p>
<p>  pinMode (NOTEYE, INPUT) ;<br />
  pinMode (SOL, OUTPUT) ;</p>
<p>  for (;;)<br />
  {<br />
//get eye status<br />
    //ison=!digitalRead(NOTEYE); //V1<br />
    ison=digitalRead(NOTEYE); //V2<br />
//start of stroke check speed<br />
    if(ison==1 &amp;&amp; last_ison==0){<br />
      //had a pulse<br />
      if (millis()-pulsetime&gt;MAXSPEED){<br />
        ready=1;<br />
      }<br />
    }</p>
<p>//if pulse needed turn on solenoid<br />
    if(ready){<br />
      digitalWrite (SOL, ison) ;<br />
      pulsetime=millis();<br />
      if (ison==0){ready=0;};<br />
    }else{<br />
      digitalWrite (SOL, 0) ;<br />
    }</p>
<p>//debug<br />
    printf(&quot;ison=%i ready=%i pt=%i mills=%i\n&quot;,ison,ready,millis()-pulsetime,mi</p>
<p>//set last condition<br />
    last_ison=ison;</p>
<p>  }<br />
  return 0 ;<br />
}<br />