14
In this issue: 1. Arduino Tachometer Stroboscopes are often seen in nightclubs as part of a lightshow. Flashing a light repetitively can produce some cool effects on the dance floor but they can also be used to measure rotational speed. An Arduino can be used to control the flash rate of an LED and turn it into a versatile tachometer. Find out how in this Arduino Tachometer project. Home DIY Electronics HDE Issue #4 Arduino Tachometer The Arduino is a microcontroller board based on an open source design. The hardware is low cost and available from a number of sources. It’s perfect for your electronics projects. Click here to see the range of inexpensive Arduino hardware. What could go wrong?” – Ever heard that? Whenever I hear an engineer claim that their system is foolproof I prepare for disaster. I know that they haven’t thought everything through. Expect the unexpected. It will happen and it will happen to you so be prepared. Welcome to HDE the Magazine from Home DIY Electronics. Hi, I’m Steve. I’m an engineer and I’ve been specialising in embedded software for over 40 years. In this issue I show you how you can build an Arduino driven LED strobe to check the spin speed of fans, motors and other rotating objects. Follow along with my projects as I build them. I’m going to release them here before I put them on the web site so - be sure that you get your free subscription so you won’t miss an issue. YOUR TITLE HERE Home Diy Electronics is a free magazine for people like you who build electronics projects. Make sure that you are on my mailing list so that you never miss an issue. Click here to subscribe to the HDE list. Click here to contact Steve. Thoughts Solderless Breadboards for DIY Electronics Projects April 17th 2014

HDE Issue #4 April 17th 2014 Arduino Tachometerhomediyelectronics.com/ls/vuU6EWq7uR79ayx5Eumt/hde_4.pdf · Using the tachometer Once you have built the hardware and loaded the software

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: HDE Issue #4 April 17th 2014 Arduino Tachometerhomediyelectronics.com/ls/vuU6EWq7uR79ayx5Eumt/hde_4.pdf · Using the tachometer Once you have built the hardware and loaded the software

In this issue:

1. Arduino Tachometer

Stroboscopes are often seen in nightclubs as part of a lightshow. Flashing a light repetitively can produce some cool effects on the dance floor but they can also be used to measure rotational speed. An Arduino can be used to control the flash rate of an LED and turn it into a versatile tachometer. Find out how in this Arduino Tachometer project.

Home DIY Electronics HDE Issue #4

Arduino Tachometer

The Arduino is a microcontroller board

based on an open

source design. The hardware is low cost

and available from a number of sources. It’s

perfect for your electronics projects.

Click here to see the

range of inexpensive Arduino hardware.

“What could go

wrong?” – Ever heard that?

Whenever I hear an

engineer claim that their system is

foolproof I prepare for disaster.

I know that they

haven’t thought everything through.

Expect the unexpected.

It will happen and it

will happen to you so be prepared.

Welcome to

HDE the Magazine from Home DIY

Electronics.

Hi, I’m Steve. I’m an engineer and I’ve been specialising in embedded software for over 40 years. In this issue I show you how you can build an Arduino driven LED strobe to check the spin speed of fans, motors and other rotating objects.

Follow along with my projects as I build them. I’m going to release them here before I put them on the web site so -

be sure that you get

your free subscription so you won’t miss an issue.

YOUR TITLE HERE Home Diy Electronics is a free magazine for people like you who build electronics projects. Make sure that you are on my mailing list so that you never miss an issue. Click here to subscribe to the HDE list. Click here to contact Steve.

Thoughts

Solderless Breadboards for DIY Electronics Projects

April 17th 2014

Page 2: HDE Issue #4 April 17th 2014 Arduino Tachometerhomediyelectronics.com/ls/vuU6EWq7uR79ayx5Eumt/hde_4.pdf · Using the tachometer Once you have built the hardware and loaded the software

Be the first to see future issues of HDE Magazine by subscribing for free Copyright © homediyelectronics.com

Page 2 of 14

Arduino Tachometer

So I’ve decided to present it as a project in this issue rather than make you wait for the mark 2. It’s been too long since the last issue as it is.

