Offline
Newbie
Karma: 0
Posts: 4
|
 |
« Reply #2 on: July 21, 2011, 10:10:53 am » |
Ignore all the comments This is when the encoder was running off two interrupts. I think I am going to try to do it with just one interrupt.
#include <Servo.h> Servo myservo; // create servo object to control a servo //int val; // variable to read the value from the analog pin
#define encoder0PinA 2 #define encoder0PinB 3 //int D2 = 5; //int EN = 6; int D1 = 5; int D2 = 6; ///previously inv pin 7 //int invertswitch = 8; //int motorPin2 = 8; //Servo Pin 9 //int motorPin1 = 10;
//analog pins //int Pot = 0; //MOTOR //int potpin = 1; //SERVO
volatile unsigned int encoder0Pos = 0; //const int inc = 1; //volatile int encoder0Pos = 0.0; //const int inc = 1.0; void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object pinMode(encoder0PinA, INPUT); // digitalWrite(encoder0PinA, HIGH); pinMode(encoder0PinB, INPUT); // digitalWrite(encoder0PinB, HIGH); // encoder pin on interrupt 0 (pin 2) attachInterrupt(0, doEncoderA, CHANGE); // encoder pin on interrupt 1 (pin 3) attachInterrupt(1, doEncoderB, CHANGE); Serial.begin (115200); //pinMode (EN, OUTPUT); // pinMode (motorPin1, OUTPUT); //pinMode (motorPin2, OUTPUT); //pinMode (D2, OUTPUT); pinMode (D1, OUTPUT); pinMode (D2, OUTPUT); // previously INV //pinMode (Pot, INPUT); //pinMode (invertswitch, INPUT); } //int getPot() { //MOTOR //int v; //MOTOR // v = analogRead(Pot); //MOTOR // v /= 4; //MOTOR // v = max(v,0); //MOTOR // v = min(v,255); //MOTOR // return v; //MOTOR } void loop(){ //val = analogRead(potpin); // SERVO reads the value of the potentiometer (value between 0 and 1023) //val = map(val, 0, 1023, 0, 179); // SERVO scale it to use it with the servo (value between 0 and 180) myservo.write(90); // SERVO CONTROL 0 TO 90 ...should be val SERVO sets the servo position according to the scaled value //delay(15); // SERVO waits for the servo to get there //digitalWrite (motorPin1, HIGH); //digitalWrite (motorPin2, LOW); //digitalWrite (D2, HIGH); analogWrite (D1, 127); //should be GETPOT MOTOR CONTROL PWM analogWrite (D2, 127); // MOTOR CONTROL PWM // if (digitalRead(invertswitch)==HIGH) // { // digitalWrite (INV, LOW); // } // else // { // digitalWrite (INV, HIGH) ; // } //digitalWrite (EN,HIGH);
}
void doEncoderA() { // look for a low-to-high on channel A if (digitalRead(encoder0PinA) == HIGH) { // check channel B to see which way encoder is turning if (digitalRead(encoder0PinB) == LOW) { encoder0Pos = encoder0Pos + 1; // CW } else { encoder0Pos = encoder0Pos - 1; // CCW } } else // must be a high-to-low edge on channel A { // check channel B to see which way encoder is turning if (digitalRead(encoder0PinB) == HIGH) { encoder0Pos = encoder0Pos + 1; // CW } else { encoder0Pos = encoder0Pos - 1; // CCW } } Serial.println (encoder0Pos, DEC); // use for debugging - remember to comment out }
void doEncoderB(){ // look for a low-to-high on channel B if (digitalRead(encoder0PinB) == HIGH) { // check channel A to see which way encoder is turning if (digitalRead(encoder0PinA) == HIGH) { encoder0Pos = encoder0Pos + 1; // CW } else { encoder0Pos = encoder0Pos - 1; // CCW } } // Look for a high-to-low on channel B else { // check channel B to see which way encoder is turning if (digitalRead(encoder0PinA) == LOW) { encoder0Pos = encoder0Pos + 1; // CW } else { encoder0Pos = encoder0Pos - 1; // CCW } } Serial.println(encoder0Pos, DEC); }
|