[script] /* Copyright 2012 Mozilla Foundation Copyright 2013 Lu Wang
Apache License Version 2.0 */ (function(){function b(a,b,e,f){var c=(a.className||"").split(/\s+/g);""===c[0]&&c.shift();var d=c.indexOf(b);0>d&&e&&c.push(b);0<=d&&f&&c.splice(d,1);a.className=c.join(" ");return 0<=d}if(!("classList"in document.createElement("div"))){var e={add:function(a){b(this.element,a,!0,!1)},contains:function(a){return b(this.element,a,!1,!1)},remove:function(a){b(this.element,a,!1,!0)},toggle:function(a){b(this.element,a,!0,!0)}};Object.defineProperty(HTMLElement.prototype,"classList",{get:function(){if(this._classList)return this._classList; var a=Object.create(e,{element:{value:this,writable:!1,enumerable:!0}});Object.defineProperty(this,"_classList",{value:a,writable:!1,enumerable:!1});return a},enumerable:!0})}})(); [/script] [script] (function(){/* pdf2htmlEX.js: Core UI functions for pdf2htmlEX Copyright 2012,2013 Lu Wang
and other contributors https://github.com/coolwanglu/pdf2htmlEX/blob/master/share/LICENSE */ var q=window.pdf2htmlEX=window.pdf2htmlEX||{},r="pc",s="pi",t={container_id:"page-container",sidebar_id:"sidebar",outline_id:"outline",loading_indicator_cls:"loading-indicator",preload_pages:3,render_timeout:100,scale_step:0.9,key_handler:!0,hashchange_handler:!0,__dummy__:"no comma"};function v(a,b){return[a[0]*b[0]+a[2]*b[1]+a[4],a[1]*b[0]+a[3]*b[1]+a[5]]}function w(a){for(var b=0,c=a.length;b
=c[1])break}f=h-1;0>f&&(f=0);var f=e[f].page,k=f.clientWidth,h=f.clientHeight,l=f.offsetLeft+f.clientLeft,m=c[0]-l;0>m?m=0:m>k&&(m=k);k=f.offsetTop+f.clientTop;c=c[1]-k;0>c?c=0:c>h&&(c=h);for(h=0;h
b||b>=d.length||(b=z(d[b]),void 0===c&&(c=[0,0]),a=a.a,a.scrollLeft+=c[0]-b[0],a.scrollTop+=c[1]-b[1])} function G(a,b,c){try{var d=JSON.parse(b)}catch(e){return}if(d instanceof Array){b=d[0];var g=a.n;if(b in g){var h=g[b];b=a.c[h];for(var g=2,f=d.length;g
d.length;)d.push(null);g=c||a.c[a.e];c=z(g);c=v(g.o,[c[0],g.height()-c[1]]);var g=a.scale,l=[0,0],m=!0,f=!1,k=a.scale;switch(d[1]){case "XYZ":l=[null===d[2]?c[0]:d[2]*k,null===d[3]?c[1]:d[3]*k];g=d[4];if(null===g||0===g)g=a.scale;f=!0;break;case "Fit":case "FitB":l=[0,0];f=!0;break; case "FitH":case "FitBH":l=[0,null===d[2]?c[1]:d[2]*k];f=!0;break;case "FitV":case "FitBV":l=[null===d[2]?c[0]:d[2]*k,0];f=!0;break;case "FitR":l=[d[2]*k,d[5]*k],m=!1,f=!0}f&&(a.d(g,!1),d=function(b){l=v(b.k,l);m&&(l[1]=b.height()-l[1]);H(a,h,l)},b.loaded?d(b):(I(a,h,void 0,d),H(a,h)))}}} function J(a){window.addEventListener("DOMMouseScroll",function(b){if(b.ctrlKey){b.preventDefault();var c=a.a,d=c.getBoundingClientRect();a.d(Math.pow(a.b.scale_step,b.detail),!0,[b.clientX-d.left-c.clientLeft,b.clientY-d.top-c.clientTop])}},!1);window.addEventListener("keydown",function(b){var c=!1,d=b.ctrlKey||b.metaKey,e=b.altKey;switch(b.keyCode){case 61:case 107:case 187:d&&(a.d(1/a.b.scale_step,!0),c=!0);break;case 173:case 109:case 189:d&&(a.d(a.b.scale_step,!0),c=!0);break;case 48:d&&(a.d(0, !1),c=!0);break;case 33:e?H(a,a.e-1):a.a.scrollTop-=a.a.clientHeight;c=!0;break;case 34:e?H(a,a.e+1):a.a.scrollTop+=a.a.clientHeight;c=!0;break;case 35:a.a.scrollTop=a.a.scrollHeight;c=!0;break;case 36:a.a.scrollTop=0,c=!0}c&&b.preventDefault()},!1)}function F(a,b){if(void 0!==a.j){if(!b)return;clearTimeout(a.j)}a.j=setTimeout(function(){delete a.j;K(a)},a.b.render_timeout)} function K(a){for(var b=a.a,c=b.scrollTop,d=b.clientHeight,b=c-d,c=c+d+d,d=a.c,e=0,g=d.length;e
=b?h.loaded?h.show():I(a,e):A(h)}}function C(){var a="@media screen{."+r+"{display:none;}}",b=document.createElement("style");b.styleSheet?b.styleSheet.cssText=a:b.appendChild(document.createTextNode(a));document.head.appendChild(b)} function I(a,b,c,d){var e=a.c;if(!(b>=e.length||(e=e[b],e.loaded||a.i[b]))){var e=e.page,g=e.getAttribute("data-page-url");if(g){a.i[b]=!0;var h=a.q.cloneNode();h.classList.add("active");e.appendChild(h);var f=a,k=new XMLHttpRequest;k.open("GET",g,!0);k.onreadystatechange=function(){if(4==k.readyState){if(200===k.status){var a=document.createElement("div");a.innerHTML=k.responseText;for(var c=null,a=a.childNodes,g=0,e=a.length;g
c)){for(var d=a.a,e=d.scrollTop,d=e+d.clientHeight,l=-1,m=c,n=m-l;1
=e?m=p:l=p;n=m-l}a.l=m;for(var p=l=a.e,D=0;m
d)break;n=(Math.min(d,u+n)-Math.max(e,u))/n;if(m===l&&1E-6>=Math.abs(n-1)){p=l;break}n>D&&(D=n,p=m)}a.e=p}F(a,!0)}, !1),[a.a,a.outline].forEach(function(b){b.addEventListener("click",a.p.bind(a),!1)}),K(a))}q.Viewer=B;})(); [/script] [script] try{ pdf2htmlEX.defaultViewer = new pdf2htmlEX.Viewer({}); }catch(e){} [/script]