Stroboscopic rpm meter The basic idea is to build a calibrated stroboscope similar to one you might see in a night club but more controllable and measurable. So how can you measure rpm with a flashing light? Well, if you direct short pulses of light towards a rotating object and synchronise the pulse rate with the rotational speed then you can create the illusion that the object is stationary. You can use this illusion to measure the rotational speed.

That’s when I decided that I needed to build a tachometer to give the machine a proper test. Tachometers are often designed to count the interruptions in a light beam made by the vanes of a fan or rotor. This method obviously wasn’t going to work with the washing machine so I decided to use a non-invasive stroboscopic method instead. As it turns out the glass door of the washing machine reflects a lot of the light from the LEDs of my first prototype which makes it harder to see into the drum. I will need to develop a mark 2 with more super bright LEDs to make this a practical reality. The prototype is however more than good enough to measure the speed of fans and motors.

An excellent beginner’s guide to

Arduino Programming

There are a few problems that must be overcome when designing a strobe tachometer. The duration of the pulses of light must be short enough to illuminate the object only while it travels a short distance. This is required to minimize the amount of blurring on the stationary illusion. The average intensity of the light must be sufficient to adequately illuminate the object so that we can actually see it. This requirement means that we cannot make the pulse duration as short as we would like to and therefore it forces us to compromise. Traditionally stroboscopic tachometers have been designed using discharge tubes similar to those used in photography. These devices provide high intensity light for very short durations. Unfortunately they are a little difficult to drive at high repetition rates.

We just got ourselves a new washing machine. Our old one bled out and we were unable to save it. The replacement is an exciting new toy that claims to spin at 1400 rpm. That’s 200 rpm faster than the old one. But how do I know? How can I check that it spins as fast as it says it does?

Page 3: HDE Issue #4 April 17th 2014 Arduino Tachometerhomediyelectronics.com/ls/vuU6EWq7uR79ayx5Eumt/hde_4.pdf · Using the tachometer Once you have built the hardware and loaded the software

Be the first to see future issues of HDE Magazine by subscribing for free Copyright © homediyelectronics.com

Page 3 of 14

The alternative is to use high brightness LEDs instead of the flash tube. They are easier to drive but not as bright. We can compensate for this by using more than one LED to provide a relatively simple solution.

The strobing pulse is generated by an Arduino Uno from pin 12. This generates output levels of 0 – 5V. The LED drivers are powered from a 9V battery which allows two LEDs to be driven from a single NPN transistor. I used BC108 transistors but just about any general purpose transistor capable of switching more than 60mA will do.

The LEDs that I used have a forward voltage around 3V and the transistors drop about 200mV when conducting. This leaves 2.8V from the 9V supply to be dropped by the LED feed resistor.

I wanted to drive the LEDs as hard as I could to make the perceived light output as high as possible. To make the strobe work as a tachometer the LED can be on for only a small fraction of the time so maximising output is important. For continuous use the LEDs I used would be driven with a maximum of around 20mA but as the average current will be much lower than this I pushed the current pulses up towards 40mA using 68Ω resistors. Using the formula R = V / I

2.8/0.04 = 70 (68Ω standard value) My transistors have a gain of around 100 which is common for general purpose bipolar junction transistors. This means that to conduct 40mA between collector and emitter I needed to drive at least 0.4mA into the base. The output from the Arduino is 5V when high and the base of a BJT will sit 0.7V above it’s emitter which leaves 4.3V across the 1.2kΩ base resistor. This allows 3.6mA to flow into the base. More than enough current to saturate the transistor when on.

The first and best guide to C

programming

Page 4: HDE Issue #4 April 17th 2014 Arduino Tachometerhomediyelectronics.com/ls/vuU6EWq7uR79ayx5Eumt/hde_4.pdf · Using the tachometer Once you have built the hardware and loaded the software

Be the first to see future issues of HDE Magazine by subscribing for free Copyright © homediyelectronics.com

Page 4 of 14

I used 3 copies of this circuit in my prototype hoping that it would provide enough light. I could have used a little more when shining it into my washing machine but hey, that’s what prototypes are for. All 3 circuits are driven from the same Arduino pin and you could add more copies to make it brighter. If you do, be careful not to exceed the maximum 40mA that the Arduino is capable of delivering from a single pin. If you are close to these limits then switch some copies of the circuit onto a second output pin.

