Skip to content

Instantly share code, notes, and snippets.

@luigifcruz
Last active December 13, 2016 02:42
Show Gist options
  • Save luigifcruz/dfac0174203849ea4f24030c80bd0bae to your computer and use it in GitHub Desktop.
Save luigifcruz/dfac0174203849ea4f24030c80bd0bae to your computer and use it in GitHub Desktop.
#define sensorPin A0 // Hall Effect Sensor Pin
#define pwmPin 3 // Transisor Pin
int levitPoint = 690; // Levitation Point relative to the sensor readings
#define FILTER_SHIFT 4
int sensorValue = 0;
int deltaLevit = 15;
int maxL, minL;
int filter_input;
int filter_output;
long filter_reg;
byte induction = 128;
void setup() {
pinMode(pwmPin, OUTPUT);
// PWM Direct Controller (Note Below)
TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM20);
TCCR2B = _BV(CS20);
maxL = levitPoint - deltaLevit;
minL = levitPoint + deltaLevit;
}
void loop() {
filter_input = analogRead(sensorPin);
// Lowpass Filter from http://www.edn.com/
filter_reg = filter_reg - (filter_reg >> FILTER_SHIFT) + filter_input;
sensorValue = filter_reg >> FILTER_SHIFT;
if (sensorValue < levitPoint - 50) {
induction = 255
} else {
if (sensorValue > maxL) induction = 0;
if (sensorValue < minL) induction = 255;
if (sensorValue <= maxL and sensorValue >= minL) induction = ((sensorValue - maxL)/5);
}
OCR2B = induction;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment