Recitation 2: Arduino Basics by Haiyan Zhang

September 20, 2019
Material

From Arduino Kit:

1 * Arduino Uno
1 * USB A to B cable
1 * breadboard
1 * buzzer
2 * LEDs
2 * 220 ohm resistors
2 * 10K ohm resistors
2 * pushbuttons
A handful of jumper cables

From cart:

2 * arcade buttons
1 * Multimeter (optional)

Circuit 1: Fade (tutorial)

*The schematic, diagram and description are from https://www.arduino.cc/en/Tutorial/Fade.

*The code is Arduino>File>Examples>03.Analog>Fading

Description:

This example demonstrates the use of the analogWrite() function in fading an LED off and on. AnalogWrite uses pulse width modulation (PWM), turning a digital pin on and off very quickly with different ratio between on and off, to create a fading effect.

Reflection:

This circuit isn’t complex. The dimming effect was obvious once we ran the code in Arduino.  The concept of PWM is new to me. And analogWrite’s working principle is based on PWM. As explained in the tutorial on Arduino’s official website, “brightness” is a variable used in the code. AnalogWrite() can change the PWM very fast, so at the end of the code delay is written to control the speed of the fading effect. 

Fade Schematic

Fade Circuit

  int led = 9; // the PWM pin the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by// the setup routine runs once when you press reset:
void setup() {
   // declare pin 9 to be an output:
   pinMode(led, OUTPUT);
}// the loop routine runs over and over again forever:
void loop() {
   // set the brightness of pin 9:
   analogWrite(led, brightness);   // change the brightness for next time through the loop:
   brightness = brightness + fadeAmount;   // reverse the direction of the fading at the ends of the fade:
   if (brightness <= 0 || brightness >= 255) {
   fadeAmount = -fadeAmount;
   }
   // wait for 30 milliseconds to see the dimming effect
   delay(30);
}

↑ Fade Arduino Code  

Circuit 2: toneMelody (tutorial)

*The schematic, diagram and code are from https://www.arduino.cc/en/Tutorial/toneMelody. 

*The code is  Arduino>File>Examples>02.Digital>toneMelody.

Description: 

“This example shows how to use the tone() command to generate notes. It plays a little melody you may have heard before.” 

“The code below uses an extra file, pitches.h. This file contains all the pitch values for typical notes. For example, NOTE_C4 is middle C. NOTE_FS4 is F sharp, and so forth. This note table was originally written by Brett Hagman, on whose work the tone() command was based. You may find it useful whenever you want to make musical notes.”

Reflection: 

The full name for the buzzer is “piezo buzzer”. The circuit only needs two hookup wires and doesn’t require any resistor. On Arduino official website, it says “Digital pin 13 is harder to use as a digital input than the other digital pins because it has an LED and resistor attached to it that are soldered to the board on most boards” (https://wp.nyu.edu/shanghai-ima-interaction-lab/category/recitations/). But obviously in the circuit, the buzzer is connected to Digital pin 8, so the internal resistor is not here to account for that. A Question remains to be solved !!! 

toneMelody Schematic

toneMelody Circuit

toneMelody Arduino Code→

  #include “pitches.h”

// notes in the melody:
int melody[] = {
NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
4, 8, 8, 4, 4, 4, 4, 4
};

void setup() {
// iterate over the notes of the melody:
   for (int thisNote = 0; thisNote < 8; thisNote++) {

     // to calculate the note duration, take one second divided by the note         type.
     //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
     int noteDuration = 1000 / noteDurations[thisNote];
     tone(8, melody[thisNote], noteDuration);

     // to distinguish the notes, set a minimum time between them.
     // the note’s duration + 30% seems to work well:
     int pauseBetweenNotes = noteDuration * 1.30;
     delay(pauseBetweenNotes);
     // stop the tone playing:
     noTone(8);
   }
}

void loop() {
   // no need to repeat the melody.
}

Circuit 3: Speed Game (tutorial)

*The diagram and code are from https://www.tinkercad.com/things/6MzvN5rlZlr-race-the-led-spring19

Description & Reflection:

This circuit is a 2-person game. It wasn’t really difficult to build the circuit. But it was very easy to mess it up since there were 16 hook-up wires. So I needed to be careful and check which two points one wire was supposed to connect. At first, I missed one wire, so when the code was running, only “player 1” was recorded. Later the problem was solved.  

Speed Game Circuit

breadboard*1 Arduino*1
buzzerpin*1 Led*2
resistor(220Ω)*2

placed next to Leds

resistor(10k)*2

placed next to pushbuttons

pushbutton*2 hook-up wire*16

 

Speed Game Schematic

Speed Game Arduino Code (read from left to right)↓

int buzzerPin = 8;
int button1 = 11;
int button2 = 10;
int led1 = 3;
int led2 = 2;int goal = 10;
int buttonState1 = LOW;
int previousState1 = LOW;
int buttonState2 = LOW;
int previousState2 = LOW;int counter1 = 0;
int counter2 = 0;
boolean winner1 = false;
boolean winner2 = false;// the follow variables are long’s because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickersvoid setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(buzzerPin, OUTPUT);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
Serial.println(“******************* RACE THE LED *******************”);
delay(1000);
Serial.println(“READY”);
delay(1000);
Serial.println(“SET”);
delay(1500);
Serial.println(“GO!!!!!!!!!!!!!!!!”);}void loop() {
// put your main code here, to run repeatedly:
buttonState1 = digitalRead(button1);
buttonState2 = digitalRead(button2);
//Serial.println(buttonState1);//this checks the times player 01 has pressed the button
if (counter1 < goal && winner2 == false) {
if (buttonState1 != previousState1 && millis() – time > debounce) {
if (buttonState1 == HIGH) {
counter1++;
Serial.print(“player 01: “);
Serial.println(counter1);
time = millis();
}
}
previousState1 = buttonState1;
if (counter1 == goal && winner2 == false) {
winner1 = true;
digitalWrite(led1, HIGH);
Serial.println(“PLAYER 01 WINS”);
playMelody();
}
}
//this checks the times player 02 has pressed the button

if (counter2 < goal && winner1 == false) {
if (buttonState2 != previousState2 && millis() – time > debounce) {
if (buttonState2 == HIGH) {
counter2++;
Serial.print(“player 02: “);
Serial.println(counter2);
time = millis();
}
}
previousState2 = buttonState2;
if (counter2 == goal && winner2 == false) {
winner2 = true;
digitalWrite(led2, HIGH);
Serial.println(“PLAYER 02 WINS”);
playMelody();
}
}

}