Controlling the Strobe parameters I have an inexpensive 16 x 2 line LCD display/keypad shield attached to my Arduino Uno so my first thought was to use the 6 key mini keypad to adjust the rpm and other settings of the strobe.

The compromise was to use the pot to quickly set the rate approximately and then switch over to the keypad to fine tune the speed reading. I used the Select key to toggle between the 2 modes.

Everything that you could ever want to

know about Arduino

The analog inputs digitize the input to 10 bits giving values from 0 – 1023. By adding an offset and multiplying this value by 4 I was able to control the rate to over 4000 rpm in steps of 4 rpm. This is a reasonable resolution for this application. The problem with using a variable resistor like this is inherent in all analog devices. The reading jitters one or two bits up and down. Every time the reading changed the strobe rate would jump. This made it difficult to fine tune the rpm reading. Eventually I settled on a compromise between the fast setting ability of the pot and the fine resolution and stability of the keypad approach.

Arduino Uno

Strobe pulse

-ve

12

Gnd

+5v

Gnd

A1 470kΩ

I set the up and down buttons to adjust the strobe rate and the left and right buttons to tweak the pulse duty cycle. This did as it was intended of course but I found it too difficult to use in practice.

It was taking too long to adjust the parameters making it very difficult to get the strobe rate synchronized with the rotational speed of a fan. So I decided to add a potentiometer to adjust the strobe rate. This was very easy to do given the Arduino’s analog input capability. The pot is connected between ground and +5V and the wiper of the pot connected to analog input A1.

Page 5: HDE Issue #4 April 17th 2014 Arduino Tachometerhomediyelectronics.com/ls/vuU6EWq7uR79ayx5Eumt/hde_4.pdf · Using the tachometer Once you have built the hardware and loaded the software

Be the first to see future issues of HDE Magazine by subscribing for free Copyright © homediyelectronics.com

Page 5 of 14

This worked very well giving the best of both methods. It is easy to set the rpm strobe rate approximately then switch to keypad mode to stabilize and make fine adjustments.

How to measure rpm So how can you use the Arduino Strobe Tachometer to measure the rotational speed of a fan? The idea is simple. You shine the flashing light at the moving object and adjust the rate of flashing until the object appears to be stationary. When this happens the rate of flashing is the same as the rotational speed of the object.

It isn’t strictly necessary but you can paint a light stripe on the moving object to make it easier to pick out under the strobe. This is particularly useful when ambient conditions are bright or when the surface of the object is smooth. Start with the strobe flashing at the maximum rate and slowly reduce it until the object appears stationary. If you see more fan blades than there actually are or multiple versions of your paint mark then your strobe is flashing multiple times the speed of the object. Reduce the strobe rate until you only see one paint mark or the correct number of fan blades. When this happens your strobe is flashing at the same speed as your object.

If you reduce the strobe speed further, say to half the speed of the object then you will again see one stationary object but this time it will appear more blurred due to the extended flash pulse time.

Page 6: HDE Issue #4 April 17th 2014 Arduino Tachometerhomediyelectronics.com/ls/vuU6EWq7uR79ayx5Eumt/hde_4.pdf · Using the tachometer Once you have built the hardware and loaded the software

Be the first to see future issues of HDE Magazine by subscribing for free Copyright © homediyelectronics.com

Page 6 of 14

Using the tachometer Once you have built the hardware and loaded the software sketch provided later in this issue you should have a working optical tachometer that is easy to use. The display shows you the following:

1. Pulse rate in rpm. Top line. 2. Duty cycle in %. Bottom right. 3. Display/keypad mode. Bottom Left.

The mode indicates potentiometer control (P) or keypad control (K).

Potentiometer control This is the default mode. Set the rpm by adjusting the potentiometer. Click the Select button to switch to keypad control mode.

Keypad control First set the rpm somewhere close to the speed of the rotating part you are trying to measure. Click the Select button to use the keypad then use the Up/Down buttons to fine tune the rpm rate. If you hold either of the buttons down they will auto-repeat.

