Solenoid Engine Single – RPI code C

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
#######################################################
# 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 ;
}