B57as L05
B57as L05
B57AS
Lecture 05
This week:
• The #define Directive
• Arduino Timers – more advanced
• Bit shift
• Lookup tables
• Object Oriented Programming (OOP)
# define
• For constant values like pin assignments that do not change during
the running of a sketch, there is an alternative to using a variable.
• #define allows you to associate a value with a name.
• Everywhere #define appears in your sketch, the value will be
substituted before the sketch is compiled.
#define kLedPin 13
• The #define directive does not use an “=” between the name and
the value.
• It does not even need a ; on the end.
• This is because it is not actually part of the C language itself; but is
called a pre-compiler directive that is run before compilation.
Using #define and const Instead of Integers
void setup() {
stepper.setSpeed(30); // set the speed at 30 RPM
}
void loop() {
// move randomly from at least 1 step
stepper.step(multiplier);
// counting how many steps already moved
// then if we reach a whole turn, reset counter and go backward
if (counter < STEPS) counter++ ;
else {
counter = 0;
multiplier *= -1;
}
}
Setting Timer Pulse Width and Duration
/*
This sketch generates pulses within the frequency of 1 MHz using Timer1
PWM on pin 9.
*/
#include <TimerOne.h>
void setup() {
Serial.begin(9600);
pinMode(outPin, OUTPUT);
Timer1.initialize(period); // initialize timer1, 1000 microseconds
setPulseWidth(pulseWidth); // call our function
void loop() { }
Setting Timer Pulse Width and Duration
bool setPulseWidth(long microseconds)
{
bool ret = false;
int prescaleValue = prescale[Timer1.clockSelectBits];
// calculate time per counter tick in nanoseconds
long precision = (F_CPU / 128000) * prescaleValue ;
period = precision * ICR1 / 1000; // period in microseconds
if( microseconds < period)
{
int duty = map(microseconds, 0,period, 0,1024);
if( duty < 1)
duty = 1;
if(microseconds > 0 && duty < RESOLUTION)
{
Timer1.pwm(outPin, duty);
ret = true;
}
}
return ret;
}
Arduino Timers
• The Arduino Uno has 3 timers:
Timer0, Timer1 and Timer2.
• Timer0 is already set up to
generate a millisecond interrupt to
update the millisecond counter
reported by millis(). Timer0 has
no PWM.
• Timer1 ia a 16-bit timer (it counts
from 0 to 65,535). It’s the same
timer used by analogWrite to
controls pins 9 and 10. Timer1 has
two PWM outputs.
• Timer2 is a 8-bit timer with one
PWM output.
Bit shift – It’s all about performance
• Bitwise operators are specific operators for
bits.
• Bitwise operations are primitive actions
directly supported by the processor.
• Especially with embedded systems, using
bitwise operations can dramatically improve
performance.
• There are four operators and two bit shift
operators.
Boolean Logic & Logic Gates
Digital Logic Gate Truth Table
AND, OR, XOR, and NOT operators
AND, OR, XOR, and NOT operators
/*
* bits sketch demonstrates bitwise operators
*/
void setup() {
Serial.begin(9600);
}
void loop(){
Serial.print("3 & 1 equals "); // bitwise And 3 and 1
Serial.print(3 & 1); // print the result
Serial.print(" decimal, or in binary: ");
Serial.println(3 & 1 , BIN); // print the binary representation of the result
Serial.print("3 | 1 equals "); // bitwise Or 3 and 1
Serial.print(3 | 1 );
Serial.print(" decimal, or in binary: ");
Serial.println(3 | 1 , BIN); // print the binary representation of the result
Serial.print("3 ^ 1 equals "); // bitwise exclusive or 3 and 1
Serial.print(3 ^ 1);
Serial.print(" decimal, or in binary: ");
Serial.println(3 ^ 1 , BIN); // print the binary representation of the result
byte byteVal = 1;
int intVal = 1;
byteVal = ~byteVal; // do the bitwise negate
intVal = ~intVal;
void setup()
void loop() {
// nothing for now!
}
void initCosineLUT() {
// statements…
• The header file has just defined what the class looks like.
• You now need a separate file that actually does the work. This is called the
implementation file and has the extension .cpp.
#include “Flasher.h”
Flasher::Flasher(int pin, int duration) // the prefix :: indicates that the methods
{ // belong to the Flasher class
// these initialise the constructor
pinMode(pin, OUTPUT); // The constructor method (Flasher) just
_pin = pin; // assigns each of its parameters to the
_duration = duration / 2; // appropriate private member variable
void Flasher::Flasher(int times) // The flash member function actually carries out
{ // the business of flashing
for(int i = 0; i < times; i++) { // loops for the appropriate number of
digitalWrite(_pin, HIGH); // times, turning the LED on and off
delay(duration); // for the appropriate delay
digitalWrite(_pin, LOW);
delay(duration);
}
}
Completing Your Library
• Define the keywords used in the library so that the Arduino
IDE can show them in the appropriate colour when users
are editing code.
• To define the keywords, you have to create a file called
keywords.txt, which goes into the Flasher directory.
• This file contains just the two following lines:
– Flasher KEYWORD1
– Flash KEYWORD2
• Class names should be a KEYWORD1 and methods should
be KEYWORD2.
Completing Your Library
• Restart the Arduino IDE, from the
Arduino IDE’s menu, select File and then
New to create a new sketch window.
• Then from the Menu, select Sketch and
the Import Library option or Add Library
option (depending on the version of the
IDE).
• The libraries above the line in the
submenu are the official libraries; below
this line are the “unofficial” contributed
libraries.
• If all has gone well, you should see
Flasher in the list.
• If Flasher is not in the list, it is very likely
that the Flasher folder is not in the
libraries folder of your sketches folder, so
go back and check.
Using Flasher.h
#include “Flasher.h”
void setup() {
// not needed in this case as the set up is done when we
// initialised the Flasher object
}
void loop()
{
slowFlasher.flash(5); // call Flasher member function
delay(1000);
fastFlasher.flash(10);
delay(2000);
}
Conclusion
• There is more to C++ and to writing libraries,
but this should get you started.
• It should also be sufficient for most of what
you are likely to do with an Arduino.
• Arduinos are small devices and the temptation
is often to overengineer solutions that could
otherwise be very simple and straightforward.
Assessment week
• When - Week 9
• Prize: 25%