Solenoid Engine – RPI – C – with speed keys

soleng1.c
Code for the Solenoid Engine for a Raspberry PI with log output and faster/slower/quit keys save as soleng1.c

<br />
/*<br />
#######################################################<br />
# Sol engine V1 #<br />
# compile with gcc soleng1.c -o soleng1 -lwiringPi #<br />
# Ext Kits 4/11/16 #<br />
#######################################################<br />
*/</p>
<p>#include &lt;stdio.h&gt;<br />
#include &lt;wiringPi.h&gt;<br />
#include &quot;conio.h&quot;<br />
#include &lt;stdlib.h&gt;</p>
<p>//define pins<br />
#define NOTEYE 7<br />
#define SOL 0<br />
#define MAXSPEED 100<br />
#define MINSPEED 2000<br />
#define LOGFILE &quot;sollog.txt&quot;<br />
#define DEBUG 0<br />
#define STATSSEC 5</p>
<p> int StatTimer;<br />
 int Speed=400;<br />
 int Revs=0;<br />
 int Pulses=0;<br />
 int StatCnt=0;</p>
<p> void DoStats(void);<br />
 void DoButtons(void);<br />
 void AppendtoFile( char *text);</p>
<p>int main (void)<br />
{</p>
<p> int ison;<br />
 int last_ison;<br />
 int ready=1;<br />
 int pulsetime;<br />
 wiringPiSetup () ;</p>
<p> remove(LOGFILE);</p>
<p> pinMode (NOTEYE, INPUT) ;<br />
 pinMode (SOL, OUTPUT) ;</p>
<p> set_conio_terminal_mode();<br />
 StatTimer=millis();</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;Speed){<br />
        ready=1;<br />
      }<br />
      Revs++;<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;Pulses++;}<br />
    }else{<br />
      digitalWrite (SOL, 0) ;<br />
    }</p>
<p>//debug<br />
  if(DEBUG){<br />
    printf(&quot;ison=%i ready=%i Speed=%i pt=%i mills=%i\n\r&quot;,ison,ready,Speed,millis()-pulsetime,millis());<br />
  }<br />
//set last condition<br />
    last_ison=ison;</p>
<p>//speed buttons<br />
  DoButtons();</p>
<p>//do Stats<br />
  DoStats();<br />
  }</p>
<p>  return 0 ;<br />
}</p>
<p>void DoButtons(){<br />
  char c;</p>
<p>//speed buttons<br />
  if(kbhit()){<br />
    c=getch();<br />
    if(c=='q'){exit(0);}<br />
    if(c=='8'){if(Speed&gt;MAXSPEED){Speed--;printf(&quot;Speed Up %i\n\r&quot;,Speed);}}<br />
    if(c=='2'){if(Speed&lt;MINSPEED){Speed++;printf(&quot;Speed Down %i\n\r&quot;,Speed);}}</p>
<p>  }<br />
}</p>
<p>void DoStats(void){<br />
 if (millis()-StatTimer&gt;STATSSEC*1000){<br />
 StatTimer=millis();<br />
 if(DEBUG){ printf(&quot;Do Stats \n\r&quot;);}<br />
 char text[100];<br />
 int rpm=0;<br />
 rpm=Revs*60/STATSSEC;<br />
 StatCnt++;<br />
 sprintf(text,&quot;CNT:%i REVS:%i PULSES:%i RPM:%i \n\r&quot;,StatCnt,Revs,Pulses,rpm);</p>
<p> AppendtoFile(text);<br />
 Pulses=0;<br />
 Revs=0;<br />
 }<br />
}</p>
<p>void AppendtoFile( char *text){<br />
 FILE *log;<br />
 char filename[100] = LOGFILE;<br />
 log = fopen (filename, &quot;a&quot;);</p>
<p> fprintf(log,&quot;%s&quot;,text);<br />
 fclose(log);<br />
}</p>
<p>

You will also need this header file conio.h

</p>
<p>#include &lt;stdlib.h&gt;<br />
#include &lt;string.h&gt;<br />
#include &lt;unistd.h&gt;<br />
#include &lt;sys/select.h&gt;<br />
#include &lt;termios.h&gt;</p>
<p>struct termios orig_termios;</p>
<p>void reset_terminal_mode()<br />
{<br />
 tcsetattr(0, TCSANOW, &amp;orig_termios);<br />
}</p>
<p>void set_conio_terminal_mode()<br />
{<br />
 struct termios new_termios;</p>
<p> /* take two copies - one for now, one for later */<br />
 tcgetattr(0, &amp;orig_termios);<br />
 memcpy(&amp;new_termios, &amp;orig_termios, sizeof(new_termios));</p>
<p> /* register cleanup handler, and set the new terminal mode */<br />
 atexit(reset_terminal_mode);<br />
 cfmakeraw(&amp;new_termios);<br />
 tcsetattr(0, TCSANOW, &amp;new_termios);<br />
}</p>
<p>int kbhit()<br />
{<br />
 struct timeval tv = { 0L, 0L };<br />
 fd_set fds;<br />
 FD_ZERO(&amp;fds);<br />
 FD_SET(0, &amp;fds);<br />
 return select(1, &amp;fds, NULL, NULL, &amp;tv);<br />
}</p>
<p>int getch()<br />
{<br />
 int r;<br />
 unsigned char c;<br />
 if ((r = read(0, &amp;c, sizeof(c))) &lt; 0) {<br />
 return r;<br />
 } else {<br />
 return c;<br />
 }<br />
}</p>
<p>