Adjusting duty cycle When you are in keypad control mode you can also use the Left and Right buttons to adjust the duty cycle of the LED pulse. The pulse of light needs to be long enough for you to see it but not so long that the object looks blurred when the rate is in sync with the object. The default is set to 1% of the pulse repetition rate. You should experiment with this setting.

Software Just like many other of my Arduino projects I have made use of the on-board timers to generate appropriate timings. So before you download the code later in this issue you must first obtain the TimerOne Software Library from here: http://code.google.com/p/arduino-timerone/downloads/detail?name=TimerOne-v9.zip&can=2&q= and install it using the guides found here: http://arduino.cc/en/Guide/Libraries Don’t worry it’s quite simple to add the library to your Arduino software. The software sketch that controls the optical tachometer is pretty straightforward. You can see the code at the end of this issue and you can download the latest version from the web site. The code is well commented so it should be easy to follow. I will mention a few of the design aspects here.

User Interface The user is able to adjust the repetition rate and duty cycle using a very simple 2-mode display and keypad interface. In mode 0 the rate is set by the external potentiometer. Mode 1 allows the keypad to be used to fine tune the rate and duty cycle.

Interrupt based pulse timing A 50uS interrupt is generated using timer 1 and used to generate both the time between pulses and the length of the pulse itself. If you are not familiar with using interrupts on the Arduino then you might want to read this article: Arduino Timer Interrupt Example The strobe pulse is generated by the pulse_manager() function. It’s a simple 2 state finite state machine implemented with a switch() statement. In mode 0 the pulse is high and in mode 1 it is low. The two time counters are decremented in the appropriate state and action taken when the counts get to zero.

Now build it and play with it.

Page 7: HDE Issue #4 April 17th 2014 Arduino Tachometerhomediyelectronics.com/ls/vuU6EWq7uR79ayx5Eumt/hde_4.pdf · Using the tachometer Once you have built the hardware and loaded the software

Be the first to see future issues of HDE Magazine by subscribing for free Copyright © homediyelectronics.com

Page 7 of 14

/*******************************************************

Arduino stroboscopic tachometer RPM meter

strobe_1

Arduino Uno + 2 x 16 LCD display

14th April 2014

********************************************************/

#include <LiquidCrystal.h> // LCD module

#include <TimerOne.h> // Header file for TimerOne library

LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // select the pins used on the LCD panel

// define some values used by the panel and buttons

#define led1Pin 12 // Pin 12 LED 1

// Define constants for button presses

#define btnRIGHT 0 // Right

#define btnUP 1 // Up

#define btnDOWN 2 // Down

#define btnLEFT 3 // Left

#define btnSELECT 4 // Select

#define btnNONE 5 // None. No button pressed

#define TIMER_US 50 // 50 uS timer tick duration

volatile int pulse_percent = 1; // Pulse as percentage of repetition duration

volatile int pulse_counts; // Ticks in each pulse

volatile int pulse_time_count = 0; // Pulse tick counter

volatile int repetition_counts; // Ticks between strobe pulses

volatile int rpm_demand; // Entered speed demand in RPM

volatile int repetition_time_count = 0; // Pulse repetition tick counter

int display_mode = 0; // Determine what the display does

// --------------------------

// setup()

// Initialize i/o pins.

// Initialize the timer and attach interrupt.

// Initialize the LCD.

// Start initial display mode.

// --------------------------

void setup()

int i, zone_diff;

pinMode(led1Pin, OUTPUT); // LED 1 pin set to output

Timer1.initialize(TIMER_US); // Initialise timer 1

Timer1.attachInterrupt(timerIsr); // Attach interrupt to the timer service routine

lcd.begin(16, 2); // start the LCD library

setup_display_rpm_mode(); // Initialise display mode

set_rpm_demand(); // Initialise pulse variables

clear_display();

/* Debugging output to serial monitor port

Serial.begin(9600);

******************** */

Arduino Optical Tachometer Sketch

Download the latest Arduino Strobe Tachometer Software Sketch

Page 8: HDE Issue #4 April 17th 2014 Arduino Tachometerhomediyelectronics.com/ls/vuU6EWq7uR79ayx5Eumt/hde_4.pdf · Using the tachometer Once you have built the hardware and loaded the software

