Yeah, you're probably right.....I am getting in the habit of impulse purchases
Anyway, I am proceeding with the code development. Since Martin has offered to get this clock thing worked out for the ATtiny, I am now working on the development of a throttle-timer based on the simple code presented earlier.
I have streamlined it, omitting those unnecessary array transfers, and updating the channels straight from the ISR. The latter now waits for the sync pulse to occur, then starts to update ch[1], ch[2], etc with the next pulses.
The pulse duration is not very stable, varying by about 4 microseconds both ways (as I expected), but it is good enough for the throttle timer project, as it requires just seconds and minutes to be shown on the screen.
Here is the code:
Code: Select all
// Ch[0] remains at 0.
unsigned long int mark,oldmark=0,space;
volatile int ch[7];
int count=1;
bool start = false;
void setup() {
Serial.begin(9600);
pinMode(2, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(2), read_me, RISING); // enabling interrupt at pin 2
}
void loop() {
Serial.print(ch[1]); Serial.print("\t");
Serial.print(ch[2]); Serial.print("\t");
Serial.print(ch[3]); Serial.print("\t");
Serial.print(ch[4]); Serial.print("\t");
Serial.print(ch[5]); Serial.print("\t");
Serial.print(ch[6]); Serial.print("\n");
delay(100);
}
void read_me() { // ISR.
mark=micros(); // store time value when PPM pin status rises.
space=mark-oldmark; // calculating time between two peaks
oldmark=mark;
if (start) {
ch[count]=space;
count+=1;
if (count>6) { // restrict channel value recording to 6 channels
count = 1;
start = false;
}
}
// detect sync space and set start flag for channel recording from next spaces.
if ((space>10000) && (space<20000)) start = true; // sync space detected.
}
Cheers,
Max.