void playFreq(double freqHz, int durationMs) {
//Calculate the period in microseconds
int periodMicro = int((1 / freqHz) * 1000000);
int halfPeriod = periodMicro / 2;

//store start time
int startTime = millis();

//(millis() – startTime) is elapsed play time
while ((millis() – startTime) < durationMs) {
digitalWrite(buzzerPin, HIGH);
delayMicroseconds(halfPeriod);
digitalWrite(buzzerPin, LOW);
delayMicroseconds(halfPeriod);
}
}

void playMelody() {

playFreq(262, 100);
playFreq(294, 100);
playFreq(349, 100);
playFreq(294, 100);
playFreq(440, 200);
delay(100);
playFreq(440, 400);
playFreq(392, 400);
delay(300);
playFreq(262, 100);
playFreq(294, 100);
playFreq(349, 100);
playFreq(294, 100);
playFreq(392, 200);
delay(100);
playFreq(392, 400);
playFreq(349, 400);

}

Question 1:

Reflect how you use technology in your daily life and on the circuits you just built. Use the text Physical Computing and your own observations to define interaction.

 

I get so used to technology in my daily life that only when I went away from the urban circle and hid in the forest, would I notice the difference and realize how dependent I had been on technology. The word “manipulation”(sounds quite negative) came to my mind. Technology has been critical in terms of being the object (either tangible or intangible, visible or invisible) that serves human beings as the subject. Voice control lamp, water heater, central air conditioning – undoubtedly, it brings convenience. But on the other hand, my dependence on these items and the way my life has been changing and orienting around them are also the evidence for the conversation that happened the other way around. These changes can be positive, negative or rather neutral/ balanced of both sides. Therefore, rather than the word “manipulation”, “interaction” would be a better fit to describe this phenomenon between technology and human beings. 

I like the word ‘relationship’ in the text Physical Computing. When giving an explanation about “Intelligence Amplification”, it says “Rather than trying to imitate the autonomy of human beings, we want to support it”. and supports a human being’s autonomy by capturing and conveying a person’s expression. It counts on human beings to make the most interesting relationships and supports a human being’s autonomy by capturing and conveying a person’s expression (p. xviii, “Introduction”).  In a particular situation like a smartphone, ideas from designers are communicated to the user through the smartphone. Relationships are built between technology and human beings. More importantly, relationships are built between human beings through technology. “Interaction” is the concrete content of these relationships. It varies in forms (such as “listening, thinking and speaking”) and is actually composed of these variations (or put in other words, an interactive process is composed of these variations) (p. xx). 

A better definition of “interaction” then consists of two parts – its process/forms and its purpose. In Physical Computing, Chris Crawford’s definition is concise and emphasizes on the process and its actors. To me, moreover, “interaction” is the process that conveys ideas. Eventually, it is the relationship between human beings rather than technology and human beings. 

Question 2:

Why did we use the 10K resistor with the push button?

To regulate the current in the circuit and avoid blowing the push button. But I don’t know why we don’t use 220 ohm. Another question!

Question 3:

If you have 100,000 LEDs of any brightness and color at your disposal, what would you make and where would you put it?

 

(1)I will make an interactive painting made of LEDs. Square shape, approximately 30×30 cm. Over the LED-matrix is a piece of transparent flesh-color material. It reacts to the distance between the piece and the watcher and changes color and brightness so as to form different pictures.

(2)A very simple piece of stagecraft. I will install it for theatrical productions.

(3)A huge LED-assembled bulb. Put in the neighborhoods in the arctic circle, it will change its brightness and color according to time and need.

References

Fade tutorial https://www.arduino.cc/en/Tutorial/Fade

toneMelody tutorial https://www.arduino.cc/en/Tutorial/toneMelody

Speed game tutorial https://www.tinkercad.com/things/6MzvN5rlZlr-race-the-led-spring19

Introduction in Physical Computing  by Igoe and O’Sullivan 

One Reply to “Recitation 2: Arduino Basics by Haiyan Zhang”

Leave a Reply