Workshop session 6
Create the following subfolder named “Session6”. All programs (scripts) created this workshop session will be saved in the “Session6” folder.
Activity 1: Turn to a degree (Disconnect the Arduino UNO R3 from the PC)
In this activity, you are going to prompt the user to input a degree to turn.
The servo motor will then turn to the specified degree.
INPUT
PROCESSING
OUTPUT
Keyboard
What’s happening on the prototype?
Screen output (Python shell)
angle
The motor turns according to the angle entered.
Value is displayed on the screen
Build: Attach the servo horn arm to the servo motor after it automatically moves to 0°.
Servo motor
When a servo motor is powered it
will automatically move to the
starting position.
This position
represents 0˚ (imagine a protractor).
The servo motor only turns to ±180˚
and back in the same direction to 0°.
It’s like a swinging gate!
***DO NOT FORCE THE MOTOR BY HAND***
MyCode
Session6
Servo horn arm
Attach the horn arm blade to
the servo motor
after it
automatically moves to 0°.
© Copyright 2020 by Reginald G. Govender

Code: Save the script as Act1Degree
Explanation of code:
Line 1: Making use of the time package.
Line 2: Making use of the Pyfirmata package.
Line 4: Declare a variable called board that is assigned to the Arduino attached to your PC.
Line 5: Delay the code for 2 seconds. This is for Python to establish a connection between the Arduino and the PC.
Line 7: Declare a variable motorpin. The d refers to the digital pin, 10 is the pin that the jumper is connected and s refers to servo.
Line 8: Display a message on the screen that the motor is positioned at 0°.
Line 9: Start of while loop. You will notice that flag is never set to false within the while loop. Therefore lines 10 to 12 will continually run.
Line 10: Declare a variable called angle to hold/store the user input.
Line 11: Writes angle to the pin where the motor is connected.
Line 12: Display a message.
*Now connect the Arduino board to the computer, give it a few seconds to detect before running the code.
If you enter a degree value greater than
180˚ the motor will only open to it’s
maximum which is about 180˚.
If you enter a degree value less than 0˚ you
will encounter an error. This error is known
as a run-time error.

