Assignment 3: Motorized Emotions

Motorized Emotions:  Make a kinetic interaction that expresses an emotion.

Due:  Wed, 27 Sep, 23:59hrs.

Find an emotion you can display with motion.  A couple of examples I used in class:

  • Light makes plants happy:  Use a photoresistor to detect light and create more motion when there is more light, less motion when it is dark.
  • Cats like to be petted:  Use buttons or other inputs to “pet” your cat, use a servo for a wagging tail and tactors to simulate purring.

Document your project with a Fritzing sketch of your circuit, an Arduino sketch, and video.  I’d like everyone to demonstrate these in class on the following Thursday.

08 – class notes, 21 Sep 2017 — Intro to Power and Motors

Timing

Two short sketches showing how we can use the timing commands to blink and LED on and off without using delay() statements.

This is the version suggested in class:

// -*-c++-*-
/*
The MIT License (MIT)

Copyright (c) 2017 J. Eric Townsend

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/


/*
* This is a simple example that turns the LED on and off based on a single time.
*/


int debugLed = 13;
int blinkTime = 500;

bool ledState = LOW;
int now = 0;
int previous = 0;

void setup() {
// put your setup code here, to run once:
pinMode(debugLed, OUTPUT);
Serial.begin(115200);
}

void loop() {
now = millis();

if ((now - previous) > blinkTime) {
ledState = !ledState;
digitalWrite(debugLed, ledState);
previous = now;
}

}

You could also use if/else

// -*-c++-*-
/*
The MIT License (MIT)

Copyright (c) 2017 J. Eric Townsend

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/


/*
* This is a simple state machine that tracks the state of the LED and
* allows it to be on and off for different lengths of time.
*/


int debugLed = 13;
int blinkTimeOn = 1000;
int blinkTimeOff = 1000;

int ledOn = 1;
int ledOff = 2;

bool ledState = LOW;
int now = 0;
int lastOn = 0;
int lastOff = 0;

void setup() {
// put your setup code here, to run once:
pinMode(debugLed, OUTPUT);
Serial.begin(115200);
}

void loop() {
now = millis();

if (!ledState && ((now - lastOff) > blinkTimeOff)) {
ledState = HIGH;
lastOn = now;
} else if (ledState && ((now - lastOn) > blinkTimeOn)) {
ledState = LOW;
lastOff = now;
}

digitalWrite(debugLed, ledState);
}

Driving Actuators

Motors, solenoids, and servos require different methods of control and often require external sources of power.

Here’s a solenoid-example with a Fritzing doc showing the schematic.  There’s a similar method for using a motor that we will go over on Tuesday.

The basics of using a servo that we looked at in class.

07 – class notes, 19 Sep 2017 — Crits and Assignments

07 – class notes, 19 Sep 2017 — Crits and Assignments

Assignment Submissions

In your Arduino sketch be sure to put your name, the assignment, and the date in comments in the start of the sketch.

If you cut-and-pasted something from another person’s sketch, give them credit!

Critiques

Starting with the next assignment we’ll try and have an informal crit of a few projects and do a more practiced version of tonight’s demo.

A guide for professional graphic designers and students that I think applies to our class.

This is from a class taught by Bruce Sterling:

Assignment 2: State Machines, switch/case, for() loops

The second assignment is about state machines, switch/case and for() statements.

As before, go through the process of coming up with an idea, planning it, implementing it, then documenting your project. If you are going to post your project to google docs, please make sure the link is sharable with other people.

This assignment can be done using your Arduino circuit for Assignment 1. The requirements are to implement a state machine with at least four states and transition paths between those states. The transition path can be a switch, a photoresistor, a pot, or some other form of input you’ve learned outside of class.  You’re free to use other outputs or make your own switches, there’s also the collection of components in A10.

Your state machine should use switch/case to change state and for() loops to create output.

Document your entire process, from your first idea, the circuits that didn’t work, the changes you made, and what was the final result. In your documentation, ask yourself what you learned, what went wrong, what you’d do differently if you did it a second time.

