10 Rules For Students and Teachers From John Cage

John Cage, 10 Rules for Students and Teachers

RULE ONE: Find a place you trust, and then try trusting it for a while.

RULE TWO: General duties of a student: pull everything out of your teacher ; pull everything out of your fellow students.

RULE THREE: General duties of a teacher: pull everything out of your students.

RULE FOUR: Consider everything an experiment.

RULE FIVE: Be self-disciplined: this means finding someone wise or smart and choosing to follow them. To be disciplined is to follow in a good way. To be self-disciplined is to follow in a better way.

RULE SIX: Nothing is a mistake. There’s no win and no fail, there’s only make.

RULE SEVEN: The only rule is work. If you want it to lead to something. It’s the people who do all of the work.

RULE EIGHT: Do not try to create and analyze at the same time. They’re different processes.

RULE NINE: Be happy whenever you can manage it. Enjoy yourself. It’s lighter than you think.

RULES: We’re breaking all the rules. Even our own rules. And how do we do that? By leaving plenty of room for X quantities.

HINTS: Always be around. Come or go to everything. Always go to classes. Read anything you can on your hands. Look at movies carefully, often. Save everything. It might come in handy later.

Class Notes, 30 Jan 2018

Preparation for a couple of weeks of p5.js

BRING YOUR LAPTOP POWER SUPPLY TO CLASS

’nuff said.

Install software

Install p5.js

Install node.js

Find a text editor

Software development requires you use an editor that supports plain text (ASCII and UNICODE) and doesn’t do goofy formatting things with HTML.  Most (all?) software development editors will handle JavaScript.  Note that “which editor is best?” is what we refer to as “an underwear argument.” This is when adults waste countless hours arguing about briefs, boxers, boyshorts, thongs, hipsters, and going commando.

Open source options include:

  • emacs — steep learning curve but worth the effort.  This is my editor for all programming languages including TeX (a markup language popular for academic papers).
  • atom — like emacs but for people who are younger than emacs. Lots of interesting ways to customize for other applications. For example, it’s my Python editor when I’m using the Rhino CAD software and writing a Python script to modify models.
  • sublime — like atom but different.  Very popular on the Mac as a replacement for BBEdit, predates atom.

There are plenty of commercial options, if you already use BBEdit (equiv) or XCode that should be fine.

Become Shiffman’s BFF and crash learn p5.js

A very detailed history of p5.js.  This will answer a lot of  what/why questions.

Since we just did arrays, here is how they work in p5.js.

Later in the semester we will look at “scraping” data from the interweb to use in class project, here’s one way we can do this.

Review the p5.js reference

bookmark this page

 

Assignment 2

This project is supposed to be an embodiment of Steve Reich’s Clapping Music. The code for this project has two versions. The first version works unreliably because it uses the delay function to time the LEDs. The second uses a helper function to time and illuminate the LEDs using millis(). This program does not even finish compiling though. It probably just needs debugging, but I am not sure. With the original program, the interrupt does not reliably switch the case. To continue this project, I will use solenoids to make a percussive sound.

 

vandezandeStateMachineInterrupt

 

Assignment 2: LED Color Picker

The circuit shown above is a 3 pin led connected to a joystick. It uses the JoyStick movement to change the RGB values and the button press in the joystick is used to switch between states. The system has 5 states

  1. All off
  2.  Red constant, change G and B by moving the joystick
  3.  Green constant, change R and G by moving the joystick
  4. Blue constant, change others through joystick
  5. All on.

Reflection:  if I were to redo this, I would transfer the states from 2, 3 and 4 making it a real color picker, and I also think the joystick button may not be ideal for an interrupt.

Code and Files: A2

Assignment Two

Hot/Cold

I wanted to build upon and improve the previous assignment. The goal is to balance at a certain distance in order to turn on the LED, but done through a classic game of hot or cold. While it’s rather trivial, it has helped me to better understand the ultrasonic sensor and its possibilities. refining the sensor data and perfecting the timing is something that I’d like to explore further down the road.

The states are used in order to guide you through the game. State one lets you now that the game is ready, state two sets a random number for you to find using the ultrasonic sensor, and in state three…you find the number. Prompted in the serial monitor if you are getting warmer or colder.

Code:
//--------NEOPIXEL SETUP --------
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
static const int PIN = 40;
static const int NUMPIXELS = 1;
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRBW + NEO_KHZ800);

//--------ULTRASONIC PINS--------
static const int trigPin = A0;
static const int echoPin = A1;

//--------LIGHTS--------
static const int ledPin = 4;

//--------SWITCH PIN--------
static const int switchPin = 3;

//------------------------------------------------------------------------------------

//--------Global variables--------

const bool isInterrupt = true;

// button state
int buttonState = 0; // current state of the button

// Target
int target;

// Time
unsigned long lastSampleTime = 0;
unsigned long sampleInterval = 5000; // in ms

unsigned long lastSampleTime2 = 0;
unsigned long sampleInterval2 = 250; // in ms

unsigned long lastSampleTime3 = 0;
unsigned long sampleInterval3 = 500; // in ms

// Ultrasonic
long duration;
int distance;

//------------------------------------------------------------------------------------

void SwitchPressed()
{
 if (digitalRead(switchPin) == HIGH) {
 // this is why people hate C
 buttonState == 3 ? buttonState = 0 : buttonState++;
 }
 // Serial.println(buttonState);
}

//------------------------------------------------------------------------------------

void setup() {

Serial.begin(9600);

pinMode(ledPin, OUTPUT);
 pinMode(switchPin, INPUT);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);

// attach the interrupt pin to a method
 if (isInterrupt) {
 attachInterrupt (digitalPinToInterrupt (switchPin), SwitchPressed, CHANGE);
 }
 randomSeed(analogRead(2));
 pixels.begin(); // This initializes the NeoPixel library.
}

void loop() {
 unsigned long now = millis();

switch (buttonState) {
 case 0:
 pixelBlinkGreen();
 Serial.println ("Ready To Start Game");
 break;
 case 1:
 if (lastSampleTime + sampleInterval < now) {
 lastSampleTime = now;
 target = random(1, 150);
 Serial.print("Target Set");
 }
 pixels.setPixelColor(0, pixels.Color(0, 255, 0));
 break;
 case 2:
 pixels.setPixelColor(0, pixels.Color(255, 200, 0));
 // Serial.print("Find ");
 // Serial.println(target);
 hotCold(target);
 break;
 default:
 pixelBlinkYellow();
 Serial.println("Push Button For New Game");
 // statements
 }
}
/*
 if (buttonState == 0) {
 pixelBlinkGreen();
 Serial.println ("GREEN BLINK");
 }
 else if (buttonState == 1) {
 if (lastSampleTime + sampleInterval < now) {
 lastSampleTime = now;
 target = random(1, 150);
 Serial.print("Target = ");
 Serial.println(target);
 }
 pixelBlinkYellow();
 }
 else if (buttonState == 2) {
 pixels.setPixelColor(0, pixels.Color(255, 200, 0));
 // Serial.print("Find ");
 // Serial.println(target);
 hotCold(target);
 }
 }
*/


//------------------------------------------------------------------------------------

void hotCold(int x) {
 digitalWrite(trigPin, LOW);
 delayMicroseconds(2);
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10);
 digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);
 distance = duration / 10 / 2 ;

unsigned long now2 = millis();
 if (lastSampleTime2 + sampleInterval2 < now2) {
 lastSampleTime2 = now2;
 if ((distance <= (target + 5)) && (distance >= (target - 5))) {
 Serial.println("ON FIRE!");
 ledOn();
 pixelBlinkPink();
 } else if ((distance <= (target + 10)) && (distance >= (target - 10))) {
 Serial.println("Things are heating up");
 ledOff();
 } else if ((distance <= (target + 20)) && (distance >= (target - 20))) {
 Serial.println("Warmer...");
 ledOff();
 } else if ((distance <= (target + 30)) && (distance >= (target - 30))) {
 Serial.println("Cooler...");
 ledOff();
 } else if ((distance <= (target + 40)) && (distance >= (target - 40))) {
 Serial.println("Pretty Cold");
 ledOff();
 } else if ((distance <= (target + 50)) && (distance >= (target - 50))) {
 Serial.println("Ice Cold, Elsa");
 ledOff();
 } else {
 ledOff();
 //Serial.println("Something is wrong");
 }
 }
}

void pixelBlinkGreen() {
 pixels.setPixelColor(0, pixels.Color(0, 255, 0));
 pixels.show(); // This sends the updated pixel color to the hardware.
 delay(500);
 pixels.setPixelColor(0, pixels.Color(0, 0, 0));
 pixels.show(); // This sends the updated pixel color to the hardware.
 delay(500);
}

void pixelBlinkYellow() {
 pixels.setPixelColor(0, pixels.Color(255, 200, 0, 75));
 pixels.show(); // This sends the updated pixel color to the hardware.
 delay(250);
 pixels.setPixelColor(0, pixels.Color(0, 0, 0));
 pixels.show(); // This sends the updated pixel color to the hardware.
 delay(250);
}

void pixelBlinkWhite() {
 pixels.setPixelColor(0, pixels.Color(255, 255, 255));
 pixels.show(); // This sends the updated pixel color to the hardware.
 delay(300);
 pixels.setPixelColor(0, pixels.Color(0, 0, 0));
 pixels.show(); // This sends the updated pixel color to the hardware.
 delay(250);
}

void pixelBlinkPink() {
 pixels.setPixelColor(0, pixels.Color(254, 6, 141));
 pixels.show(); // This sends the updated pixel color to the hardware.
 delay(300);
 pixels.setPixelColor(0, pixels.Color(0, 0, 0));
 pixels.show(); // This sends the updated pixel color to the hardware.
 delay(250);
}

void pixelOff() {
 pixels.setPixelColor(0, pixels.Color(0, 0, 0));
 pixels.show(); // This sends the updated pixel color to the hardware.
}

void ledOn() {
 digitalWrite(ledPin, HIGH);
}

void ledOff() {
 digitalWrite(ledPin, LOW);
}
Files:

Fritzing & Arduino Files

Assignment 2

Goal: The goal for my state machine was to build a basic alert system that tells you when there is someone/something in the proximity. The switch is the state change that turns the entire alert system on or off. This is essential for the edge case when you are expecting many people and do not need to be alerted. There were three LED colors: yellow for idle (nothing in the proximity), red for something physically there and it is light out, and green for something physically there and it is dark out.

Reflection: If I were to update/re-do this, I would change the code so the interrupt is the proximity sensor rather than the switch. I would also change the light so the green represents light and proximity and red represents dark and proximity. This would align with my overarching ideal goal to make a front door security system. I would also change the light/dark reading to a boolean.

(My unstable hand is to blame for the flickering green light)


 // Define constant values.

// The wiring assignment.
#include

static const int SWITCH_PIN = 3;
static const int LED1Pin = 4;
static const int LED2Pin = 5;
static const int LED3Pin = 13;
static const int lightSensor = A0;
bool LEDState = false;
static const int sampleSize = 16;
unsigned int readings[sampleSize];
unsigned int sampleIndex = 0;
static const int nighttime = 50;
const int TRIG_PIN = 9;
const int ECHO_PIN = 8;
const int MAX_DISTANCE = 700;

NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);

void SwitchPressed()
{
if (digitalRead(SWITCH_PIN) == 1) {
LEDState = true;
}
else{
LEDState = false;
}
}

// ================================================================================
// Configure the hardware once after booting up. This runs once after pressing
// reset or powering up the board.

void setup()
{
// Initialize the serial UART at 9600 bits per second.

Serial.begin(115200);

pinMode(LED_BUILTIN, OUTPUT);

pinMode(SWITCH_PIN, INPUT);

pinMode(LED1Pin, OUTPUT);

pinMode(LED2Pin, OUTPUT);

pinMode(LED3Pin, OUTPUT);

pinMode(lightSensor, INPUT);

pinMode(TRIG_PIN, OUTPUT);

digitalWrite(TRIG_PIN, LOW);

// Initialize the echo pin for input.
pinMode(ECHO_PIN, INPUT);

attachInterrupt (digitalPinToInterrupt (SWITCH_PIN), SwitchPressed, CHANGE);

}

// ================================================================================
// Run one iteration of the main event loop. The Arduino system will call this
// function over and over forever.