Activity 2: Button open and close (Disconnect the Arduino UNO R3 from the PC)
In this activity, you are going to code such that when the user holds down on a button the servo motor must swing open. When the user releases the button the servo motor
must return to its starting position (0°).
INPUT
PROCESSING
OUTPUT
Push button
What’s happening on the prototype?
Screen output (Python shell)
Pressed
The motor will swing open.
OPEN
Released
The motor will return to its starting position (0°).
CLOSE
Build:
Push button
The push button comes with
four legs which can be place on
the breadboard. The legs that
are directly opposite each other
are the connected on the inside.
Therefore you will notice in the
build we will only use one side
of the legs. You can also attach
a cap/cover on the button.
The push button can have two
st
ates True (button pushed
down or False (button
released).

Code: Save the script as Act2Button
Explanation of code:
Line 1: Making use of the time package.
Line 2: Making use of the Pyfirmata package.
Line 4: Declare a variable called board that is assigned to the Arduino attached to your PC.
Line 5: Delay the code for 2 seconds. This is for Python to establish a connection between the Arduino and the PC.
Line 7: Declare a variable it which sets up an iterator thread that will be used to read the status of the inputs of the circuit.
Line 8: Start the iterator otherwise the board will keep sending data to your serial until it overflows.
Line 10: Declare a variable motorPin. The d refers to the digital pin, 10 is the pin that the jumper is connected and s refers to servo.
Line 11: Declare a variable buttonPin. The d refers to the digital pin, 7 is the pin that the jumper is connected and i refers to input.
Line 12: Display message.
Line 13: Start of while loop. You will notice that flag is never set to false within the while loop. Therefore lines 14 to 20 will continually run.
Line 14: Declare a variable called buttonState to hold/store the state of the button.
Line 15: if statement to check the state of the button.
Line 16: Display “OPEN” on the screen.
Line 17: Turns the motor to 180 degrees.
Line 18: else part of if statement.
Line 19: Return (close) the motor to starting position by writing 0 degrees.
Line 20: Display “CLOSE” on the screen.
*Now connect the Arduino board to the computer, give it a few seconds to detect before running code.

Activity 3: Fibonacci in motion (Disconnect the Arduino UNO R3 from the PC)
In this activity, you will code the servo motor to turn accordingly to the first 12 terms of the Fibonacci sequence. Your program must display the Fibonacci number on the
screen and make a beep sound after every motor turn. No user input is needed.
Further explanation: 0 (no move), 1 (one-degree move), 1 (one-degree move), 2 (two-degree move), 3 (three-degree move), 5 (five-degree move), and so on.
INPUT
PROCESSING
OUTPUT
Keyboard
What’s happening on the prototype?
Screen output (Python shell)
-
Servo motor turns and buzzer sounds
Fibonacci term
*Remember to use a time delay between switching the buzzer ON and OFF.
Fibonacci code for the 6 first terms
1 nterms = 6
2 n1 =0
3 n2 = 1
4 count = 0
5 while count < nterms:
6 print(n1)
7 nth = n1 + n2
8 n1 = n2
9 n2 = nth
10 count += 1
Fibonacci:
The Fibonacci sequence (1, 1, 2, 3, 5, 8, 13,
21, 34, 55, 89, 144 …) can be seen in so
many places- in nature, art, engineering,
music and mathematics! We make each
term in the series by adding together the
two previous terms: 1+1 = 2, 1+2 = 3, 2+3 =
5, and so on
Explanation of lines:
1 set the number of terms
2 first term is set
3 second term is set
4 sets the counter for loop
5 while count < nterms
6 screen output
7 next term is calculated
8 n1 updated
9 n2 updated
10 counter incremented

Build:
Your code: Save the script as Act3Fibonacci
Test your code:
*Now connect the Arduino board to the computer, give it a few seconds to detect before running code.
What will you see and hear?
You will see on the screen
You will hear
You will see the motor
“buzzer beep”
move 0°
1
“buzzer beep”
move 1°
1
“buzzer beep”
remain at same position
2
“buzzer beep”
move 2°
3
“buzzer beep”
move 3
°
5
“buzzer beep”
move 5°
8
“buzzer beep”
move 8°
13
“buzzer beep”
move 13
°
21
“buzzer beep”
move 21°
34
“buzzer beep”
move 34°
55
“buzzer beep”
move 55
°
89
“buzzer beep”
move 89°