To submit your project, post a reply with the documentation. Attach a zip file to that post containing your Arduino sketch, a photo of your finished breadboard circuit, and anything else you think is relevant. I’m working on how we can post video to the blog, but for now you could post it to your AFS account and link to that from your submission.

Due 23:59, Monday, 18 September. Let me know ASAP if you have a conflict and will be submitting late, if you talked to me after class please send me email as a reminder.

06 – class notes, 14 Sep 2017 — State Machines II

06 – class notes, 14 Sep 2017 — State Machines II

Switch/Case Statements

Switch/Case statements are useful for implementing state machines. There are two good exercises on the arduino site, Tutorial1 lays out a simple example and Tutorial2 shows how to use the “default:” case as “turn everything off”.

Here’s class responses for a “make a state machine” assignment I taught in the first Making Things Interactive, there are good examples of switch/case statements in these submissions.

for() Loops

for() loops are a way to iterate through a range of values and take an action on each iteration.  In my example (below) I use for() loops to pulse a light on and off.  In the arduino tutorial they use it for a “Knight Rider” effect of cycling through LEDs.

// -*-c++-*-
/*
  The MIT License (MIT)

  Copyright (c) 2017 J. Eric Townsend

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
*/



const int onOffPin = 2;
const int switch1Pin = 7;
const int switch2Pin = 8;

const int yellowLEDPin = 3;
const int redLEDPin = 5;
const int blueLEDPin = 6;

const int redOn = 1;
const int redOff = 0;
int redStatus = redOff;

const int blueOn = 1;
const int blueOff = 0;
int blueStatus = blueOff;

const int yellowOn = 1;
const int yellowOff = 0;
int yellowStatus = yellowOff;

const int noLED = 0;
const int redLED = 1;
const int blueLED = 2;
const int yellowLED = 3;

int activeLED = noLED;
int activeLEDPin = 0;

const int maxPWM = 255;
const int lowPWM = 0;

int pushCount = 0;

int currentPWM = 0;

void setup() {

  Serial.begin(115200);
  pinMode (onOffPin, INPUT);
  pinMode (switch1Pin, INPUT);
  pinMode (switch2Pin, INPUT);

  pinMode(yellowLEDPin, OUTPUT);
  pinMode(redLEDPin, OUTPUT);
  pinMode(blueLEDPin, OUTPUT);
}

void loop() {

  if (digitalRead(switch1Pin) == HIGH) {
    activeLED++;
    if (activeLED > yellowLED) {
      activeLED = noLED;
    }
  }
  if (digitalRead(onOffPin) == HIGH) {
    //    if (redStatus || blueStatus || yellowStatus) {
    if (activeLED != noLED)  {
      switch (activeLED) {
        case redLED:
          activeLEDPin = redLEDPin;
          break;
        case blueLED:
          activeLEDPin = blueLEDPin;
          break;
        case yellowLED:
          activeLEDPin = yellowLEDPin;
          break;
        default:
          Serial.println("WARNING");
          // if nothing else matches, do the default
          // default is optional
          break;
      }
      analogWrite(yellowLEDPin, lowPWM);
      analogWrite(blueLEDPin, lowPWM);
      analogWrite(redLEDPin, lowPWM);

      for (int v = lowPWM; v < maxPWM; v++) {
        delay(10);
        analogWrite(activeLEDPin, v);
        //          analogWrite(yellowLEDPin, v);
        //          analogWrite(blueLEDPin, v);
        //          analogWrite(redLEDPin, v);
      }

    }
  }
  else   {
    //
    //    for (int v = maxPWM; v > lowPWM; v--) {
    //      delay(10);
    //      analogWrite(yellowLEDPin, v);
    //      analogWrite(blueLEDPin, v);
    //      analogWrite(redLEDPin, v);
    //    }
    activeLED == noLED;
    analogWrite(yellowLEDPin, lowPWM);
    analogWrite(blueLEDPin, lowPWM);
    analogWrite(redLEDPin, lowPWM);
  }

}
// -*-c++-*-
/*
  The MIT License (MIT)

  Copyright (c) 2017 J. Eric Townsend

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
*/



