From 9553b3a5e74b78fd7e6e3a8443bb229e3e21ef1f Mon Sep 17 00:00:00 2001 From: Ric Allinson Date: Mon, 14 Dec 2020 12:18:36 -0800 Subject: [PATCH 1/5] spacing cleanup --- ArduinoHallEffect_good_code.ino | 76 ++++++++++++++++----------------- 1 file changed, 37 insertions(+), 39 deletions(-) diff --git a/ArduinoHallEffect_good_code.ino b/ArduinoHallEffect_good_code.ino index 998dabe..34e3d2c 100644 --- a/ArduinoHallEffect_good_code.ino +++ b/ArduinoHallEffect_good_code.ino @@ -1,5 +1,5 @@ //This code is full of test strings and is generally a conglomerate of code from stackexchange and -//bad form. If it looks stupid, I probably wrote it. +//bad form. If it looks stupid, I probably wrote it. //Wesley Kagan, 2020 const int hall = 2; @@ -14,7 +14,6 @@ unsigned int degree; unsigned int state; int newstate; - void setup() { pinMode(hall, INPUT); Serial.begin(115200); @@ -24,48 +23,47 @@ void setup() { degree = 0; } - void loop() - { +void loop() { //The compiler says this is needed otherwise the govt. takes my cat - } +} void magnet_detect() { - hallcounter ++; - triggerTime = millis(); - timeGap = triggerTime - last_triggerTime; - last_triggerTime = triggerTime; + hallcounter ++; + triggerTime = millis(); + timeGap = triggerTime - last_triggerTime; + last_triggerTime = triggerTime; - if (timeGap >= last_timeGap + last_timeGap / 2) - { - //Serial.println(state); - Serial.println("missing tooth"); - hallcounter = 1; - state++; //This right here is garbage and you know it. - } + if (timeGap >= last_timeGap + last_timeGap / 2) { + //Serial.println(state); + Serial.println("missing tooth"); + hallcounter = 1; + state++; //This right here is garbage and you know it. + } - last_timeGap = timeGap; - //Serial.println(hallcounter); + last_timeGap = timeGap; + //Serial.println(hallcounter); - degree = hallcounter * 6; - if ((state % 2) == 0) { //Bool wasn't working so this dumpster fire got started. - Serial.print("STATE 1 "); - Serial.println(degree); - if (6 <= degree && degree <= 180) { //EDIT HERE INTAKE - digitalWrite(13, HIGH); // sets the digital pin 13 on - } - else { - digitalWrite(13, LOW); // sets the digital pin 13 off - } - } - if ((state % 2) == 1) { - Serial.print("STATE 2 "); - Serial.println(degree); - if (180 <= degree && degree <= 354) { //EDIT HERE EXHAUST - digitalWrite(12, HIGH); // sets the digital pin 13 on - } - else { - digitalWrite(12, LOW); // sets the digital pin 13 off - } - } + degree = hallcounter * 6; + + if ((state % 2) == 0) { //Bool wasn't working so this dumpster fire got started. + Serial.print("STATE 1 "); + Serial.println(degree); + if (6 <= degree && degree <= 180) { //EDIT HERE INTAKE + digitalWrite(13, HIGH); // sets the digital pin 13 on + } else { + digitalWrite(13, LOW); // sets the digital pin 13 off + } + } + + if ((state % 2) == 1) { + Serial.print("STATE 2 "); + Serial.println(degree); + if (180 <= degree && degree <= 354) { //EDIT HERE EXHAUST + digitalWrite(12, HIGH); // sets the digital pin 13 on + } else { + digitalWrite(12, LOW); // sets the digital pin 13 off + } + } + lasthallstate = hallstate; } From 85035e3eb9d89b574500a86cf38fb8b507e1c762 Mon Sep 17 00:00:00 2001 From: Ric Allinson Date: Mon, 14 Dec 2020 13:25:42 -0800 Subject: [PATCH 2/5] First pass at rewite. --- ArduinoHallEffect_good_code.ino | 99 +++++++++++++++------------------ 1 file changed, 44 insertions(+), 55 deletions(-) diff --git a/ArduinoHallEffect_good_code.ino b/ArduinoHallEffect_good_code.ino index 34e3d2c..f949213 100644 --- a/ArduinoHallEffect_good_code.ino +++ b/ArduinoHallEffect_good_code.ino @@ -1,69 +1,58 @@ -//This code is full of test strings and is generally a conglomerate of code from stackexchange and -//bad form. If it looks stupid, I probably wrote it. -//Wesley Kagan, 2020 +// This code is full of test strings and is generally a conglomerate of code from StackExchange and +// bad form. If it looks stupid, I probably wrote it. +// Wesley Kagan, 2020 +// Ric Allinson, 2020 -const int hall = 2; -int hallcounter = 0; -int hallstate = 0; -int lasthallstate = 0; -unsigned long triggerTime; -unsigned long last_triggerTime; -unsigned long timeGap; -unsigned long last_timeGap; -unsigned int degree; -unsigned int state; -int newstate; +// Control variables. +const int MAGNET_PER_DEG = 6 +const int FREEVALVE_ANGLE = 180; + +// Pins assignment. +const int HALL_MAGNET = 2; +const int EXHAUST_V = 12; +const int INTAKE_V = 13; + +int cad; // Crank Angle Degrees (CAD). +bool cycle; // "true" for Intake, "false" for Exhaust. +unsigned long timeGap; // Function level time between interrupts. +unsigned long lastTimeGap; // Global level time between interrupts. void setup() { - pinMode(hall, INPUT); Serial.begin(115200); - pinMode(13, OUTPUT); // sets the digital pin 13 as output - pinMode(12, OUTPUT); // sets the digital pin 12 as output - attachInterrupt(0, magnet_detect, RISING);//Initialize the intterrupt pin digital pin 2 - degree = 0; + pinMode(HALL_MAGNET, INPUT); + attachInterrupt(0, magnetDetect, RISING); + pinMode(INTAKE_V, OUTPUT); + pinMode(EXHAUST_V, OUTPUT); } void loop() { - //The compiler says this is needed otherwise the govt. takes my cat + if(printLog){ + Serial.print(cad); + printLog = false; + } } -void magnet_detect() { - hallcounter ++; - triggerTime = millis(); - timeGap = triggerTime - last_triggerTime; - last_triggerTime = triggerTime; - - if (timeGap >= last_timeGap + last_timeGap / 2) { - //Serial.println(state); - Serial.println("missing tooth"); - hallcounter = 1; - state++; //This right here is garbage and you know it. - } - - last_timeGap = timeGap; - //Serial.println(hallcounter); - - degree = hallcounter * 6; +void magnetDetect() { + hallCounter++; + timeGap = millis() - lastTimeGap; - if ((state % 2) == 0) { //Bool wasn't working so this dumpster fire got started. - Serial.print("STATE 1 "); - Serial.println(degree); - if (6 <= degree && degree <= 180) { //EDIT HERE INTAKE - digitalWrite(13, HIGH); // sets the digital pin 13 on - } else { - digitalWrite(13, LOW); // sets the digital pin 13 off - } + // Find missing tooth for dead top. + if (timeGap >= lastTimeGap * 3 / 2) { + hallCounter = 1; + cycle = !cycle; } - if ((state % 2) == 1) { - Serial.print("STATE 2 "); - Serial.println(degree); - if (180 <= degree && degree <= 354) { //EDIT HERE EXHAUST - digitalWrite(12, HIGH); // sets the digital pin 13 on - } else { - digitalWrite(12, LOW); // sets the digital pin 13 off - } - } + // Store the last time difference so we can use it in the next cycle. + lastTimeGap = timeGap; + + + cad = hallCounter * MAGNET_PER_DEG; - lasthallstate = hallstate; + // Every rotation of the crank alternates between Intake and Exhaust. + if (cycle) { + digitalWrite(INTAKE_V, cad <= FREEVALVE_ANGLE); + } else { + digitalWrite(EXHAUST_V, FREEVALVE_ANGLE <= cad); + } + printLog = true; } From 9ec6aa48631e6a6efa2fbe0683feec34cde835ee Mon Sep 17 00:00:00 2001 From: Ric Allinson Date: Mon, 14 Dec 2020 13:46:54 -0800 Subject: [PATCH 3/5] Checked in Arduino IDE --- .../ArduinoHallEffect.ino | 126 ++++++++++-------- 1 file changed, 68 insertions(+), 58 deletions(-) rename ArduinoHallEffect_good_code.ino => ArduinoHallEffect/ArduinoHallEffect.ino (67%) diff --git a/ArduinoHallEffect_good_code.ino b/ArduinoHallEffect/ArduinoHallEffect.ino similarity index 67% rename from ArduinoHallEffect_good_code.ino rename to ArduinoHallEffect/ArduinoHallEffect.ino index f949213..3c886ea 100644 --- a/ArduinoHallEffect_good_code.ino +++ b/ArduinoHallEffect/ArduinoHallEffect.ino @@ -1,58 +1,68 @@ -// This code is full of test strings and is generally a conglomerate of code from StackExchange and -// bad form. If it looks stupid, I probably wrote it. -// Wesley Kagan, 2020 -// Ric Allinson, 2020 - -// Control variables. -const int MAGNET_PER_DEG = 6 -const int FREEVALVE_ANGLE = 180; - -// Pins assignment. -const int HALL_MAGNET = 2; -const int EXHAUST_V = 12; -const int INTAKE_V = 13; - -int cad; // Crank Angle Degrees (CAD). -bool cycle; // "true" for Intake, "false" for Exhaust. -unsigned long timeGap; // Function level time between interrupts. -unsigned long lastTimeGap; // Global level time between interrupts. - -void setup() { - Serial.begin(115200); - pinMode(HALL_MAGNET, INPUT); - attachInterrupt(0, magnetDetect, RISING); - pinMode(INTAKE_V, OUTPUT); - pinMode(EXHAUST_V, OUTPUT); -} - -void loop() { - if(printLog){ - Serial.print(cad); - printLog = false; - } -} - -void magnetDetect() { - hallCounter++; - timeGap = millis() - lastTimeGap; - - // Find missing tooth for dead top. - if (timeGap >= lastTimeGap * 3 / 2) { - hallCounter = 1; - cycle = !cycle; - } - - // Store the last time difference so we can use it in the next cycle. - lastTimeGap = timeGap; - - - cad = hallCounter * MAGNET_PER_DEG; - - // Every rotation of the crank alternates between Intake and Exhaust. - if (cycle) { - digitalWrite(INTAKE_V, cad <= FREEVALVE_ANGLE); - } else { - digitalWrite(EXHAUST_V, FREEVALVE_ANGLE <= cad); - } - printLog = true; -} +// This code is full of test strings and is generally a conglomerate of code from StackExchange and +// bad form. If it looks stupid, I probably wrote it. +// Wesley Kagan, 2020 +// Ric Allinson, 2020 + +// Control variables. +const int MAGNET_PER_DEG = 6; +const int FREEVALVE_ANGLE = 180; + +// Pins assignment. +const int HALL_MAGNET = 2; +const int EXHAUST_V = 12; +const int INTAKE_V = 13; + +int cad; // Crank Angle Degrees (CAD). +int hallCounter; // The number of magnets after the last TDC. +bool cycle; // "true" for Intake, "false" for Exhaust. +bool printLog; // Used to trigger a print in the loop. +unsigned long timeGap; // Function level time between interrupts. +unsigned long lastTimeGap; // Global level time between interrupts. + +void setup() { + Serial.begin(115200); + pinMode(HALL_MAGNET, INPUT); + attachInterrupt(0, magnetDetect, RISING); + pinMode(INTAKE_V, OUTPUT); + pinMode(EXHAUST_V, OUTPUT); +} + +void loop() { + if(printLog){ + Serial.print(lastTimeGap); + Serial.print(cycle); + Serial.print(cad); + printLog = false; + } +} + +void magnetDetect() { + // Increment the counter to keep track of the position. + hallCounter++; + // Get the time gap between this interrupt and the last. + timeGap = millis() - lastTimeGap; + + // Find missing tooth for Top Dead Center (TDC). + if (timeGap >= lastTimeGap * 3 / 2) { + // Reset the hall counter. + hallCounter = 1; + // Flip the cycle phase. + cycle = !cycle; + } + + // Store the last time difference so we can use it in the next interrupt. + lastTimeGap = timeGap; + + // Store the current crank angle for logging. + cad = hallCounter * MAGNET_PER_DEG; + + // Every rotation of the crank alternates between Intake and Exhaust. + if (cycle) { + // If the crank angle degree is in the intake range open it, otherwise close it. + digitalWrite(INTAKE_V, cad <= FREEVALVE_ANGLE); + } else { + // If the crank angle degree is in the exhaust range open it, otherwise close it. + digitalWrite(EXHAUST_V, FREEVALVE_ANGLE <= cad); + } + printLog = true; +} From 0febcb417a4b848c7726e44dccf6ab30b0fd758f Mon Sep 17 00:00:00 2001 From: Ric Allinson Date: Mon, 14 Dec 2020 15:47:09 -0800 Subject: [PATCH 4/5] Change value offset to allow different top and bottom values. --- ArduinoHallEffect/ArduinoHallEffect.ino | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/ArduinoHallEffect/ArduinoHallEffect.ino b/ArduinoHallEffect/ArduinoHallEffect.ino index 3c886ea..451b6c4 100644 --- a/ArduinoHallEffect/ArduinoHallEffect.ino +++ b/ArduinoHallEffect/ArduinoHallEffect.ino @@ -1,11 +1,12 @@ -// This code is full of test strings and is generally a conglomerate of code from StackExchange and -// bad form. If it looks stupid, I probably wrote it. +// An Arduino program for controlling value timing on a 6.5ph Predator engine from Harbor Freight. +// // Wesley Kagan, 2020 // Ric Allinson, 2020 // Control variables. -const int MAGNET_PER_DEG = 6; -const int FREEVALVE_ANGLE = 180; +const int DEG_PER_MAGNET = 6; // Number of degrees for each magnet. +const int FREEVALVE_OFFSET_TOP = 0; // The amount of off set from TDC (needs to be multiples of DEG_PER_MAGNET). +const int FREEVALVE_OFFSET_BOTTOM = 180 + FREEVALVE_OFFSET_TOP; // Pins assignment. const int HALL_MAGNET = 2; @@ -36,6 +37,7 @@ void loop() { } } +// This function will be called about every 15.79ms at the maximum RPM of 3800. void magnetDetect() { // Increment the counter to keep track of the position. hallCounter++; @@ -54,15 +56,15 @@ void magnetDetect() { lastTimeGap = timeGap; // Store the current crank angle for logging. - cad = hallCounter * MAGNET_PER_DEG; + cad = hallCounter * DEG_PER_MAGNET; // Every rotation of the crank alternates between Intake and Exhaust. if (cycle) { // If the crank angle degree is in the intake range open it, otherwise close it. - digitalWrite(INTAKE_V, cad <= FREEVALVE_ANGLE); + digitalWrite(INTAKE_V, cad > FREEVALVE_OFFSET_TOP && cad <= FREEVALVE_OFFSET_BOTTOM); } else { // If the crank angle degree is in the exhaust range open it, otherwise close it. - digitalWrite(EXHAUST_V, FREEVALVE_ANGLE <= cad); + digitalWrite(EXHAUST_V, cad >= FREEVALVE_OFFSET_BOTTOM || cad < FREEVALVE_OFFSET_TOP); } printLog = true; } From 75e7cb6267b11aa0ace999b05f47808b3cce6f5f Mon Sep 17 00:00:00 2001 From: Ric Allinson Date: Mon, 14 Dec 2020 16:00:00 -0800 Subject: [PATCH 5/5] Fixed typo --- ArduinoHallEffect/ArduinoHallEffect.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ArduinoHallEffect/ArduinoHallEffect.ino b/ArduinoHallEffect/ArduinoHallEffect.ino index 451b6c4..504716d 100644 --- a/ArduinoHallEffect/ArduinoHallEffect.ino +++ b/ArduinoHallEffect/ArduinoHallEffect.ino @@ -1,4 +1,4 @@ -// An Arduino program for controlling value timing on a 6.5ph Predator engine from Harbor Freight. +// An Arduino program for controlling valve timing on a 6.5ph Predator engine from Harbor Freight. // // Wesley Kagan, 2020 // Ric Allinson, 2020