Be the first to see future issues of HDE Magazine by subscribing for free Copyright © homediyelectronics.com

Page 8 of 14

// --------------------------

// set_rpm_demand()

//

// Compute the pulse and repetition parameters.

// --------------------------

int set_rpm_demand()

repetition_counts = get_repetition_counts(); // Compute ticks per repetition

repetition_time_count = repetition_counts; // Load repetition timer ticks

pulse_counts = get_pulse_counts(); // Compute length of pulse in ticks

pulse_time_count = pulse_counts; // Load pulse length timer

// --------------------------

// display_rpm_mode()

//

// Refresh the display.

// --------------------------

int display_rpm_mode()

lcd.setCursor(0, 0); // Set the cursor at the start of the top line

lcd.print(" "); // Display the strobe repetition rate in RPM

display_5_digits(rpm_demand);

lcd.print(" rpm ");

//display_5_digits(repetition_counts); // Debugging: Display the repetition tick value

lcd.setCursor(0, 1); // Set the cursor at the start of the bottom line

switch(display_mode) // Show display mode as

case 0: // 'P' - Potentiometer input mode

lcd.print("P");

break;

case 1: // 'K' - Keypad input mode

lcd.print("K");

break;

display_5_digits(pulse_percent); // Display pulse length as percentage of rate

lcd.print(" % ");

//display_5_digits(pulse_counts); // Debugging: Display pulse length in ticks

// --------------------------

// display_5_digits(val)

//

// Print a +ve 5 digit value to the display.

// Printed right justified.

// --------------------------

void display_5_digits(int val)

if (val < 10000)

// Print appropriate number of

lcd.print(" "); // spaces depending on value

if (val < 1000)

lcd.print(" ");

if (val < 100)

lcd.print(" ");

if (val < 10)

lcd.print(" ");

lcd.print(val); // Print the value

Page 9: HDE Issue #4 April 17th 2014 Arduino Tachometerhomediyelectronics.com/ls/vuU6EWq7uR79ayx5Eumt/hde_4.pdf · Using the tachometer Once you have built the hardware and loaded the software

Be the first to see future issues of HDE Magazine by subscribing for free Copyright © homediyelectronics.com

Page 9 of 14

// --------------------------

// setup_display_rpm_mode()

//

// Initialise the display mode

// --------------------------

int setup_display_rpm_mode()

display_mode = 0; // Potentiometer mode

// --------------------------

// get_pulse_counts()

//

// Compute the timer counts for pulse duration.

// --------------------------

int get_pulse_counts()

return int((float(pulse_percent) * float(repetition_counts)) / 100); // Compute length of pulse

// --------------------------

// get_repetition_counts()

// Compute the timer counts for pulse repetition.

// --------------------------

int get_repetition_counts()

return int((60000000.0 / (float(rpm_demand) * float(TIMER_US))) ); // Compute pulse repetition rate ticks

// --------------------------

// loop()

// Main loop.

// Keypad buttons are serviced every 50mS.

// Display is updated every 500mS.

// --------------------------

void loop()

static unsigned long print_timer = 0; // For scheduling LCD updates

static unsigned long button_timer = 0; // For scheduling button polling

if ((millis() - button_timer) > 50) // Is it time to check the buttons?

button_timer = millis(); // Reset the timer

switch(display_mode)

case 0:

do_button_input_p(); // Hande user input from potentiometer

break;

case 1:

do_button_input_k(); // Handle user input on buttons

break;

if ((millis() - print_timer) > 500) // Is it time to update the display?

print_timer = millis(); // Reset the timer

switch(display_mode)

case 0:

do_speed_pot_input(); // Handle speed demand pot

break;

case 1:

break;

display_rpm_mode(); // Show rpm and pulse values

Page 10: HDE Issue #4 April 17th 2014 Arduino Tachometerhomediyelectronics.com/ls/vuU6EWq7uR79ayx5Eumt/hde_4.pdf · Using the tachometer Once you have built the hardware and loaded the software

Be the first to see future issues of HDE Magazine by subscribing for free Copyright © homediyelectronics.com