const int onOffPin = 2;
const int switch1Pin = 7;
const int switch2Pin = 8;

const int yellowLEDPin = 3;
const int redLEDPin = 5;
const int blueLEDPin = 6;

const int redOn = 1;
const int redOff = 0;
int redStatus = redOff;

const int blueOn = 1;
const int blueOff = 0;
int blueStatus = blueOff;

const int yellowOn = 1;
const int yellowOff = 0;
int yellowStatus = yellowOff;

const int noLED = 0;
const int redLED = 1;
const int blueLED = 2;
const int yellowLED = 3;

int activeLED = noLED;
int activeLEDPin = 0;

const int maxPWM = 255;
const int lowPWM = 0;

int pushCount = 0;

int currentPWM = 0;

void setup() {

  Serial.begin(115200);
  pinMode (onOffPin, INPUT);
  pinMode (switch1Pin, INPUT);
  pinMode (switch2Pin, INPUT);

  pinMode(yellowLEDPin, OUTPUT);
  pinMode(redLEDPin, OUTPUT);
  pinMode(blueLEDPin, OUTPUT);
}

void loop() {

  if (digitalRead(switch1Pin) == HIGH) {
    activeLED++;
    if (activeLED > yellowLED) {
      activeLED = noLED;
    }
  }
  if (digitalRead(onOffPin) == HIGH) {
    //    if (redStatus || blueStatus || yellowStatus) {
    if (activeLED != noLED)  {

      if (activeLED == redLED) {
        activeLEDPin = redLEDPin;
      }
      else if (activeLED == blueLED) {
        activeLEDPin = blueLEDPin;
      }
      else if (activeLED == yellowLED) {
        activeLEDPin = yellowLEDPin;
      }
      else {
        Serial.println("WARNING");
      }
    }
    for (int v = lowPWM; v < maxPWM; v++) {
      delay(10);
      analogWrite(activeLEDPin, v);
      //          analogWrite(yellowLEDPin, v);
      //          analogWrite(blueLEDPin, v);
      //          analogWrite(redLEDPin, v);
    }

  }
}
else   {
  //
  //    for (int v = maxPWM; v > lowPWM; v--) {
  //      delay(10);
  //      analogWrite(yellowLEDPin, v);
  //      analogWrite(blueLEDPin, v);
  //      analogWrite(redLEDPin, v);
  //    }
  activeLED == noLED;
  analogWrite(yellowLEDPin, lowPWM);
  analogWrite(blueLEDPin, lowPWM);
  analogWrite(redLEDPin, lowPWM);
}

}

05 – class notes, 12 Sep 2017 — Intro to State Machines

05 – class notes, 12 Sep 2017 — Intro to State Machines

Posting code to the blog

Oops.  This didn’t make it in to my notes for class last Thursday.  One option for posting code to the blog is to use the “CodeColorer” plugin.  For your Arduino sketches switch to the Text tab and use the CodeColoer wrapper with lang=”C”

State Machines

State machines are graphs showing the relationships between various states of a system.  A light switch has a simple state machine of “on” and “off”; the transition happens by using the switch.

Here’s a state machine for a student’s MTI project from a few years ago.  It contains enough detailed information that I was able to OK the final project concept over email: MTI state machine.

Look around your environment for state machines and ask yourself, “How does this work?   How would I implement this?”

In class we looked at a simple state machine in my game that prevents cheating by keeping track of whether or not a button has been pressed and released.  Each player’s button has a state that is set to “YES” when the digitalRead() returns HIGH and set to “NO” when the digitalRead() returns LOW.

