So I am working on getting my car VSS PWM signal inputed into my redBOARD via PWM PIN5.
When I have nothing connected to pin5 I get a 0 reading, but when I connect a lead(the lead only) to PIN5 I get random readings of 50 thru 54. Also, I get higher random readings when I connect the PWM(VSS) non-energized lead to PIN5, which is around 80-100.
[When I connect the VSS and engage it I see a difference, i.e. a reading that is in addition to the random readings.
// Digital speedometer
// VSS on car connects to pin 5
const int hardwareCounterPin = 5;
const int samplePeriod = 1000; //in milliseconds
const float pulsesPerMile = 4000; // this is pulses per mile for Toyota. Other cars are different.
const float convertMph = pulsesPerMile/3600;
unsigned int count;
float mph;
unsigned int imph;
int roundedMph;
int previousMph;
int prevCount;
void setup() {
TCCR1B = 0; //Configure hardware counter
TCNT1 = 0; // Reset hardware counter to zero
Serial.begin(9600);
}
void loop() {
//////////////////////////////////////////////////
// This uses the hardware pulse counter on the Arduino. //
// Currently it collects samples for one second. //
//////////////////////////////////////////////////
bitSet(TCCR1B, CS12); // start counting pulses
bitSet(TCCR1B, CS11); // Clock on rising edge
delay(samplePeriod); // Allow pulse counter to collect for samplePeriod
TCCR1B = 0; // stop counting
count = TCNT1; // Store the hardware counter in a variable
TCNT1 = 0; // Reset hardware counter to zero
mph = (count/convertMph)*10; // Convert pulse count into mph.
imph = (unsigned int) mph; // Cast to integer. 10x allows retaining 10th of mph resolution.
int x = imph / 10;
int y = imph % 10;
// Round to whole mile per hour
if(y >= 5){
roundedMph = x + 1;
}else{
roundedMph = x;
}
//If mph is less than 1mph just show 0mph.
//Readings of 0.9mph or lower are some what erratic and can
//occasionally be triggered by electrical noise.
if(x == 0){
roundedMph = 0;
}
// Don't display mph readings that are more than 50 mph higher than the
// previous reading because it is probably a spurious reading.
// Accelerating 50mph in one second is rocketship fast so it is probably
// not real.
if((roundedMph - previousMph) > 50){
Serial.print("Previous MPH = ");
Serial.println(previousMph);
}else{
Serial.print("Rounded MPH = ");
Serial.println(roundedMph);
}
previousMph = roundedMph; // Set previousMph for use in next loop.
}