Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions ArduinoFreevalve/ArduinoFreevalve.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// An Arduino program for controlling valve timing on a 6.5HP Predator engine from Harbor Freight.
//
// License goes here.
//
// Wesley Kagan, 2020
// Ric Allinson, 2020

// Pins assignment.
static const int HALL_MAGNET = 2;
static const int ROTATION_MAGNET = 3;
static const int EXHAUST_V = 12;
static const int INTAKE_V = 13;

// Implementation constants (to be changed by implementer).
static const int NUM_STEPS = 60;

// Control constants.
static const int STEPS_PER_ROTATION = NUM_STEPS - 1; // Number of interrupts in a half cycle for a zero based array.
static const int STEPS_PER_CYCLE = NUM_STEPS * 2; // Number of interrupts in the full cycle.

// Runtime mapping variables.
bool intakeMap[STEPS_PER_CYCLE] = {}; // Intake open/close mapping is equal the total number of interrupts for two rotations.
bool exhaustMap[STEPS_PER_CYCLE] = {}; // Exhaust open/close mapping is equal the total number of interrupts for two rotations.

// ISR variables.
volatile int hallCounter = 0; // The number of magnets after the last TDC.
volatile bool secondCycle = false; // "true" if the cam is on its second rotation.
volatile bool printLog = false; // Used to stop duplicate values from printing in the loop.

void setup() {
Serial.begin(115200);
pinMode(HALL_MAGNET, INPUT);
attachInterrupt(0, rotationDetect, RISING);
attachInterrupt(1, magnetDetect, RISING);
pinMode(INTAKE_V, OUTPUT);
pinMode(EXHAUST_V, OUTPUT);
/*
Load mappings here.
*/
// intakeMap = ?
// exhaustMap = ?
}

void loop() {
// This may not print the correct values. Only the most recent from the last magnetDetect() interrupt.
if(printLog){
Serial.print(hallCounter);
Serial.println(secondCycle);
printLog = false;
}
}

// This function is called once per rotation.
void rotationDetect() {
if (secondCycle) {
hallCounter = 0; // On the second cycle reset the hallCounter.
} else {
hallCounter = STEPS_PER_ROTATION; // Forcing the counter in case of drift over a rotation.
}
// Flip the secondCycle flag.
secondCycle = !secondCycle;
}

// This function is called NUM_STEPS per rotation.
// This function will be called about every ~263.16usec at the maximum RPM of 3800.
// (1 rev / 60 pulses) * (1 min / 3800 rev) * (60 sec / min) = 1 / 3800 sec/pulse = ~263.16usec.
void magnetDetect() {
// Increment the counter to keep track of the position.
hallCounter++;

// If the hallCounter is greater than the mapping index reset it.
// This would only happen when the rotationDetect() interrupt was NOT triggered.
if (hallCounter >= STEPS_PER_CYCLE) {
hallCounter = 0;
}

// Use the intake/exhaust maps to open or close the valves.
digitalWrite(INTAKE_V, intakeMap[hallCounter]);
digitalWrite(EXHAUST_V, exhaustMap[hallCounter]);

// Tells the loop() that the ISR variables have changed.
printLog = true;
}
71 changes: 0 additions & 71 deletions ArduinoHallEffect_good_code.ino

This file was deleted.

13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
# Arduino Freevalve

Repository for the Arduino Freevalve Project at www.youtube.com/c/wesleykagan

## Overview

This code is tested with the [Harbor Freight 6.5hp 212cc OHC Horizontal Shaft Gas Engine EPA&CARB](https://www.harborfreight.com/engines-generators/gasoline-engines/65-hp-212cc-ohv-horizontal-shaft-gas-engine-epacarb-69727.html)

## Original README

# Freevalve_Arduino

Codebase for the Arduino Freevalve Project- found here: www.youtube.com/c/wesleykagan

Okay, so this is a super early stage of this project, but as of this post, it does work in the
Expand All @@ -25,4 +36,4 @@ Website: www.wesleykagan.com

For promotional inquiries: [email protected]

USE AT YOUR OWN RISK!
USE AT YOUR OWN RISK!