-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathencoder
More file actions
48 lines (38 loc) · 1.22 KB
/
encoder
File metadata and controls
48 lines (38 loc) · 1.22 KB
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
// Include the TimerOne Library from Paul Stoffregen
#include "TimerOne.h"
// Constants for Interrupt Pins
// Change values if not using Arduino Uno
const byte MOTOR1 = 2; // Motor 1 Interrupt Pin - INT 0
// Integers for pulse counters
unsigned int counter1 = 0;
// Float for number of slots in encoder disk
float diskslots = 20; // Change to match value of encoder disk
// Interrupt Service Routines
// Motor 1 pulse count ISR
void ISR_count1()
{
counter1++; // increment Motor 1 counter value
}
// TimerOne ISR
void ISR_timerone()
{
Timer1.detachInterrupt(); // Stop the timer
Serial.print("Motor Speed 1: ");
float rotation1 = (counter1 / diskslots) * 60.00; // calculate RPM for Motor 1
Serial.print(rotation1);
Serial.print(" RPM - ");
counter1 = 0; // reset counter to zero
Timer1.attachInterrupt( ISR_timerone ); // Enable the timer
}
void setup()
{
Serial.begin(9600);
Timer1.initialize(1000000); // set timer for 1sec
attachInterrupt(digitalPinToInterrupt (MOTOR1), ISR_count1, RISING); // Increase counter 1 when speed sensor pin goes High
Timer1.attachInterrupt( ISR_timerone ); // Enable the timer
}
void loop()
{
// Nothing in the loop!
// You can place code here
}