Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Constants
const int upLed = 13;
const int pendingLed = 12;
const long pendingLedInterval = 500;
const int spwPin = 5;
const long pulsePerMile = 4000; // For Honda Element
const int upSpeed = 40;
const long upTime = 60000; // 1 Minute
const int dnSpeed = 30;
const long dnTime = 30000; // 30 Seconds
const long mphFactor = 3600000 / pulsePerMile; // Figure what 1 mph is in pulses per hour
// Globals
unsigned long lastSpeedCheck = 0;
unsigned long lastPendingTime = 0;
boolean upStatus = false;
boolean pendingStatus = false;
long timeSincePending = 0;
unsigned long pendingLedLast = 0;
int pendingLedState = LOW;
void setup() {
Serial.begin(9600);
pinMode(upLed, OUTPUT);
pinMode(pendingLed, OUTPUT);
TCCR1A = 0; // reset timer/counter control
digitalWrite(upLed, LOW);
digitalWrite(pendingLed, LOW);
bitSet(TCCR1B, CS12);
bitSet(TCCR1B, CS11);
lastSpeedCheck = millis();
}
void loop() {
int currentSpeed = getSpeed();
if (currentSpeed < dnSpeed && upStatus) { // We are below the down threshold, and we're up
if (lastPendingTime == 0) {
lastPendingTime = millis(); // If we don't have a pending time, we just started pending
}
pendingStatus = true;
timeSincePending = millis() - lastPendingTime; // How long have we been pending?
if (timeSincePending > dnTime) { // If we've been pending long enough...
doDnWindow(); // Roll down the window
lastPendingTime = 0; // Clear the pending time
pendingStatus = false; // We're no longer pending
}
blinkPendingLed();
}
else if (currentSpeed > upSpeed && !upStatus) { // We are above the up threshold, and we're down
if (lastPendingTime == 0) {
lastPendingTime = millis(); // If we don't have a pending time, we just started pending
}
pendingStatus = true;
timeSincePending = millis() - lastPendingTime; // How long have we been pending?
if (timeSincePending > dnTime) { // If we've been pending long enough...
doUpWindow(); // Roll down the window
lastPendingTime = 0; // Clear the pending time
pendingStatus = false; // We're no longer pending
}
blinkPendingLed();
} else {
lastPendingTime = 0; // Clear the pending time
pendingStatus = false; // We're not pending
if (pendingLedState == HIGH) {
digitalWrite(pendingLed, LOW);
}
}
}
void blinkPendingLed() {
unsigned long currentMillis = millis(); // Grab the current time
if (currentMillis - pendingLedLast >= pendingLedInterval) { // Has it been long enough since we blinked?
pendingLedLast = currentMillis; // Update the time we last blinked
pendingLedState = pendingLedState == LOW ? HIGH : LOW; // Change the state
digitalWrite(pendingLed, pendingLedState);
}
}
void doUpWindow() {
digitalWrite(upLed, HIGH);
upStatus = true; // TODO: This will become I2C code to trigger things on the remote relay board
}
void doDnWindow() {
digitalWrite(upLed, LOW);
upStatus = false; // TODO: This will become I2C code to trigger things on the remote relay board
}
float getSpeed() {
TCCR1B = 0; // Stop counting (No Clock Source)
int pulseCount = TCNT1; // Get the count
int timePassed = millis() - lastSpeedCheck; // Get time passed
TCNT1 = 0; // Reset the Count
bitSet(TCCR1B, CS12); // Needed for following bits
bitSet(TCCR1B, CS11); // Book says rising edge, datasheet says falling edge
bitSet(TCCR1B, CS10); // Datasheet says this and previous for rising edge
lastSpeedCheck = millis(); // Update the time we last checked speed
Serial.write("Current Speed: ");
Serial.print(calculateMPH(pulseCount, timePassed);
return calculateMPH(pulseCount, timePassed);
}
float calculateMPH(int pulseCount, int timePassed) {
return pulseCount / timePassed * mphFactor; // ( (Pulse_Count / Millis_Passed) * 3600000) / Pulses_per_Mile = MPH
} // (3600000 / Pulses_per_Mile) = mphFactor Const
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.