Page 10 of 14

// --------------------------

// clear_display()

//

// Clear the display.

// --------------------------

void clear_display()

char* blank = " ";

lcd.setCursor(0, 0); // Clear the top row;

lcd.print(blank);

lcd.setCursor(0, 1); // Clear the bottom row;

lcd.print(blank);

// --------------------------

// timerIsr() 50uS interrupt routine

// Called every time the hardware timer 1 times out.

// --------------------------

void timerIsr()

pulse_manager(); // Handle the strobe pulses

// --------------------------

// pulse_manager() called every 50 uS to schedule and deliver pulses.

// --------------------------

void pulse_manager()

static volatile int state = 0; // State machine variable

--repetition_time_count; // Must decrement the repetition count every tick

switch(state) // State machine handles delivery of

// a regularly repeating pulse

case 0: // Normal state does nothing

if (repetition_time_count <= 0) // Time to do a pulse?

// Time out - Initiate pulse

repetition_time_count = repetition_counts; // Reload for the next pulse

digitalWrite(led1Pin, HIGH); // Set the strobe output high

pulse_time_count = pulse_counts; // Load pulse length timer

state = 1; // Changing to state 1 initiates a pulse

break;

case 1: // Initiate pulse

if (--pulse_time_count <= 0) // Time to end the pulse?

// Time out - terminate the pulse

digitalWrite(led1Pin, LOW); // Set the strobe output low

state = 0; // and set state to 0

break;

default:

state = 0; // Return state to normal 0

break;

// --------------------------

// read_LCD_buttons()

// Read the buttons on the LCD mini keypad.

// The buttons are all wired through resistors to an

// analog input channel so which button has been pressed

// can be determined by the value read from the input.

//

// Note: It may be neccessary to calibrate the keypad input as

// voltages on your board may differ from those used here.

// my buttons when read are centered at these valies: 0, 100, 250, 410, 640

// we add approx 50 to those values and check to see if we are close

// --------------------------

int read_LCD_buttons()

int adc_key_in;

adc_key_in = analogRead(0); // read the value from the sensor

// Debug code - Uncomment this to send the key value to the serial monitor for calibration.

/* *******************************

Page 11: HDE Issue #4 April 17th 2014 Arduino Tachometerhomediyelectronics.com/ls/vuU6EWq7uR79ayx5Eumt/hde_4.pdf · Using the tachometer Once you have built the hardware and loaded the software

Be the first to see future issues of HDE Magazine by subscribing for free Copyright © homediyelectronics.com

Page 11 of 14

Serial.println(adc_key_in);

Debug code End **************** */

// Each key will read a different value.

if (adc_key_in > 1000) return btnNONE; // 1st option for speed reasons since it will be the

most likely result.

if (adc_key_in < 50) return btnRIGHT;

if (adc_key_in < 150) return btnUP;

if (adc_key_in < 300) return btnDOWN;

if (adc_key_in < 460) return btnLEFT;

if (adc_key_in < 690) return btnSELECT;

return btnNONE; // When all others fail, return this...

// --------------------------

// do_speed_pot_input()

// Read the analog speed pot

// --------------------------

void do_speed_pot_input()

static int last_speed_pot_in = 0;

int speed_pot_in;

speed_pot_in = analogRead(1); // Read the value from the control

//Serial.println(speed_pot_in);

if (speed_pot_in != last_speed_pot_in) // Action only if the value changes

last_speed_pot_in = speed_pot_in; // Save value for next reading

rpm_demand = (speed_pot_in + 16) * 4; // Roughly 64 - 4156 rpm

set_rpm_demand(); // Compute and set parameters

// --------------------------

// do_button_input_k()

// Read the buttons on the LCD mini keypad.

// Determine and take appropriate action.

// The buttons are debounced using a simple finite state

// machine which requires the routine to be called frequently.

//

// The debug code prints the state transitions to the serial monitor.

// --------------------------

void do_button_input_k()

int button_down;

static int button_state = 0; // Initialise the state machine to state 0

static unsigned long int auto_repeat; // Repeat if button down

static unsigned long down_timer; // For scheduling button polling