/*
The MIT License (MIT)

Copyright (c) 2014 J. Eric Townsend

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/


/*
logic_0

Some examples of logic to make a simple game using state machines.

fastest click. player A and player B click the button as
quickly as possible without holding it down. Light the LED
of the person who is ahead.

Every 25 clicks, start counting again to keep the players in
relative sync

*/


const int playerAPin = 2;
const int playerALed = 3;
int playerAPushed;
int playerACount;

const int playerBPin = 10;
const int playerBLed = 11;
int playerBPushed;
int playerBCount;

const int tieLed = 4;

const int rotateCount = 25;

// another way to use const variables is to define words that you'd
// use while talking about the sketch
const int YES = 1;
const int NO = 0;

void setup() {
pinMode(playerAPin, INPUT);
pinMode(playerALed, OUTPUT);

pinMode(playerBPin, INPUT);
pinMode(playerBLed, OUTPUT);

pinMode(tieLed, OUTPUT);
// make sure we start off with reasonable values, it's a game!
playerAPushed = 0;
playerACount = 0;

playerBPushed = 0;
playerBCount = 0;

Serial.begin(9600);
}

void loop(){

// if either player is over 25, reset them both to 0
if ((playerACount &gt; 25) || (playerBCount &gt; 25)) {
// "||" means "or", if ((this is true) or if (this other thing is true))
playerACount = 0;
playerBCount = 0;
}

// if they are pushing down the button, we need to see if
// they have let go of the button. We use the
// variable "playerAPushed" to keep track of previous state
// of the button
//
// this is the basic idea of a state engine in a few lines of
// code: "What was the previous state of playerA, and how
// will that change our decisions?"
if (digitalRead(playerAPin) == HIGH) {
// if playerAPushed is false, we know they have let
// go of the switch and count this as a new push
if (playerAPushed == NO) {
playerACount += 1;
playerAPushed = YES;
}
// else {
// they are holding down the button and trying to cheat!
// }
}
// but if they are not pressing the button, reset our state
else {
playerAPushed = NO;
}

// same for playerB
if (digitalRead(playerBPin) == HIGH) {
if (playerBPushed == NO) {
playerBCount += 1;
playerBPushed = YES;
}
}
else {
playerBPushed = NO;
}

// here is some code I wrote to help me debug things. I
// can comment it out and save it for future debugging.

// Serial.println("A count, pushed; B count, pushed");
// Serial.print(playerACount);
// Serial.print(" ");
// Serial.print(playerAPushed);
// Serial.print(", ");
// Serial.print(playerBCount);
// Serial.print(" ");
// Serial.println(playerBPushed);

if ((playerACount == 0) &amp;&amp; (playerBCount == 0)) {
digitalWrite(playerBLed, LOW);
digitalWrite(playerALed, LOW);
digitalWrite(tieLed, LOW);
}
else if (playerBCount &gt; playerACount) {
digitalWrite(playerBLed, HIGH);
digitalWrite(playerALed, LOW);
digitalWrite(tieLed, LOW);
}
else if (playerBCount &gt; playerACount) {
digitalWrite(playerBLed, LOW);
digitalWrite(playerALed, HIGH);
digitalWrite(tieLed, LOW);
}
else { // it's a tie!
digitalWrite(playerBLed, LOW);
digitalWrite(playerALed, LOW);
digitalWrite(tieLed, HIGH);
}
}

Feedback on Exercise, “Physical Computing as Foundation”

Hey ya’ll,

I just now finished reading all the submissions for this exercise and I’m really happy with your thinking and your writing.

Right now we’re in the fundamentals section of this class and learning the basics so we don’t have much room to discuss context or content.  In a few short weeks we will change our focus from “how does it work?” to “what does it mean?”  Then we can have in-class discussions about the spaces we are working in, our goals and challenges, and how we can help others realize their goals.

This is reflected in your first assignment — right now our goal is “make it work and not catch on fire, fall down, and sink into the swamp.”  In a few weeks our goal will be “make it interactive and express meaning within a context.”

 

