Sol Engine – RPI code C

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

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

#include <stdio.h>
#include <wiringPi.h>

//define pins
// WiringPi 7 = gpio 4
#define NOTEYE  7
//wiringPi 0 = gpio 17
#define SOL     0
#define MAXSPEED 1000

int main (void)
{

  int ison; // asking for a power stroke
  int last_ison; // saved state of is on
  int ready=1; // ready for next pulse
  int pulsetime; //time of last pulse

  wiringPiSetup () ;

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

  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>MAXSPEED){
        ready=1;
      }
    }

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

//debug
    printf("ison=%i ready=%i pt=%i mills=%i\n",ison,ready,millis()-pulsetime,mi

//set last condition
    last_ison=ison;

  }
  return 0 ;
}