Kenneth Hawthorn

Kenneth HawthornKenneth HawthornKenneth Hawthorn

Kenneth Hawthorn

Kenneth HawthornKenneth HawthornKenneth Hawthorn
  • Home
  • Super Arduino Book
  • Share Sheets
  • More
    • Home
    • Super Arduino Book
    • Share Sheets

  • Home
  • Super Arduino Book
  • Share Sheets

The "Super Arduino" Book

Finally, the book!

In May of 2019, my wife received a great job offer in Austin, Texas. Over the five

previous years, I had developed the Mechatronics program at St. Raymond school in

Menlo Park, California. There was no question that I wanted to support my wife or that

we would make the move, but at the same time I was terribly sad to leave my dream job

at St. Raymond, where I could express myself freely and develop lessons every week

for my students based on what I found interesting. I made a deal with my wife that I

would, by default, land in Austin as a "Trophy Wife" with no pressure to do anything

besides tinker with interesting tech (my definition of a trophy wife:), until I could find a

dream job in Austin. Teaching was just too fun and rewarding to go back to plain old

engineering. 


Happily, less than a week later, someone at Rockridge Press found this website

and asked me if I would be interested in writing a book that would encompass some of

the Arduino projects I had done over the last five years with students and teachers. I

had also dreamed of writing a book for a few years, but distractible folks like me often,

well, get distracted along the way. So, that email came at the right time! I signed the

contract and punched out the book of my most successful student projects over the

summer. After a lot of long nights here is the result! 

"Super Arduino" Ch 1-7.... Feel Free to copy and paste!

Ch1

Ch1

Ch1

void setup()

{

  pinMode(13, OUTPUT);

}


void loop()

{

  // turn the LED on (HIGH is the voltage level)

  digitalWrite(13, HIGH);

  delay(300); // Wait for 1000 millisecond(s)

  // turn the LED off by making the voltage LOW

  digitalWrite(13, LOW);

  delay(300); // Wait for 1000 millisecond(s)

}

  

Ch3

Ch1

Ch1

int ledPin = 12;                

void setup()

{

  pinMode(ledPin, OUTPUT);      

}


void loop()

{

  digitalWrite(ledPin, HIGH);   

  delay(600);                  

  digitalWrite(ledPin, LOW);    

  delay(300);                  

}

Ch4

Ch1

Ch4

#include <Servo.h>

Servo motor1;

void setup() {
motor1.attach(3);
}

void loop() {
motor1.write (50);
delay(100);  

motor1.write (130); 

delay(100);

}

Ch5

Ch5

Ch4

#include <Servo.h>


Servo motor1;


void setup() {

motor1.attach(11);

}


void loop() {

motor1.write(30);

delay(100);

motor1.write(30);

delay(100);

}

Ch6

Ch5

Ch6

#include <Adafruit_NeoPixel.h>


#define PIN 2 // input pin Neopixel is attached to


#define NUMPIXELS      12 // number of neopixels in strip


Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);


int delayval = 1000; // timing delay in milliseconds


int redColor = 250;

int greenColor = 0;

int blueColor = 0;


void setup() {

  // Initialize the NeoPixel library.

  pixels.begin();

}


void loop() {

  setColor();


  for (int i=0; i < NUMPIXELS; i++) {

    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255

    pixels.setPixelColor(i, pixels.Color(redColor, greenColor, blueColor));


    // This sends the updated pixel color to the hardware.

    pixels.show();


    // Delay for a period of time (in milliseconds).

    delay(delayval);

  }

}


// setColor()

// picks random values to set for RGB

void setColor(){

  redColor = random(100, 100);

  greenColor = random(25,50);

  blueColor = random(200, 255);

}

Ch7

Ch5

Ch6

#define trigPin 13

#define echoPin 12

#define led 11

#define led2 10


void setup() {

  Serial.begin (9600);

  pinMode(trigPin, OUTPUT);

  pinMode(echoPin, INPUT);

  pinMode(led, OUTPUT);

  pinMode(led2, OUTPUT);

}


