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 > 25) || (playerBCount > 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) && (playerBCount == 0)) {
digitalWrite(playerBLed, LOW);
digitalWrite(playerALed, LOW);
digitalWrite(tieLed, LOW);
}
else if (playerBCount > playerACount) {
digitalWrite(playerBLed, HIGH);
digitalWrite(playerALed, LOW);
digitalWrite(tieLed, LOW);
}
else if (playerBCount > playerACount) {
digitalWrite(playerBLed, LOW);
digitalWrite(playerALed, HIGH);
digitalWrite(tieLed, LOW);
}
else { // it's a tie!
digitalWrite(playerBLed, LOW);
digitalWrite(playerALed, LOW);
digitalWrite(tieLed, HIGH);
}
}

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

Exercise 2: Physical Computing as Foundation

First review the slides related to reaction and interaction.  (Slight correction: light switches can be interactive if they have the context of entertainment.)

Find and analyze an example of physical computing with a goal that is artistic or design based, less than a page and with links to the original.  It should only take 30-60min. Youtube is a great place to start, so are NYU ITP and IDeATe.  Due noon 7 September so I have time to review them before class.

If a project is an exercise or etude  for class it doesn’t qualify for this exercise.  What you’re looking for has to do something with interaction, preferably vaguely interesting, even if it’s just playing/entertainment.   Sandnoisedevice meets plenty of engineering requirements but it doesn’t hold long term interest as it’s more about the technology than the interaction experience or functionality.  If I could add something to this, it would a game where you generate music, something like Rez.  Or perhaps add a second device and use the two as consoles in a music competition game like DDR.

A sample format:

  1. basic description, elaborate on a title or use
  2. what is it supposed to do?
  3. how does it do this?
  4. does it work?
  5. how would you change it?

03 – class notes, 5 Sep 2017 — Arduino 1

03 – class notes, 5 Sep 2017 — Arduino 1

Boring Stuff

If you are required to go to the Tue/Thu presentations for School of Art, email me with the dates you’ll miss.  If it’s planned as a class lab day and there’s a presentation you want to attend, just let me know in advance.

School of Art party (7 Sep,2 017) is a class lecture/assignment day.  I’d advise attending class as we’re introducing analog inputs and outputs and the first real physcomp assignment.

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.

Class Notes

Introduction to Arduino and digital sketches.

simple digital schematic

The code we used in class with extra comments:

// -*-c++-*-
/*
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.
*/


/*
digital_in_0

read and display status of two switches
0 == LOW == off
1 == HIGH == on
*/


// a line that starts with "//"
/* or text that starts and ends with slash-star and star-slash */
// are called "comment" lines and are ignored by the Arduino

// First, we set up some "variables" that we will use to talk to the
// external hardware.

// A variable is a way of storing information in the Arduino
// that is easy for humans to understand.

// In this sketch we will also use a "const" variable. A const can never
// be changed in the program, but can be changed before you build
// a sketch. A "const var" is used for pin assignments and other things that
// rarely need to change, but it makes it easy to change them all in one
// line of code

// left switch on the PDF
const int switchPin = 2;
const int led1Pin = 3;

// right switch on the PDF
const int momentarySwitchPin = 4;
const int led2Pin = 5;

// now we start the sketch with setup()
// setup() only runs once. it's where we put all the code we only
// want to run once when the sketch starts.

// a pin can be either "INPUT" or "OUTPUT" and we define that in
// setup() because it will never change while the sketch is running
// (you'll see complex scripts on the Internet that do this in other
// places, but those are written by experienced or crazy Arduino people.)

void setup() {
pinMode (switchPin, INPUT);
pinMode (led1Pin, OUTPUT);

pinMode (momentarySwitchPin, INPUT);
pinMode (led2Pin, OUTPUT);
}

// loop() is where our sketch actually does something. The loop()
// repeats itself from start to finish as long as the Arduino is
// turned on.

void loop() {

// use digitalRead(pin) to find out if the input pins are on
// or off. We use the terms HIGH and LOW because we're
// actually measuring the amount of electricity reaching
// the pin, but for digital, consider it the same as ON and OFF.
// HIGH: pin is ON
// a switch is ON or we want turn ON a LED
// LOW: pin is off
// a switch is OFF or we want to turn OFF a LED

digitalWrite(led2Pin, LOW);

if (digitalRead(momentarySwitchPin) == HIGH) {
digitalWrite(led2Pin, HIGH);
}

if (digitalRead(switchPin) == HIGH) {
digitalWrite(led1Pin, HIGH);
}
else {
digitalWrite(led1Pin, LOW);
}

}