Wednesday, January 19, 2011

Weekly Update: Arduino and Shaft Encoders

Arduino
  • Lesson 4
  • Lesson 5
  • Pair Shaft Encoder with Arduino
  • Basic Encoder Code
 Shaft Encoder and Code

int inputPin = 2;
int val;                    
int rotationState;       
int inputSwitch = 0;         
float wheelRadius = 1.125;
int spokes = 44;
float distMet;
float rads;
int deg;
long numDeg;

void setup() {
  pinMode(inputPin, INPUT);   

  Serial.begin(9600);          
  rotationState = digitalRead(inputPin);  
}


void loop(){
  val = digitalRead(inputPin);      

  if (val != rotationState) {       
    if (val == LOW) {               
      inputSwitch++;
      deg = 360/spokes;
      numDeg = inputSwitch * deg;
      rads = radians(numDeg);
      Serial.print("Radians: ");     
      Serial.println(rads);
      distMet = (wheelRadius * rads)/39.3700787;
      Serial.print("Rotation Distance ");
      Serial.print(distMet);
      Serial.println(" Meters.");
    }
  }
  rotationState = val;               
}

No comments:

Post a Comment