void loop() {

  long duration, distance;

  digitalWrite(trigPin, LOW);  

  delayMicroseconds(2); 

  digitalWrite(trigPin, HIGH);

  delayMicroseconds(10); 

  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);

  distance = (duration/2) / 29.1;

  if (distance < 4) {  

    digitalWrite(led,HIGH); 

  digitalWrite(led2,LOW);

}

  else {

    digitalWrite(led,LOW);

    digitalWrite(led2,HIGH);

  }

  if (distance >= 200 || distance <= 0){

    Serial.println("Out of range");

  }

  else {

    Serial.print(distance);

    Serial.println(" cm");

  }

  delay(500);

}

Ch. 8-11 of the "Super Arduino" book

Ch 8

Ch 8

Ch 8

//This sketch is useful for testing whether or not your ATTiny85

//is uploading code.  It blinks the onboard LED on the TINY AVR 

//USB Stick every half second after you upload the code.


 

int blinkPin = 0;   /* onboard LED is on pin 1


void setup()

{

  pinMode(blinkPin, OUTPUT);   /* onboard LED is an output

}


void loop()

{

  digitalWrite(blinkPin, HIGH);  /* Turn on LED  */

  delay(500);                    /* Wait half a second  */

  digitalWrite(blinkPin, LOW);   /* Turn off LED */

  delay(500);                    /* Wait half a second  */

}

Ch9

Ch 8

Ch 8

#include <Stepper.h>


  #define STEPS 2038 

  Stepper stepper(STEPS, 8, 10, 9, 11);

  

  void setup() {

  

  }

  

  void loop() {

  stepper.setSpeed(3); 

  stepper.step(2038); 

  delay(1000); 

  stepper.setSpeed(7); 

  stepper.step(-2038); 

  

  }

Ch 10

Ch 10

Ch 10

const int buttonPin = 2;    

const int ledPin =  13;    


int buttonState = 0;        


void setup() {

  // initialize the LED pin as an output:

  pinMode(ledPin, OUTPUT);

  // initialize the pushbutton pin as an input:

  pinMode(buttonPin, INPUT);

}


void loop() {


  buttonState = digitalRead(buttonPin);


  if (buttonState == HIGH) {

    // turn LED on:

    digitalWrite(ledPin, HIGH);

  } else {

    // turn LED off:

    digitalWrite(ledPin, LOW);

  }

}

Ch 11

Ch 10

Ch 10

#include <WiFi101.h>

#include <WiFiClient.h>

#include <WiFiServer.h>

#include <WiFiSSLClient.h>

#include <WiFiUdp.h>

#include <SPI.h>

#include <WiFi101.h>

 

char ssid[] = "networkName";      //  your network SSID (name)

char pass[] = "password";   // your network password

int keyIndex = 0;      // your network key Index number (needed only for WEP)

int ledpin = 6;

bool val = true;

 

int status = WL_IDLE_STATUS;

WiFiServer server(80);

 

void setup() {

  Serial.begin(9600);      // initialize serial communication

  Serial.print("Start Serial ");

  pinMode(ledpin, OUTPUT);      // set the LED pin mode

  // Check for the presence of the shield

  Serial.print("WiFi101 shield: ");

  if (WiFi.status() == WL_NO_SHIELD) {

    Serial.println("NOT PRESENT");

    return; // don't continue

  }

  Serial.println("DETECTED");

  // attempt to connect to Wifi network:

  while ( status != WL_CONNECTED) {

    digitalWrite(ledpin, LOW);

    Serial.print("Attempting to connect to Network named: ");

    Serial.println(ssid);                   // print the network name (SSID);

    digitalWrite(ledpin, HIGH);

    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:

    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:

    delay(10000);

  }

  server.begin();                           // start the web server on port 80

  printWifiStatus();                        // you're connected now, so print out the status

  digitalWrite(ledpin, HIGH);

}