button_down = read_LCD_buttons(); // Read the keypad

switch(button_state) // Begin the finite state machine key debouncer

case 0: // 0 - idle state. No buttons being pressed.

if (button_down != btnNONE) // Is a button down?

down_timer = millis(); // Start the button down timer

auto_repeat = millis(); // Start the auto repeat timer

button_state = 1; // Go to state 1 to begin the debounce

break;

case 1: // 1 A button was down

if (button_down == btnSELECT)

// Select changes mode

button_state = 3; // Mode changes in state 3

break;

if (button_down == btnUP)

// Nudge rpm demand up

rpm_demand++;

if(rpm_demand > 30000)

// Limit the maximum value

rpm_demand = 30000;

Page 12: HDE Issue #4 April 17th 2014 Arduino Tachometerhomediyelectronics.com/ls/vuU6EWq7uR79ayx5Eumt/hde_4.pdf · Using the tachometer Once you have built the hardware and loaded the software

Be the first to see future issues of HDE Magazine by subscribing for free Copyright © homediyelectronics.com

Page 12 of 14

if (button_down == btnDOWN)

// Nudge rpm demand down

rpm_demand--;

if(rpm_demand < 60)

// Limit the minimum value

rpm_demand = 60;

if (button_down == btnRIGHT)

// Nudge the duty cycle up

pulse_percent++;

if (pulse_percent > 50)

// Limit maximum duty cycle

pulse_percent = 50;

if (button_down == btnLEFT)

// Nudge the duty cycle down

pulse_percent--;

if (pulse_percent < 1)

// Limit minimum duty cycle

pulse_percent = 1;

set_rpm_demand(); // Set the new parameters

button_state = 2; // Go to state 2 to wait for no button down

break;

case 2: // 2 Waiting for btnNONE ensures no further action

if (button_down == btnNONE) // until the button is released and pushed again.

button_state = 0;

else

if ((millis() - auto_repeat) > 500) // Auto repeat after timout

auto_repeat = millis();

button_state = 1;

break;

case 3:

if (button_down == btnNONE) // When button released

display_mode = 0; // Set potentiometer mode

button_state = 0; // Back to idle state

break;

// --------------------------

// do_button_input_p()

//

// Takes rpm input from the potentiometer

// --------------------------

void do_button_input_p()

int button_down;

static int button_state = 0; // Initialise the state machine to state 0

button_down = read_LCD_buttons(); // Read the keypad

switch(button_state) // Begin the finite state machine key debouncer

case 0: // 0 - Idle state. No buttons being pressed.

if (button_down != btnNONE)

Page 13: HDE Issue #4 April 17th 2014 Arduino Tachometerhomediyelectronics.com/ls/vuU6EWq7uR79ayx5Eumt/hde_4.pdf · Using the tachometer Once you have built the hardware and loaded the software

Be the first to see future issues of HDE Magazine by subscribing for free Copyright © homediyelectronics.com

Page 13 of 14

button_state = 1; // Go to state 1 to begin the debounce

break;

case 1: // 1 A button was down

if (button_down == btnSELECT) // If it's the select button

button_state = 2; // Go to state 2 to wait for no button down

else

button_state = 0; // Back to idle

break;

case 2:

if (button_down == btnNONE)

display_mode = 1; // Change to keypad input mode

button_state = 0; // Back to idle

break;

Page 14: HDE Issue #4 April 17th 2014 Arduino Tachometerhomediyelectronics.com/ls/vuU6EWq7uR79ayx5Eumt/hde_4.pdf · Using the tachometer Once you have built the hardware and loaded the software

Be the first to see future issues of HDE Magazine by subscribing for free Copyright © homediyelectronics.com

Page 14 of 14

The Home DIY Electronics Magazine

is produced and edited by me, Steve Garratt. I would love to hear what you think about this

magazine or the web site so please feel free to drop me a line at

[email protected] Or use the

contact form on the web site.

Introduction To Basic Electronics An honest review of this downloadable

course

I go through all of the material in this product to highlight both bad and the good points. Overall it’s a good beginners introduction to the how and why of

electronics. Find out how this information can help you get the most from your electronic project building.