Hi everyone, I have a working sketch that reads the memsic 2125 accelerometer using pulsein. I am trying to convert it to using interrupts but I am getting bizarre behavior. Here is the working example:
byte xPin = 2;
byte yPin = 3;
byte value = 0;
int count = 0;
int pulse = 0;
void setup() {
beginSerial(57600);
for (int i = 2; i <= 8; i++) {
pinMode(i, INPUT);
}
}
void loop() {
readTiltPulse(xPin);
readTiltPulse(yPin);
}
void readTiltPulse(int axe) {
value = digitalRead(axe);
while (value == HIGH) { // Loop until pin reads a low
value = digitalRead(axe);
}
pulse = pulseIn(axe, HIGH);
if (axe == xPin) {
Serial.print("X ");
Serial.print(pulse);
Serial.print("\t");
} else {
Serial.print("Y ");
Serial.println(pulse);
}
}
With this I get smooth, consistent output like this (with the unit lying nearly flat):
X 4948 Y 4914
X 4947 Y 4916
X 4948 Y 4913
X 4949 Y 4913
X 4948 Y 4916
X 4944 Y 4916
... plenty more
Here is my sketch with interrupts (only X-axis so far).
volatile unsigned long currentTicksValueX = 0;
volatile unsigned long currentTicksValueY = 0;
volatile unsigned long lastStartX = 0;
volatile unsigned long lastStartY = 0;
volatile unsigned long dutyCycleX = 0;
volatile unsigned long dutyCycleY = 0;
void setup (void) {
Serial.begin(57600);
for (int i = 2; i <= 13; i++) {
pinMode(i, INPUT);
}
attachInterrupt(0, onChangeX, CHANGE);
//attachInterrupt(1, onChangeY, CHANGE);
}
void loop (void) {
/*Serial.print("X: ");
Serial.print(dutyCycleX);
Serial.print("\t");
Serial.print("Y: ");
Serial.println(dutyCycleY);
delay(500);*/
}
void onChangeX() {
currentTicksValueX = (millis() << 8) + TCNT0;
if (digitalRead(2) == HIGH) {
lastStartX = currentTicksValueX;
} else {
dutyCycleX = currentTicksValueX - lastStartX;
}
Serial.println(dutyCycleX);
}
This yields output like so:
1250
1250
1250
1250
1250
1505
1505
1249
1249
1249
... more
Notice the jump from 1250 to 1500 for no apparent reason (unit was resting). If I comment the serial print in the onChangeX function and enable the serial print in the loop function but comment out the delay I get something like this:
X: 1249 Y: 0
X: 1249 Y: 0
X: 1250 Y: 0
X: 33889976320 Y: 0
X: 1249 Y: 0
X: 1249 Y: 0
X: 1249 Y: 0
X: 1506 Y: 0
X: 1506 Y: 0
X: 1506 Y: 0
X: 1506 Y: 0
X: 1250 Y: 0
X: 1250 Y: 0
X: 1250 Y: 0
X: 1250 Y: 0
X: 1249 Y: 0
X: 1249 Y: 0
X: 1249 Y: 0
X: 1249 Y: 0
X: 49739776249 Y: 0
X: 1249 Y: 0
X: 1249 Y: 0
X: 1249 Y: 21102222012220102001
X: 1503 Y: 0
X: 1503 Y: 0
X: 1249 Y: 0
X: 1249 Y: 0
X: 1249 Y: 0
X: 1249 Y: 0
X: 66152249 Y: 0
X: 1249 Y: 0
X: 1249 Y: 0
X: 1249 Y: 0
X: 1250 Y: 0
X: 1250 Y: 0
X: 1250 Y: 0
X: 1250 Y: 0
X: 2730491904250 Y: 0
X: 1250 Y: 0
X: 1250 Y: 0
...more
If I enable the delay(500) I get something like this:
X: 0 Y: 0
X: 462 Y: 0
X: 462 Y: 0
X: 1250 Y: 0
X: 1250 Y: 0
X: 1249 Y: 0
X: 1249 Y: 0
X: 1249 Y: 0
X: 1249 Y: 0
X: 1249 Y: 0
X: 1249 Y: 0
X: 1250 Y: 0
X: 1250 Y: 0
X: 1249 Y: 0
X: 1249 Y: 0
What's interesting is that this is ALL the output. It stops altogether while the other examples go on forever.
Can anybody spot some trouble that would cause this?
Thanks.
I'm using Arduino 12.
/Daryl