void loop() {

  WiFiClient client = server.available();   // listen for incoming clients

 

  if (client) {                             // if you get a client,

    Serial.println("new client");           // print a message out the serial port

    String currentLine = "";                // make a String to hold incoming data from the client

    while (client.connected()) {            // loop while the client's connected

      if (client.available()) {             // if there's bytes to read from the client,

        char c = client.read();             // read a byte, then

        Serial.write(c);                    // print it out the serial monitor

        if (c == '\n') {                    // if the byte is a newline character

 

          // if the current line is blank, you got two newline characters in a row.

          // that's the end of the client HTTP request, so send a response:

          if (currentLine.length() == 0) {

            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)

            // and a content-type so the client knows what's coming, then a blank line:

            client.println("HTTP/1.1 200 OK");

            client.println("Content-type:text/html");

            client.println();

 

            // the content of the HTTP response follows the header:

            client.print("Click <a href=\"/H\">here</a> turn the LED on pin 9 on<br>");

            client.print("Click <a href=\"/L\">here</a> turn the LED on pin 9 off<br>");

 

            // The HTTP response ends with another blank line:

            client.println();

            // break out of the while loop:

            break;

          }

          else {      // if you got a newline, then clear currentLine:

            currentLine = "";

          }

        }

        else if (c != '\r') {    // if you got anything else but a carriage return character,

          currentLine += c;      // add it to the end of the currentLine

        }

 

        // Check to see if the client request was "GET /H" or "GET /L":

        if (currentLine.endsWith("GET /H")) {

          digitalWrite(ledpin, HIGH);               // GET /H turns the LED on

        }

        if (currentLine.endsWith("GET /L")) {

          digitalWrite(ledpin, LOW);                // GET /L turns the LED off

        }

      }

    }

    // close the connection:

    client.stop();

    Serial.println("client disconnected");

  }

}

 

void printWifiStatus() {

  // print the SSID of the network you're attached to:

  Serial.print("SSID: ");

  Serial.println(WiFi.SSID());

 

  // print your WiFi shield's IP address:

  IPAddress ip = WiFi.localIP();

  Serial.print("IP Address: ");

  Serial.println(ip);

 

  // print the received signal strength:

  long rssi = WiFi.RSSI();

  Serial.print("signal strength (RSSI):");

  Serial.print(rssi);

  Serial.println(" dBm");

  // print where to go in a browser:

  Serial.print("To see this page in action, open a browser to http://");

  Serial.println(ip);

}

Bonus Code for Pi Sense Hat & Python Turtle

Random Sparkles (for Pi SenseHat)

Random Sparkles (for Pi SenseHat)

Random Sparkles (for Pi SenseHat)

from sense_hat import SenseHat
from random import randint
from time import sleep

sense = SenseHat()

while True:
   x = randint(0, 7)
   y = randint(0, 7)
   r = randint(0, 255)
   g = randint(0, 255)
   b = randint(0, 255)
   sense.set_pixel(x, y, r, g, b)
   sleep(0.01)

Draw a Sun Burst

Random Sparkles (for Pi SenseHat)

Random Sparkles (for Pi SenseHat)

import turtle 


ninja = turtle.Turtle()


ninja.speed(10)


for i in range(180):

    ninja.forward(100)

    ninja.right(30)

    ninja.forward(20)

    ninja.left(60)

    ninja.forward(50)

    ninja.right(30)

    

    ninja.penup()

    ninja.setposition(0, 0)

    ninja.pendown()

    

    ninja.right(2)

    

turtle.done()

Draw a PolyGone

Draw a PolyGone

Draw a PolyGone

import turtle 


polygon = turtle.Turtle()


num_sides = 6

side_length = 70

angle = 360.0 / num_sides 


for i in range(num_sides):

    polygon.forward(side_length)

    polygon.right(angle)

    

turtle.done()

Sunburst 3D(er)

Draw a PolyGone

Draw a PolyGone

import turtle 

ninja = turtle.Turtle()

ninja.speed(10)

for i in range(180):

    ninja.forward(100)

    ninja.right(30)

    ninja.forward(20)

    ninja.left(60)

    ninja.forward(50)

    ninja.right(30)

    ninja.penup()

    ninja.setposition(0, 0)

    ninja.pendown()

    ninja.right(2)

turtle.done()

Copyleft © 2022 Kenneth Hawthorn


All Images are from actual workshops/camps/classes