Advertisement
Guest User

Untitled

a guest
Mar 1st, 2013
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.41 KB | None | 0 0
  1. const int I1_PIN = 6;
  2. const int I2_PIN = 7;
  3. const int SPEED_PIN = 3; // PWM output
  4. const int HALL_A_PIN = 2;
  5. const int HALL_B_PIN = 4;
  6.  
  7. volatile int curPos = 0;
  8.  
  9. volatile int prevA = LOW;
  10. volatile int prevB = LOW;
  11. int prevPos = 0;
  12.  
  13. volatile boolean stateChanged = false;
  14.  
  15. void setup() {
  16.   Serial.begin(9600);
  17.   pinMode(I1_PIN, OUTPUT);
  18.   pinMode(I2_PIN, OUTPUT);
  19.   pinMode(SPEED_PIN, OUTPUT);
  20.  
  21.   delay(100);
  22.   attachInterrupt(0, hallChange, CHANGE); // interrupt on pin 2
  23.   // Move clockwise
  24.   digitalWrite(I1_PIN, LOW);
  25.   digitalWrite(I2_PIN, HIGH);
  26.   analogWrite(SPEED_PIN, 100);
  27. }
  28.  
  29. void loop() {
  30.   Serial.println(digitalRead(HALL_A_PIN));
  31.   if (curPos != prevPos) {
  32.     Serial.println(curPos);
  33.     prevPos = curPos;
  34.   }
  35.   /*
  36.   if (stateChanged) {
  37.     Serial.println("Changed");
  38.     stateChanged=false;
  39.   }*/
  40. }
  41.  
  42. void hallChange() {
  43.   stateChanged = true;
  44.   int a = digitalRead(HALL_A_PIN);
  45.   int b = digitalRead(HALL_B_PIN);
  46.   if (a == HIGH && b == LOW) {
  47.     if (prevA == LOW && prevB == LOW) {  
  48.       curPos = 1;
  49.     }
  50.   }
  51.   else if (a == HIGH && b == HIGH) {
  52.     if (prevA == HIGH && prevB == LOW) {
  53.       curPos = 2;
  54.     }
  55.   }
  56.   else if (a == LOW && b == HIGH) {
  57.     if (prevA == HIGH && prevB == HIGH) {
  58.       curPos = 3;
  59.     }
  60.   }
  61.   else if (a == LOW && b == LOW) {
  62.     if (prevA == LOW && prevB == HIGH) {
  63.       curPos = 4;
  64.     }
  65.   }
  66.  
  67.   prevA = a;
  68.   prevB = b;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement