What are we going to do ?
USE THESE
TO DRIVE
THESE
Slide 3
Slide 3 text
Why ???
“BECAUSE WE CAN”
AND
“BECAUSE IT'S FUN”
Slide 4
Slide 4 text
And ???
SO THAT
THIS GUY
WILL THINK
HIS DAD
IS COOL
Slide 5
Slide 5 text
And hopefully...
SO THAT
YOU
WILL THINK
THIS STUFF
IS COOL
Slide 6
Slide 6 text
Arduino
Ethernet
Shield
Motor
Shield
The Hardware...
Slide 7
Slide 7 text
An open source microcontroller
Platform that is really easy to use
And supported by a huge and
Fanatical user base who create
Libraries and boards (shields) and
tutorials and variations and stuff....
FREE
NOT
EXPENSIVE
The Arduino...
Slide 8
Slide 8 text
The Motor Shield
Uses an H Bridge and PULSE
WIDTH MODULATION to control
the speed of a motor.
Slide 9
Slide 9 text
Connections...
Ethernet Shield uses pins
4, 10, 11, 12, 13
Motor Shield uses pins
10, 11, 12, 13
USE DIFFERENT PINS FOR THE MOTOR
SHIELD
PWM – 3 → 10, 5 →11 IO 6 →12,7→13
PROBLEM
Some pins clash
SOLUTION
Slide 10
Slide 10 text
How to change the pins?
●
We could cut pins or....
Pins outside socket
So we can re-use shield
Jumper leads
To re-assign
pinout
Slide 11
Slide 11 text
Program the motor...
Define the pins
int pwm_a = 3; //PWM control for motor outputs 1 and 2
int pwm_b = 5; //PWM control for motor outputs 3 and 4
int dir_a = 6; //direction control for motor outputs 1 and 2
int dir_b = 7; //direction control for motor outputs 3 and 4
pinMode(dir_a, OUTPUT);
pinMode(dir_b, OUTPUT);
To go forward
digitalWrite(dir_a, HIGH);
To go backwards
digitalWrite(dir_a, LOW);
To control the speed
analogWrite(pwm_a, speed); // speed is a number from 0 to 255
Slide 12
Slide 12 text
A quick test...
Slide 13
Slide 13 text
Phone Connection
●
Need to select a protocol.....
– HTTP
Very easy – well supported by IOS and Android.
No real session support
– Telnet
Better for manual control by MAC / PC – more work
to integrate into program. Support long sessions.
– Custom
Hard work – required for things like home
automation, complex remote control etc. Supports
long sessions.
Slide 14
Slide 14 text
Since we have TWO cars...
●
We need two controllers (phones)
●
Ethernet shield cannot support multiple
connections so each client has to connect
commando style,
– Open
– Set the parameters
– Close....
●
HTTP looks like a good contender.
●
We can test using a web browser !!!
Slide 15
Slide 15 text
Ethernet Library Basics
Include the libraries
#include
#include
Define a MAC address
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
●
Define Ethernet as listening on port 80
EthernetServer server(80); // HTTP Port
●
Start the server on a particular MAC address
Ethernet.begin(mac);
Slide 16
Slide 16 text
Ethernet handling
●
Check for connection in loop...
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (!client.available()) break;
// Do something... e.g.
char c = client.read(); // on char from input
client.println(“Hello”); // send stuff back
}
}
client.stop();
Slide 17
Slide 17 text
HTTP Protocol....
●
When the browser asks for a page it sends
GET /speed.html?A=0 HTTP/1.1
Host: 192.168.2.100
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6;
rv:13.0) Gecko/20100101 Firefox/13.0.1
Accept:
text/html,application/xhtml+xml,application/xml;q=0.9,*/
*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Browser request
Note blank line here
(end of header)
Slide 18
Slide 18 text
Handling the input...
●
We have to build up the characters into a line
by adding the characters to a string unless the
character is a newline character
char c = client.read();
if (c != '\n')
line = line + c;
else {
handleLine(line); // do something with it;
line = '';
}
Slide 19
Slide 19 text
And the HTTP....
●
We can parse the first line for the page name and the
parameters....
– GET /speed.html?A=0 HTTP/1.1
by
int aPos = str.indexOf("speed.html?A=");
int bPos = str.indexOf("speed.html?B=");
if (aPos > 0)
{
str = str.substring(aPos+13);
char buf[80];
str.toCharArray(buf,80);
speedA = atoi(buf); // will ignore tail
analogWrite(pwm_a,speedA); // write to PWM
}
");
● client.print("server is at ");
● client.println(Ethernet.localIP());
● client.print("CAR A : ");
● client.println(speedA);
● client.print("CAR B : ");
● client.println(speedB);
● client.println("");
● client.println("");
● }
Header
+ TWO
Blank lines
HTML page with
Data added to it
Slide 21
Slide 21 text
Hook it all up....
Moto
Ethernet
Arduino
12V
Wireless /
Wired
Router
Ethernet
cable
Slide 22
Slide 22 text
Knock up a quick app...
Value Changed event
Send an HTTP request to the
Arduino's address formatted with
speed.html?A=xxx
or
speed.html?B=xxx
0 255
Slide 23
Slide 23 text
Now lets see some smoke...
Insert photo here of either
Cars whizzing round
Or
ArduMoto emitting blue smoke
BLOG: ioblocks.blogspot.com
Courses: www.curica.com
Hackerspace SG