gdgph hack fair presentation

110
How designing an Arduino Boot Camp! inspired me to think about code (it really did!)

Upload: mithi-sevilla

Post on 22-Jan-2017

221 views

Category:

Software


0 download

TRANSCRIPT

Page 1: GDGPH Hack Fair Presentation

How designing an

Arduino Boot Camp!inspired me to think about code

(it really did!)

Page 2: GDGPH Hack Fair Presentation

Slides prepared for:GDGPH Hack Fair - December 19, 2015

The Mind Museum Taguig, Manila

Page 3: GDGPH Hack Fair Presentation

Hello! I am Mithi!

Page 4: GDGPH Hack Fair Presentation

I graduated from BS Electronics and Communication Engineering in UP Diliman sometime ago...

I am also one of the co-founders of Nanica.io, a young and small robotics education start-up.

Hello! I am Mithi!

Page 5: GDGPH Hack Fair Presentation

Here are a few thingswe do at Nanica.io(it’s video time, guys!)

Page 6: GDGPH Hack Fair Presentation

Our most recentproject isArduino Boot Camp: A Different Approach!

Page 7: GDGPH Hack Fair Presentation
Page 8: GDGPH Hack Fair Presentation

A few cool Arduino projects:

Page 9: GDGPH Hack Fair Presentation
Page 10: GDGPH Hack Fair Presentation
Page 11: GDGPH Hack Fair Presentation
Page 12: GDGPH Hack Fair Presentation
Page 13: GDGPH Hack Fair Presentation
Page 14: GDGPH Hack Fair Presentation
Page 15: GDGPH Hack Fair Presentation

Our most recentproject isArduino Boot Camp: A Different Approach!

Page 16: GDGPH Hack Fair Presentation

I designed it with <3 (love) for beginners and intermediate Arduino users

Page 17: GDGPH Hack Fair Presentation

You can find it at:http://ArduinoBootCamp.xyz

Page 18: GDGPH Hack Fair Presentation

It’s NOT your usual Basic Arduino Workshop >_<

Page 19: GDGPH Hack Fair Presentation

How?

Page 20: GDGPH Hack Fair Presentation

Well, let me give you an example.

Page 21: GDGPH Hack Fair Presentation

Usually, in beginner workshops, you are taught the following:

Page 22: GDGPH Hack Fair Presentation

ONE:How to blink an LED

Page 23: GDGPH Hack Fair Presentation

TWO:How to blink an LED without delay()

Page 24: GDGPH Hack Fair Presentation

THREE:How to make a breathing LED

Page 25: GDGPH Hack Fair Presentation

FOUR:How to sweep a servo back and forth

Page 26: GDGPH Hack Fair Presentation

FIVE:How to light an LED with a debouncedbutton

Page 27: GDGPH Hack Fair Presentation

This is how the official Arduino website teaches you how to blink an LED...

Page 28: GDGPH Hack Fair Presentation

digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(13, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second

Page 29: GDGPH Hack Fair Presentation

We prefer to do it like this:

Page 30: GDGPH Hack Fair Presentation

led.Toggle();delay(1000);

Page 31: GDGPH Hack Fair Presentation

This is how the official Arduino website teaches you how to blink an LED without delay()...

Page 32: GDGPH Hack Fair Presentation

unsigned long currentMillis = millis(); if(currentMillis - previousMillis >= interval){

previousMillis = currentMillis;

if (ledState == LOW) ledState = HIGH; else ledState = LOW;

digitalWrite(ledPin, ledState);}

Page 33: GDGPH Hack Fair Presentation

We prefer to do it like this:

Page 34: GDGPH Hack Fair Presentation

if(metronome.Tick()) led.Toggle();

Page 35: GDGPH Hack Fair Presentation

This is how the official Arduino website teaches you how to make a breathing LED...

Page 36: GDGPH Hack Fair Presentation

int fadeValue;

for(fadeValue=0; fadeValue<=255; fadeValue+=5){ analogWrite(ledPin, fadeValue); delay(30);}

for(fadeValue=255; fadeValue>=0; fadeValue-=5){ analogWrite(ledPin, fadeValue); delay(30);}

Page 37: GDGPH Hack Fair Presentation

We prefer to do it like this:

Page 38: GDGPH Hack Fair Presentation

led.Set(sweeper.Next(metronome.Tick()));

Page 39: GDGPH Hack Fair Presentation

if (metronome.Tick()) led.Set(sweeper.Next(1));

Page 40: GDGPH Hack Fair Presentation

This is how the official Arduino website teaches you how to sweep a servo...

Page 41: GDGPH Hack Fair Presentation

for (pos = 0; pos <= 180; pos += 1) { servo.write(pos); delay(15); }

for (pos = 180; pos >= 0; pos -= 1) { servo.write(pos); delay(15); }

Page 42: GDGPH Hack Fair Presentation

We prefer to do it like this:

Page 43: GDGPH Hack Fair Presentation

if (metronome.Tick()): servo.write(sweeper.Next(1));

Page 44: GDGPH Hack Fair Presentation

servo.write(sweeper.Next(metronome.Tick()));

Page 45: GDGPH Hack Fair Presentation

This is how the official Arduino website teaches youhow to light an LED with a debounced button...

Page 46: GDGPH Hack Fair Presentation

int reading = digitalRead(buttonPin);if (reading != lastButtonState) { lastDebounceTime = millis();}if ((millis() - lastDebounceTime) > debounceDelay) { if (reading != buttonState) { buttonState = reading; if (buttonState == HIGH) ledState = !ledState; }}digitalWrite(ledPin, ledState);lastButtonState = reading;

Page 47: GDGPH Hack Fair Presentation

We prefer to do it like this:

Page 48: GDGPH Hack Fair Presentation

button.Pressed() ? led.On() : led.Off();

Page 49: GDGPH Hack Fair Presentation

Basically, it’s different because it emphasizes the following things immediately:

Page 50: GDGPH Hack Fair Presentation

ONE: CLEAN READABLE CODE

Page 51: GDGPH Hack Fair Presentation

TWO: BASIC OBJECT-ORIENTED DESIGN

Page 52: GDGPH Hack Fair Presentation

THREE: REDUCED USAGE OF delay()so you can multi-task anytime.

Page 53: GDGPH Hack Fair Presentation

BUT HOW DO YOU DO THAT????!?

Page 54: GDGPH Hack Fair Presentation

The obvious message here is how you can use the power of OOP design thinking...

Page 55: GDGPH Hack Fair Presentation

... to abstract implementation details...

Page 56: GDGPH Hack Fair Presentation

... so that you can focus at the things you want to do.

Page 57: GDGPH Hack Fair Presentation

You get the beneficial side-effects as well:

Page 58: GDGPH Hack Fair Presentation

Code that is easy to understand.

Page 59: GDGPH Hack Fair Presentation

Code that is easy to debug.

Page 60: GDGPH Hack Fair Presentation

Code that is multi-tasking ready.

Page 61: GDGPH Hack Fair Presentation

Code that is scalable.Easily add as many buttons and LEDs as the Arduino can allow.

Page 62: GDGPH Hack Fair Presentation

Code that allows more complex behavior.Add more features and functions without overwhelming yourself.

Page 63: GDGPH Hack Fair Presentation

BUT… HOW DO YOU DO THAT EXACTLY ????!?

Page 64: GDGPH Hack Fair Presentation

The first step is to identify the OBVIOUS objects

Page 65: GDGPH Hack Fair Presentation

LED, BUTTON, and SERVO (the Arduino already has a built-in servo class in one of its libraries)

Page 66: GDGPH Hack Fair Presentation

DigitalOutput led;

led.New(int pin);led.On();led.Off();led.Toggle();led.Set(int brightness);

Page 67: GDGPH Hack Fair Presentation

Button button;

button.New(pin, debounceTime);bool state = button.Pressed();

Page 68: GDGPH Hack Fair Presentation

The next step is to identify not so obvious objects

Page 69: GDGPH Hack Fair Presentation

sweeper.New(x1, x2, inc, type); sweeper.Next(0/1);

// type = BACKANDFORTH/NORMAL/* if 0, returns current state** if 1, updates to and return ** next state */

Page 70: GDGPH Hack Fair Presentation

metronome.New(milliSeconds)bool hasTicked= metronome.Tick()

Page 71: GDGPH Hack Fair Presentation

You can use sweeper in myriad applications…not just servos and LEDs...

Page 72: GDGPH Hack Fair Presentation

You can use this to toggle buttons,play tunes, do countdowns...

Page 73: GDGPH Hack Fair Presentation

and even do away with long subroutines because of for-loops.

Page 74: GDGPH Hack Fair Presentation

Using metronome instead of delay(), you get a more readable code that’s even multi-tasking ready.

Page 75: GDGPH Hack Fair Presentation

You can even sweep multiple servos….

Page 76: GDGPH Hack Fair Presentation

...blink and sweep multiple LEDs...

Page 77: GDGPH Hack Fair Presentation

...(simultaneously, and at different rates )...

Page 78: GDGPH Hack Fair Presentation

...while catching as many buttons as you wish...

Page 79: GDGPH Hack Fair Presentation

...without making your code a nightmare.

Page 80: GDGPH Hack Fair Presentation

You can even sweep multiple servos, blink and sweep multiple LEDs, (simultaneously, at different rates)while catching as many buttons as you wish, without making your code a nightmare.

Page 81: GDGPH Hack Fair Presentation

Awesome right?!!

Page 82: GDGPH Hack Fair Presentation

But wait...

Page 83: GDGPH Hack Fair Presentation

...what about performance?

Page 84: GDGPH Hack Fair Presentation

Only sacrifice readability for performance if you have measured that your code is too slow for its intended use.

Page 85: GDGPH Hack Fair Presentation

Premature optimization is the root of all evil. -Sir Tony Hoare

Page 86: GDGPH Hack Fair Presentation

Programs must be written for people to read, and only incidentally for machines to execute.-Harold AbelsonStructure and Interpretation of Computer Programs

Page 87: GDGPH Hack Fair Presentation

Any fool can write code that a computer can understand. Good programmers write code that humans can understand.

-Martin FowlerRefactoring: Improving the Design of Existing Code

Page 88: GDGPH Hack Fair Presentation

If the computer doesn't run it, it's broken. If people can't read it, it will be broken. Soon.-Some Random Guy (or girl?) in Stack Overflow

Page 89: GDGPH Hack Fair Presentation

Correct. Beautiful. Fast. (in that order) -Elliot Rusty Harold

Page 90: GDGPH Hack Fair Presentation

I hope to dispel the myth that fast code must be illegible ugly code... -Elliot Rusty Harold

Page 91: GDGPH Hack Fair Presentation

...improvement in beauty can also lead to improvement in speed.-Elliot Rusty Harold

Page 92: GDGPH Hack Fair Presentation

One more thing though...

Page 93: GDGPH Hack Fair Presentation

No code is God ;)

Page 94: GDGPH Hack Fair Presentation
Page 95: GDGPH Hack Fair Presentation
Page 96: GDGPH Hack Fair Presentation
Page 97: GDGPH Hack Fair Presentation
Page 98: GDGPH Hack Fair Presentation
Page 99: GDGPH Hack Fair Presentation
Page 100: GDGPH Hack Fair Presentation
Page 101: GDGPH Hack Fair Presentation
Page 102: GDGPH Hack Fair Presentation
Page 103: GDGPH Hack Fair Presentation

If there’s anything I hope you take home

from this talk...

Page 104: GDGPH Hack Fair Presentation

Two things!

Page 105: GDGPH Hack Fair Presentation

First thing...

Page 106: GDGPH Hack Fair Presentation

I hope it reminds you to design your code not only to

work but also to be easily understood by others…

(and yourself!)

Page 107: GDGPH Hack Fair Presentation

Second thing...

Page 108: GDGPH Hack Fair Presentation

Second thing…(and more important thing!)

Page 109: GDGPH Hack Fair Presentation

I hope you tell your friends about

http://ArduinoBootCamp.xyz!

hehehehe :))

Page 110: GDGPH Hack Fair Presentation

Thank you for listening!

:)