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

/*
#######################################################
# Sol engine V1 #
# compile with gcc soleng1.c -o soleng1 -lwiringPi #
# Ext Kits 4/11/16 #
#######################################################
*/

#include <stdio.h>
#include <wiringPi.h>
#include "conio.h"
#include <stdlib.h>

//define pins
#define NOTEYE 7
#define SOL 0
#define MAXSPEED 100
#define MINSPEED 2000
#define LOGFILE "sollog.txt"
#define DEBUG 0
#define STATSSEC 5

 int StatTimer;
 int Speed=400;
 int Revs=0;
 int Pulses=0;
 int StatCnt=0;

 void DoStats(void);
 void DoButtons(void);
 void AppendtoFile( char *text);


int main (void)
{

 int ison;
 int last_ison;
 int ready=1;
 int pulsetime;
 wiringPiSetup () ;

 remove(LOGFILE);

 pinMode (NOTEYE, INPUT) ;
 pinMode (SOL, OUTPUT) ;

 set_conio_terminal_mode();
 StatTimer=millis();


 for (;;)
 {
//get eye status
 //ison=!digitalRead(NOTEYE); //V1
 ison=digitalRead(NOTEYE); //V2
//start of stroke check speed
    if(ison==1 && last_ison==0){
      //had a pulse
      if (millis()-pulsetime>Speed){
        ready=1;
      }
      Revs++;
    }

//if pulse needed turn on solenoid
    if(ready){
      digitalWrite (SOL, ison) ;
      pulsetime=millis();
      if (ison==0){ready=0;Pulses++;}
    }else{
      digitalWrite (SOL, 0) ;
    }

//debug
  if(DEBUG){
    printf("ison=%i ready=%i Speed=%i pt=%i mills=%i\n\r",ison,ready,Speed,millis()-pulsetime,millis());
  }
//set last condition
    last_ison=ison;

//speed buttons
  DoButtons();

//do Stats
  DoStats();
  }


  return 0 ;
}

void DoButtons(){
  char c;

//speed buttons
  if(kbhit()){
    c=getch();
    if(c=='q'){exit(0);}
    if(c=='8'){if(Speed>MAXSPEED){Speed--;printf("Speed Up %i\n\r",Speed);}}
    if(c=='2'){if(Speed<MINSPEED){Speed++;printf("Speed Down %i\n\r",Speed);}}

  }
}

void DoStats(void){
 if (millis()-StatTimer>STATSSEC*1000){
 StatTimer=millis();
 if(DEBUG){ printf("Do Stats \n\r");}
 char text[100];
 int rpm=0;
 rpm=Revs*60/STATSSEC;
 StatCnt++;
 sprintf(text,"CNT:%i REVS:%i PULSES:%i RPM:%i \n\r",StatCnt,Revs,Pulses,rpm);

 AppendtoFile(text);
 Pulses=0;
 Revs=0;
 }
}

void AppendtoFile( char *text){
 FILE *log;
 char filename[100] = LOGFILE;
 log = fopen (filename, "a");

 fprintf(log,"%s",text);
 fclose(log);
}


You will also need this header file conio.h



#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#include <termios.h>

struct termios orig_termios;

void reset_terminal_mode()
{
 tcsetattr(0, TCSANOW, &orig_termios);
}

void set_conio_terminal_mode()
{
 struct termios new_termios;

 /* take two copies - one for now, one for later */
 tcgetattr(0, &orig_termios);
 memcpy(&new_termios, &orig_termios, sizeof(new_termios));

 /* register cleanup handler, and set the new terminal mode */
 atexit(reset_terminal_mode);
 cfmakeraw(&new_termios);
 tcsetattr(0, TCSANOW, &new_termios);
}

int kbhit()
{
 struct timeval tv = { 0L, 0L };
 fd_set fds;
 FD_ZERO(&fds);
 FD_SET(0, &fds);
 return select(1, &fds, NULL, NULL, &tv);
}

int getch()
{
 int r;
 unsigned char c;
 if ((r = read(0, &c, sizeof(c))) < 0) {
 return r;
 } else {
 return c;
 }
}