Skip to content

Instantly share code, notes, and snippets.

@gigawatts
Last active October 13, 2015 02:18
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save gigawatts/9f898e106e4e0f886084 to your computer and use it in GitHub Desktop.
Arduino PPM to MPH sketch
// PPM MPH readout for Futaba BTTF remote
// Using a Seeed Studio 4-Digit 7 segment display (Part Number: LED05291P)
#include "TM1637.h"
#define CLK 2 //pins definitions for TM1637 and can be changed to other ports
#define DIO 3
TM1637 tm1637(CLK,DIO);
#define chantotal 1 //How many channels on your radio?
#define chanthr 1 // Which channel should we listen to?
#define filter 10 // Glitch Filter
int channel[chantotal]; // readed Channel values
int lastReadChannel[chantotal]; //Last values readed
int counter=0; //couter
int ppmmin=700; // PPM min value for chanthr
int ppmmax=1650; //PPM max value for chanthr
int mph;
#define PIN_PPM 4 // PPM output from R/C Transmitter to arduino
void setup()
{
tm1637.init();
tm1637.set(BRIGHT_TYPICAL);//BRIGHT_TYPICAL = 2,BRIGHT_DARKEST = 0,BRIGHTEST = 7;
//Serial.begin(9600); //Serial Begin
pinMode(PIN_PPM, INPUT);
}
void loop()
{
if(pulseIn(PIN_PPM, HIGH) > 3000) //If pulse > 3000 useconds, continues
{
for(int i = 0; i <= chantotal-1; i++) // Read the pulses of the channels
{
channel[i]=pulseIn(PIN_PPM, HIGH);
}
for(int i = 0; i <= chantotal-1; i++) //Average the pulses
{
//If channel > max range, change value to the last pulse
if((channel[i] > 2000) || (channel[i] <100))
{
channel[i]= lastReadChannel[i];
}
else
{
//Average the last pulse eith the current pulse
channel[i]=(lastReadChannel[i]+channel[i])/2;
counter++; //increment counter
}
}
}
if(counter > filter) //If counter is > than filter, then prints values
{
for(int i = 0; i <= chantotal-1; i++) //Cycle to print values
{
lastReadChannel[i]=channel[i];
}
// if( channel[(chanthr - 1)] < ppmmin )
// {
// ppmmin = channel[(chanthr - 1)];
// }
//
// if( channel[(chanthr - 1)] > ppmmax )
// {
// ppmmax = channel[(chanthr - 1)];
// }
//PWM Fades an LED up and down with throttle
//analogWrite(PIN_LED, map(channel[(chanthr - 1)], ppmmin, ppmmax, 0, 255));
mph = map( channel[(chanthr - 1)], ppmmin, ppmmax, 0, 880 );
if (mph > 880)
{
mph = 880; // force max speed to 88.0 mph
tm1637.clearDisplay(); //then blink on and off while showing 88.0
delay(100);
}
if (mph < 0) mph = 0; // force MPH output to 0 so we dont go negative
// divide speed up into 3 digits to show on 7 segment LEDs
int digit0 = (mph / 100) % 10;
int digit1 = (mph % 100) / 10;
int digit2 = (mph % 10);
// Display each speed digit on the display
tm1637.display(0,digit0);
tm1637.display(1,digit1);
tm1637.display(2,digit2);
//tm1637.display(3,digit3); // Keep this 4th digit blank
// Some debug serial output
// Serial.print("LED: ");
// Serial.print(digit0);
// Serial.print(" ");
// Serial.print(digit1);
// Serial.print(" . ");
// Serial.println(digit2);
// Serial.print("Raw: ");
// Serial.print(channel[chanthr -1]);
//
// Serial.print(" Min: ");
// Serial.print(ppmmin);
//
// Serial.print(" Max: ");
// Serial.print(ppmmax);
//
// Serial.print(" MPH: ");
// //Serial.println( printf("%.1f", mph) );
// //Serial.println( mph );
// Serial.println( mph / 10.0 );
counter=0; // Restart couter.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment