-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBallTouch.cpp
61 lines (49 loc) · 1.19 KB
/
BallTouch.cpp
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
#include "BallTouch.hpp"
BallTouch::BallTouch(int pinLed, int pinSensor){
_pinLed = pinLed;
_pinSensor = pinSensor;
}
void BallTouch::init(){
_threshold = (int)EEPROM.read(EEPROM_BALLTOUCH_THRESHOLD) * 4;
pinMode(_pinLed, OUTPUT);
pinMode(_pinSensor, INPUT_PULLUP);
}
void BallTouch::calibrate(){
_threshold = _value - 20;
EEPROM.write(EEPROM_BALLTOUCH_THRESHOLD, (byte)_threshold / 4);
}
void BallTouch::update(){
if(_state == LED_ON && millis()-_onTimer > 10){
turnOff();
calculate();
}
if(_state == LED_OFF && millis()-_offTimer > 10){
turnOn();
calculate();
}
debug("Threshold: ");
debug(_threshold);
debug(" Value: ");
debug(_value);
}
int BallTouch::getValue(){
return _value;
}
bool BallTouch::hasBall(){
return _value > _threshold;
}
void BallTouch::turnOn(){
_darkValue = analogRead(_pinSensor);
digitalWrite(_pinLed, HIGH);
_onTimer = millis();
_state = LED_ON;
}
void BallTouch::turnOff(){
_lightValue = analogRead(_pinSensor);
digitalWrite(_pinLed, LOW);
_offTimer = millis();
_state = LED_OFF;
}
void BallTouch::calculate(){
_value = (_darkValue-_lightValue);
}