/* ------------------------ ON BOARD GLOW PLUG DRIVER -------------------------------- It uses the Smoothing/Averaging code found in the Arduino Examples, written By David A. Mellis and modified 9 Apr 2012 by Tom Igoe http://www.arduino.cc/en/Tutorial/Smoothing */ // ---------------------------- USER CONFIGURATION ----------------------------------- // The glow driver will be turned ON between the minValue and the maxValue // In ersky9x/er9x the channels normally range between 988 and 2012 milliseconds. // Better to set the minimum value a bit under 988 to avoid the glow plug driver turning OFF when throttle is at idle #define minValue 980 //Insert the minimum Value in ms (milliseconds) #define maxValue 1300 //Insert the maximum value in ms (Milliseconds) // -------------------------- END OF USER CONFIGURATION ------------------------------- // Define the number of samples to keep track of. The higher the number, // the more the readings will be smoothed, but the slower the output will // respond to the input. Using a constant rather than a normal variable lets // use this value to determine the size of the readings array. const int numReadings = 20; // use this value to determine the size of the readings array. int readings[numReadings]; // the readings from the analog input int readIndex = 0; // the index of the current reading int total = 0; // the running total int average = 0; // the average int inputPin = 3; // Signal from the receiver connets to Digital Pin 3, which is pin 2 of the ATtiny int RX_Channel; int RX_Channel_ofs; int ledPin = 0; // LED connected to digital pin 0, Pin 5 of the ATtiny int analogPin = A2; // potentiometer connected to analog pin 2, Pin 3 of the ATtiny int val = 0; // variable to store the read value void setup() { // initialize serial communication with computer: // Serial not available on the ATtiny85 // Serial.begin(9600); pinMode(inputPin, INPUT_PULLUP); //Sets the pin as input and turns ON the internal Pullup resistor pinMode(ledPin, OUTPUT); // sets the pin as output for (int thisReading = 0; thisReading < numReadings; thisReading++) readings[thisReading] = 0; // initialize all the readings to 0: } void loop() { RX_Channel_ofs = RX_Channel + 8; RX_Channel = pulseIn(inputPin, HIGH, 25000); //Read The pulses received from the throttle Channel total = total - readings[readIndex]; // subtract the last reading: readings[readIndex] = RX_Channel_ofs; // Select the pulses for averaging total = total + readings[readIndex]; // add the reading to the total: readIndex = readIndex + 1; // advance to the next position in the array: if (readIndex >= numReadings) // if we're at the end of the array... readIndex = 0; // ...wrap around to the beginning: average = total / numReadings; // calculate the average: // Serial.println(average); // send it to the computer as ASCII digits (Serial not available on the ATtiny85) delay(10); // delay in between reads for stability if (average >= minValue && average <= maxValue) // Apply the min and max Channel values to the averaged pulses to determin the turn ON region { val = analogRead(analogPin); // read the analog input pin. Potentiometer pin to adjust PWM frequency analogWrite(ledPin, val / 16); // analogRead values go from 0 to 1023, analogWrite values from 0 to 64 } else { digitalWrite(ledPin, LOW); // If the Channel is not within the set values, turn the driver OFF } }