Skip to content

Instantly share code, notes, and snippets.

@AnandChowdhary
Created September 25, 2018 11:49
Show Gist options
  • Save AnandChowdhary/1e147ce3ab6b6c0083650e280af1e4f5 to your computer and use it in GitHub Desktop.
Save AnandChowdhary/1e147ce3ab6b6c0083650e280af1e4f5 to your computer and use it in GitHub Desktop.
Tutorial 4
int potentiometerPin = A0;
int sensorPin1 = 3;
int sensorPin2 = 2;
long potentiometerValue = 0;
int sensor1Value = 0;
int sensor2Value = 0;
volatile long position;
void setup() {
// Set up internal pull up resistor for pin
pinMode(sensorPin1, INPUT_PULLUP);
pinMode(sensorPin2, INPUT_PULLUP);
pinMode(sensorPin1, HIGH);
pinMode(sensorPin2, HIGH);
attachInterrupt(0, encoderA, CHANGE);
attachInterrupt(1, encoderB, CHANGE);
Serial.begin(1200);
}
void loop() {
potentiometerValue = analogRead(potentiometerPin);
sensor1Value = analogRead(sensorPin1);
sensor2Value = analogRead(sensorPin2);
Serial.print(3600 * potentiometerValue / 1024);
Serial.print(" ");
Serial.print(3600 * position / 1024);
Serial.println(" ");
}
void encoderA() // encoder service routine
{
int A = digitalRead(sensorPin1);
int B = digitalRead(sensorPin2);
if ((A == 1 && B == 0) || (A == 0 && B == 1)) position++;
if ((A == 1 && B == 1) || (A == 0 && B == 0)) position--;
}
void encoderB() // encoder service routine
{
int A = digitalRead(sensorPin1);
int B = digitalRead(sensorPin2);
if ((A == 1 && B == 0) || (A == 0 && B == 1)) position--;
if ((A == 1 && B == 1) || (A == 0 && B == 0)) position++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment