Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Hardware is not Boring

Hardware is not Boring

Talk presented to GeekCampSG showing using Arduino and mobile phone to control Scalextric Racing Cars

daveappleton

August 25, 2012
Tweet

Other Decks in Technology

Transcript

  1. 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...
  2. The Motor Shield Uses an H Bridge and PULSE WIDTH

    MODULATION to control the speed of a motor.
  3. 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
  4. 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
  5. 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
  6. 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.
  7. 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 !!!
  8. Ethernet Library Basics Include the libraries #include <SPI.h> #include <Ethernet.h>

    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);
  9. 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();
  10. 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)
  11. 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 = ''; }
  12. 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 }
  13. And output (for browsers) • void printHeader(EthernetClient client) • {

    • client.println("HTTP/1.1 200 OK"); • client.println("Content-Type: text/html"); • client.println("Connnection: close"); • client.println(); • client.println(); • client.println("<!DOCTYPE HTML>"); • client.println("<html>"); • client.println("<head>"); • client.println("<title>Racing Cars</title>"); • client.println("</head><body>"); • client.println("<H1>Racing Car Server v1.0</H1>"); • 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("</body>"); • client.println("</html>"); • } Header + TWO Blank lines HTML page with Data added to it
  14. 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
  15. 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