–jet

Assignment 1: Analog and Digital Input and Output

The first assignment is a way for you to show that you understand the basics of digital and analog input and output and how to write a basic Arduino sketch.  It’s also an introduction to coming up with an idea, planning it, implementing it, then documenting your project.  There’s no content requirement, we’re saving that for later classes after everyone is good with the fundamentals of physical computing.

First, come up with an idea and a plan. “Use three switches to turn on three different LEDs and a photoresistor to change the intensity of a fourth LED.”  Based on that idea, create a circuit diagram, draw it on paper or use Fritzing.  Now build the circuit, write the code, and make it happen!

The requirements for this assignment are to take digital and analog input and create outputs for LEDs or the digital vibrators we handed out today in class.  You’re free to use other outputs or make your own switches, there’s also the collection of components in A10.

Document your entire process, from your first idea, the circuits that didn’t work, the changes you made, and what was the final result.   In your documentation, ask yourself what you learned, what went wrong, what you’d do differently if you did it a second time.

To submit your project, post a reply with the documentation.  Attach a zip file to that post containing your Arduino sketch, a photo of your finished breadboard circuit, and anything else you think is relevant.  I’m working on how we can post video to the blog, but for now you could post it to your AFS account and link to that from your submission.

Due 23:59, Monday.  Let me know ASAP if you have a conflict and will be submitting late, if you talked to me after class please send me email as a reminder.

04 – class notes, 7 Sep 2017 — Arduino Analog

04 – class notes, 7 Sep 2017 — Arduino Analog

Boring Bits

My office hours are noon to 3 this Friday (8 Sep) but I can come in at 11 if that works around a time conflict, just drop me email.

Arduino Documentation

The Arduino tutorial pages are a great resource.  In the Arduino IDE, go to File->Examples and you’ll find example sketches for, well, almost everything in Arduino.

Analog vs. Digital

In the previous class we talked about analog vs digital.  Digital only has to states: on or off, open or closed, or in the Arduino, 0 volts or 5 volts. We use pull-down resistors to force digital inputs to be 0 if they are not 5 volts.

Analog has a range between the states of digital.  A door can be half-open, a class can have final grades instead of being pass/fail, a party can range from quiet to loud depending on the people and the music.

Reading Analog Signals

Arduino Uno has six analog input pins labeled A0 to A5 that can read voltages between 0 and 5V.  Use analogRead() to read a pin, it will return a value between 0 (for no voltage) and 1023 (for around 5V).

The Arduino documentation has good circuit diagrams for setting up a potentiometer (or “pot”) and a photoresistor to be read on an analog input.  For now, only read analog signals powered by the Arduino 5V pin, outside signals might release the magic smoke from your Arduino.

To test your hardware, use the smallest sketch possible to read the analog input and write the information to the console:

// "const" is short for "constant" and means we can never change
// the value of dialPin. It's not something we would want to change,
// but making it "const" prevents us from doing it by accident

const int dialPin = 0;

void setup() {
pinMode(dialPin, INPUT);
// set up the serial console that we used in class
Serial.begin(115200);

}

void loop() {
lightValue = analogRead(dialPin);
Serial.println(lightValue);
}

Writing Analog

When we read analog we get values from 0 to 1023 from pins A0 thru A5, however when we write analog we can only write from 0 to 255 and we can only use certain pins for output, which pin varies by the type of Arduino board.

const int ledPin = 6;
int ledValue = 0;

void setup() {
pinMode (ledPin, OUTPUT);
}

void loop() {

ledValue = ledValue + 1;

// a good example of using an if statement.
// the max value for analogWrite is 255, so if ledValue
// is over 255, reset it to 0
if (ledValue &gt; 255) {
ledValue = 0;
}

analogWrite(ledPin, ledValue );

// the delay() statement can cause real problems in your actual script. While
// the delay is happening, the arduino does nothing, it's like hitting "pause"
// on a music player.
delay(20);
}