void loop()
{

int photo = analogRead(lightSensor);

readings[sampleIndex] = photo;

if (sampleIndex < (sampleSize -1)) {
sampleIndex++;
}
else {
sampleIndex = 0;
}

int median = 0;
int low = 1024;
int high = 0;
int mean = 0;
for (int i = 0; i < sampleSize; i++) {
mean += readings[i];
if (readings[i] < low) { low = readings[i]; } if (readings[i] > high) {
high = readings[i];
}
}
mean = mean / sampleSize;
bubbleSort();
median = readings[sampleSize / 2];

//TURNED ON, IDLE STATE

if (LEDState && sonar.ping_median() < 700 && mean > nighttime ) {
digitalWrite(LED1Pin, 255);
digitalWrite(LED2Pin, 0);
digitalWrite(LED3Pin, 0);
}

else if (LEDState && sonar.ping_median() < 700 && mean < nighttime ) {
digitalWrite(LED1Pin, 0);
digitalWrite(LED2Pin, 0);
digitalWrite(LED3Pin, 255);
}

//TURNED OFF

else if (!LEDState) {
digitalWrite(LED2Pin, 0);
digitalWrite(LED3Pin, 0);
digitalWrite(LED1Pin, 0);
}

else {
digitalWrite(LED2Pin, 255);
digitalWrite(LED3Pin, 0);
digitalWrite(LED1Pin, 0);
}

delay(500);
Serial.println("mean:");
Serial.println(mean);
Serial.println("median:");
Serial.println(sonar.ping_median());
}

// tigoe's sort for an array
void bubbleSort() {
int out, in, swapper;
for(out=0 ; out < sampleSize; out++) { // outer loop
for(in=out; in<(sampleSize-1); in++) { // inner loop if( readings[in] > readings[in+1] ) { // out of order?
// swap them:
swapper = readings[in];
readings [in] = readings[in+1];
readings[in+1] = swapper;
}
}
}
}

 

Assignment2 – Knock Interface

Imagine your desks, doors, and floors turning into an interface that responds to knocks. What if you could control room lighting with the silent knocks? What if you could customize the knock interaction for each object? In this assignment, I built an interactive system that is able to detect and record knock patterns. Used a piezo sensor to detect the knock vibrations and a button to trigger interrupt to switch 2 different modes – detect and record. Knock detection result is displayed on a console.

The system consists of 6 states in total – wait / read / output for detect mode and wait / read / record for record mode.

Demo:

Code and fritzing here

Assignment 2: State Machines and Interrupts

Assignment 2: State Machines and Interrupts

Due 23:59, 29 January 2018.  We will discuss in class on the 30th and suggest improvements or analyze different ways to resolve your problem.

Requirements

State a question or process you would like to solve. (Ex: “control the lights in a room based on a door being opened and curtains being opened or closed.”)  Create a physical interaction using a state machine driven by inputs and interrupts that collects data, interprets the data, then displays a result via console, LEDs, or other output devices. Speakers are acceptable but must be executed in a useful manner, kinematic solutions are also welcome.

The state machine should have at least three states not including on and off states.  Declare whether your states have an order or if any state can transition to another state.

Post a short video showing the various states, I suggest vimeo or youtube.   For your Arduino and Fritzing sketches, make a zip file or gzip/tar bundled and post that to the blog.

Class Notes, 23 Jan 2018

First off, if you’re on a Mac and seeing the error about “Disk Not Ejected Properly”, I posted a fix for this last Fall.

Second, the problem I had with my Metro was apparently a short.  When I got back to my studio I took out all the components and my Metro worked fine.  I replaced the components one by one, doing a new build and upload on each component.  By the time I was finished, it all worked correctly.

Magic.

Interrupts

Interrupts are a part of our daily life.  The best example is a door bell.  You have two options:

1) Go to the door once a minute and see if anyone is there.

2) Install a door bell.  When someone comes to the door they push the bell and you are notified.  You can choose whether or not to respond to the door bell.

Almost every input to a computing device is based on interrupts:

  • buttons on your mobile
  • game controllers
  • controls on a microwave or stove

Take a look at the attached Arduino and Fritzing sketches.  We will continue to discuss interrupts as well as state machines on Thursday.  I’ll email the reading assignment in a few, please read it and be prepared for a short discussion on potential interaction methods.

class-3 updated 25 Jan with more examples of interrupts