Project

General

Profile

Statistics
| Branch: | Revision:

robobuggy / arduino / Encoder / encoder / Enc / Enc.ino @ c5d6b0e8

History | View | Annotate | Download (812 Bytes)

1
// declare pins
2
int enc = 1;
3

    
4
// declare globals
5
int count = 0;
6
int state = 1; // 1 if was high, 0 if was low
7

    
8
//code that keeps loop time constant each loop
9
int STD_LOOP_TIME = 9;
10
int hz = 40;
11
int print_period = 1000 / hz;
12
unsigned long last_time = 0;
13

    
14
void setup(){
15
  Serial.begin(9600);
16
  pinMode(enc, INPUT);
17
}
18

    
19
/* returns 1 if there is a magnet
20
 * returns 0 otherwise
21
 */
22
int isMagnet(){
23
  int val = analogRead(enc);
24
  if (val < 512){
25
   return 0; 
26
  } else {
27
    return 1;
28
  }
29
}
30

    
31

    
32
void loop(){
33
  // START Loop timing control  ********************************
34
  if (last_time < (millis() - print_period)) {
35
    Serial.println(count);
36
    last_time = millis();
37
  }
38
  // END loop timing control    ********************************
39

    
40
  if (state != isMagnet()){
41
    count++; 
42
    state = !state;
43
  }
44
}
45

    
46

    
47

    
48

    
49

    
50

    
51

    
52