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.

Project 2 State Machines, Switch/Case, and For () Loops

For this project I utilized my previous configuration and implemented for () loops, switch/case, and a state machine to meet the requirements of this project. My previous code was all about 5 inputs that would turn on a light if pressed in the right order as well as an analog reading from the photo resistor. I proceeded to remove one of the 5 buttons as well as the photo resistor and work from the left over 4 inputs A-D. Since I am new to Arduino coding I did a very simple configuration in which each of the 4 buttons would make the LED behave differently. InputA would make the LED flash rapidly, InputB would make the LED pulse slower, InputC would make the LED fade from off to on, and InputC would make the LED fade to off. I practiced by trying to figure out the statement to fade on and once figured out reversed that to have it fade off and then copy pasted that into my for()loop. The rest of the commands were pretty simple.

Link to video- https://youtu.be/lUm5E2ytEoQ

Project2-20170920T170753Z-001

Assignment 2 – Elton Spektor

My idea for the project came from my prior assignment. I adapted my first assignment to use a potentiometer to control different states of my circuit. First, I needed to find the analogRead range of the potentiometer on the Arduino and then I used that to create 4 different states of LED flashes. As I change the potentiometer’s knob, the LEDs would flash in different sequences. The problem I ran into was trying to use a digital pin with PWM to read the analog input of the potentiometer. It did not work well probably because it is not designed for that. Using an analog pin, it worked well and the coding went smoothly.

Video: https://youtu.be/8wO9_W6Jvp0

Assignment 2 Code

Circuit Drawing Assignment 2

 

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);
}
}