Hi all,
I haven't had my Arduino terribly long, but one of my first goals was to get it talking to my Lego motors as to broaden my horizons. However, I have become frustrated quickly. Here is the code I'm using:
/*
Motor Test
*/
volatile int tack0 = 0;
volatile int tack1 = 0;
volatile int degree = 0;
volatile int count = 0;
int speedPin = 9;
int tacho0 = 2;
int tacho1 = 13;
int motor1APin = 10;
int motor2APin = 8;
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 6, 5, 4, 3);
void setup() {
// set up the LCD's number of rows and columns:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, world!");
pinMode(speedPin, OUTPUT);
pinMode(motor1APin, OUTPUT);
pinMode(tacho0, INPUT);
digitalWrite(tacho0, HIGH);
pinMode(motor2APin, OUTPUT);
pinMode(tacho1, INPUT);
digitalWrite(tacho1, HIGH);
attachInterrupt(0, encode, RISING);
}
void loop() {
}
void encode() {
tack1 = digitalRead(tacho1);
tack0 = digitalRead(tacho0);
count++;
if (tack0!=1) {
tack0 = 1;
lcd.setCursor(14,1);
lcd.print("!");
}
if (tack0 == tack1) {
degree++;
} else {
degree--;
}
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(degree);
lcd.setCursor(6,1);
lcd.print(count);*/
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(degree);
lcd.setCursor(6,1);
lcd.print(count);
lcd.setCursor(13,1);
lcd.print(tack0);
lcd.setCursor(15,1);
lcd.print(tack1);
}
Basically, what I've noticed is that if I spin the motor too quickly, it doesn't keep track of the rotations very well (for instance, tack0 doesn't always equal 1...). Furthermore, the interrupts seem to all register, but if I turn it too quickly (on the NXT, the encoder records every degree of rotation), it looks like the other digital signal has already switched to something else, so that it reads that the motor is turning in the wrong direction.
I've only got the one motor attached, and I've tried to streamline the code so that it reads in the signals as soon as an interrupt is called, giving it minimal time to change. Is this something the arduino just can't handle? (I'm not turning it more than a couple RPS)
Additionally, I never figured it out, but during my debugging, it seemed that the interrupt would often only be called in twos... that is, the "count" variable would only be an even number... can't figure out why.
Any advice/suggestions would be greatly appreciated.
Thanks!
