Hi,
I'm trying to make a cataloguer to log current and motor temp on a rc car. Ideally using a channel on the rc to start and stop logging.
So far, I have interrupt code to measure the PWM width and start and stop logging depending on a threshold value. This Works OK.
The problem Im getting is that I'm getting a error in the analogue read, easier to show an example than explain. Below is data from the serial link.
12:10:19.782 -> START
12:10:19.782 -> 707
12:10:19.819 -> 709
12:10:19.819 -> 30
12:10:19.852 -> 30
12:10:19.852 -> 30
12:10:19.889 -> 31
12:10:19.889 -> 30
12:10:19.924 -> 30
12:10:19.957 -> 31
12:10:19.957 -> 31
12:10:19.992 -> 30
12:10:19.992 -> 30
12:10:20.027 -> 30
12:10:20.027 -> 30
12:10:20.060 -> 30
12:10:20.094 -> 707
12:10:20.094 -> 707
12:10:20.131 -> 707
12:10:20.131 -> 30
12:10:20.166 -> 30
12:10:20.204 -> 30
12:10:20.204 -> 30
12:10:20.241 -> 30
12:10:20.241 -> 30
12:10:20.275 -> 30
This is at development stage, so all I have for an input is a pot, looking at the above data I'm getting spurious 707 values at regular intervals. I'm guessing this is being caused by a misread of the analogue during interrupt.
Any idea how to prevent this?
//assume that pin 32 is receiving PWM input
#define CHANNEL_1_PIN 2
int pulse_time = 0;
int dump = 0;
bool startLogging = LOW;
bool endSent = HIGH;
bool startSent = LOW;
const int analogInPin = A0;
bool pause = LOW;
//micros when the pin goes HIGH
volatile unsigned long timer_start;
//difference between timer_start and micros() is the length of time that the pin
//was HIGH - the PWM pulse length. volatile int pulse_time;
//this is the time that the last interrupt occurred.
//you can use this to determine if your receiver has a signal or not.
volatile int last_interrupt_time; //calcSignal is the interrupt handler
void calcSignal()
{
//record the interrupt time so that we can tell if the receiver has a signal from the transmitter
last_interrupt_time = micros();
pause = HIGH;
//analogRead(analogInPin);
//if the pin has gone HIGH, record the microseconds since the Arduino started up
if(digitalRead(CHANNEL_1_PIN) == HIGH)
{
timer_start = micros();
}
//otherwise, the pin has gone LOW
else
{
//only worry about this if the timer has actually started
if(timer_start != 0)
{
//record the pulse time
pulse_time = (micros() - timer_start);
//restart the timer
timer_start = 0;
}
}
dump = analogRead(analogInPin);
}
//this is all normal arduino stuff
void setup()
{
timer_start = 0;
pinMode(12, OUTPUT);
attachInterrupt(CHANNEL_1_PIN, calcSignal, CHANGE);
Serial.begin(9600);
pause = LOW;
}
void loop()
{
//Serial.println(pulse_time);
if (pulse_time > 1750){
Serial.println("START");
endSent = LOW;
digitalWrite(12, HIGH);
while(pulse_time > 1750 ){
if(!pause){
Serial.println(dump) ;
}
delay(205);
pause = LOW;
}
}
if (!endSent ){
Serial.println("END");
endSent = HIGH;
digitalWrite(12, LOW);
}
}