-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy patharduino_uno_counter_ir_sensor.ino
57 lines (42 loc) · 1.37 KB
/
arduino_uno_counter_ir_sensor.ino
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
#include <Wire.h> // Enable this line if using Arduino Uno, Mega, etc.
//#include <TinyWireM.h> // Enable this line if using Adafruit Trinket, Gemma, etc.
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
Adafruit_7segment matrix = Adafruit_7segment(); //end Adafruit requirements
const int sensorPin = 0; //analog pin 0
const int ledPin = 13; // the pin that the LED is attached to
// vars
int sensorCounter = 0; // counter for the number of button presses
int sensorState = 0; // current state of the sensor
void setup() {
// initialize the sensor pin and input
pinMode(sensorPin, INPUT);
// initialize the LED as an output
pinMode(ledPin, OUTPUT);
// initialize serial communication
Serial.begin(9600);
// set the matrix
matrix.begin(0x70);
}
void loop() {
// read the sensor input pin:
sensorState = analogRead(sensorPin);
// if sensor is closer than than x distance
if (sensorState > 460 ) {
sensorCounter++;
Serial.println(sensorState);
Serial.print("number of counts: ");
Serial.println(sensorCounter);
// show output on the digital display
matrix.print(sensorCounter);
matrix.writeDisplay();
// slow down the output
delay(500);
}
// turns on the LED every ten counts
if (sensorCounter % 10 == 0) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}