From the Arduino environment, click the Terminal button and you'll see this output. Nothing fancy, but it allows accurate fiddling with transmitter controls.
This version of the code uses pulseIn(), which nicely does the right thing but suckily does it very slowly. It's fine if you're just printing out the values, but if you were wanting to do some real control work in between reading the signals, it wouldn't work very well.
#include "WProgram.h"
#define NCHAN (sizeof(chan)/sizeof(chan[0]))
int chan[]={2,3,4,5,6};
int val[NCHAN];
void setup()
{
int i;
for (i = 0; i < NCHAN; ++i)
pinMode(chan[i], INPUT);
Serial.begin(115200);
}
void loop()
{
int i;
for (i = 0; i < NCHAN; ++i)
val[i] = pulseIn(chan[i], HIGH);
Serial.println();
for (i = 0; i < NCHAN; ++i) {
Serial.print(i+1);
Serial.print(": ");
Serial.print(val[i]);
Serial.print(" ");
}
//delay(20);
}
Here's some code that uses interrupts to get the timings. This would be useful on an Arduino Mega, which supports interrupts on six pins. If you don't have a Mega, you've only got two pins so you're better off with the code above. There are two other problems with this code:
- micros() returns a value which is rounded to a multiple of 4 microseconds, totally unacceptable for tuning radio PWM signals.
- digitalRead() is very slow and can be made much faster.
I'm going to keep working on this approach, and I'll update later when I've got something to show. I'm also thinking of making a GUI using Processing.
// reading a PWM signal using interrupts int pin = 2; // arduino pin number int intrnum = 0; // interrupt number, 0-5 for mega, 0-1 for others volatile int width; // width of most recent signal volatile unsigned long start; // start time of rising signal // myisr -- interrupt handler void myisr() { unsigned long now = micros(); int val = digitalRead(2); // are we high (meaning pulse just started) // or low (meaning pulse just finished) ? if(val == HIGH) // ascending edge, just save off start time start = now; else // val == LOW, descending edge, compute pulse width width= now - start; } void setup() { Serial.begin(115200); pinMode(pin,INPUT); attachInterrupt(intrnum, myisr, CHANGE); } void loop() { Serial.println(width); delay(10); }
No comments:
Post a Comment