Skip to content

Instantly share code, notes, and snippets.

@giljr
Last active September 14, 2018 01:18
Show Gist options
  • Save giljr/3783188087b9b53e797566ed3f9fd7f4 to your computer and use it in GitHub Desktop.
Save giljr/3783188087b9b53e797566ed3f9fd7f4 to your computer and use it in GitHub Desktop.
Quick Fun Speed Lab Closed Loop Motion Control — Ardu_Serie#44 https://medium.com/jungletronics/quick-fun-speed-lab-39b0cbc53303
/*
Project name: Quick Fun Speed Lab - Ardu_Serie#44
Simpler experiment -  Closed Loop Motion Control - Small Fan
Flavour I - tachometer: Code From the Book Arduino Internals (file size: ???? bytes)
Hex File: _44_Tachometer_Reading_Ticks_01.ino
*/ // Fun's Connections definition
// In case using bare Arduino: Connect the fun's terminal
// on pins 5, 6 and 7 (SGN to pin 5)
//#define VCC 6 // VCC - FUN'S RED WIRE - POWER SUPPLY BUS
#define SGN 5 // SIGNAL - FUN'S YELLOW WIRE - TACHOMETER
//#define GND 7 // VCC - FUN'S BLACK WIRE - POWER SUPPLY BUS
unsigned int fan_odometer = 0; // count of fan tachometer ticks
volatile byte update_flag = 0; // used to signal update of odometer reading
ISR(TIMER2_COMPA_vect) {
static byte prescaler;
if (prescaler) {
prescaler--;
} else {
prescaler = 125; // reset prescaler
fan_odometer = TCNT1; // capture odometer reading
update_flag = 1; // set flag
}
}
void setup() {
//pinMode(VCC, OUTPUT); // VCC source current; On Power bus the speed increases up 740 RPM
//pinMode(GND, OUTPUT); // GND ground reference; On Pin's set to VCC/GND the speed is 450 RPM
//digitalWrite(VCC, HIGH); // VCC - Power Supply
digitalWrite(SGN, HIGH); // SIGNAL - tachometer -enable pullup resistor for D5/PD5/T1 for tachometer input
//digitalWrite(GND, LOW); // GND - Power Supply
// timer/counter1 counts fan tachometer pulses
// on rising edge of T1, 2 per revolution
TCCR1A = 0 << WGM11 | 0 << WGM10;
TCCR1B = 0 << WGM13 | 0 << WGM12 | 1 << CS12 | 1 << CS11 | 1 << CS10;
// timer/counter2 provides 125 interrupts per second
TCCR2A = 1 << WGM21 | 0 << WGM20;
TCCR2B = 0 << WGM22 | 1 << CS22 | 1 << CS21 | 1 << CS20;
OCR2A = 124; // n-1
TIMSK2 = 1 << OCIE2A; // enable compare match interrupt
Serial.begin(9600);
Serial.println("Fan Speed in RPM");
}
void loop() {
static unsigned int previous_fan_odometer = 0;
while (update_flag == 0); // wait for update to occur
update_flag = 0; // reset update flag
Serial.println(((fan_odometer - previous_fan_odometer) * 60) / 2); // in RPM
previous_fan_odometer = fan_odometer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment