for some reason this one’s video dropped in quality and I have no idea why it happened…
codes (first one is 8, second is 13)
/*
Analog Input
Demonstrates analog input by reading an analog sensor on analog pin 0 and
turning on and off a light emitting diode(LED) connected to digital pin 13.
The amount of time the LED will be on and off depends on
the value obtained by analogRead().
The circuit:
* Potentiometer attached to analog input 0
* center pin of the potentiometer to the analog pin
* one side pin (either one) to ground
* the other side pin to +5V
* LED anode (long leg) attached to digital output 13
* LED cathode (short leg) attached to ground
* Note: because most Arduinos have a built-in LED attached
to pin 13 on the board, the LED is optional.
Created by David Cuartielles
Modified 16 Jun 2009
By Tom Igoe
http://arduino.cc/en/Tutorial/AnalogInput
*/
int sensorPin = 0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// turn the ledPin on
digitalWrite(ledPin, HIGH);
// stop the program for milliseconds:
delay(sensorValue);
// turn the ledPin off:
digitalWrite(ledPin, LOW);
// stop the program for for milliseconds:
delay(sensorValue);
}
/*
* Force Sensitive Resistor Test Code
*
* The intensity of the LED will vary with the amount of pressure on the sensor
*/
int sensePin = 2; // the pin the FSR is attached to
int ledPin = 9; // the pin the LED is attached to (use one capable of PWM)
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT
}
void loop() {
int value = analogRead(sensePin) / 4; //the voltage on the pin divded by 4 (to scale from 10 bits (0-1024) to 8 (0-255)
analogWrite(ledPin, value); //sets the LEDs intensity proportional to the pressure on the sensor
Serial.println(value); //print the value to the debug window
}
Please ignore the random chatter in the background of the video but mostly ignore my incredibly creepy laugh
/*
Button
Turns on and off a light emitting diode(LED) connected to digital
pin 13, when pressing a pushbutton attached to pin 7.
The circuit:
* LED attached from pin 13 to ground
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground
* Note: on most Arduinos there is already an LED on the board
attached to pin 13.
created 2005
by DojoDave
modified 17 Jun 2009
by Tom Igoe
http://www.arduino.cc/en/Tutorial/Button
*/
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
or
int ledPin = 13; // choose the pin for the LED
int inputPin1 = 3; // button 1
int inputPin2 = 2; // button 2
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin1, INPUT); // make button 1 an input
pinMode(inputPin2, INPUT); // make button 2 an input
}
void loop(){
if (digitalRead(inputPin1) == LOW) {
digitalWrite(ledPin, LOW); // turn LED OFF
} else if (digitalRead(inputPin2) == LOW) {
digitalWrite(ledPin, HIGH); // turn LED ON
}
}
Rather than wiring up the given diagrams for sensors from the book, I made a circuit using something I hadn’t used before. I made a simple circuit which plays tones based on a value measured from a proximity sensor.
Here is the video:
Here is the fritzing diagram:
Here is the code:
/*
* Proximity instrument
* This is totally not anything like a theramin.
* Author: Matthew Kellogg
* Date: October 22, 2014
*/
int ledPin = 5; // LED connected to digital pin 9
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
static const int thresh = 160;
void loop() {
int val = analogRead(0);
if (val > thresh){
analogWrite(ledPin, map(val, 30, 950, 0, 255));
tone(11, map(val, thresh, 950, 60, 4400));
} else {
analogWrite(ledPin, 0);
noTone(11);
}
}
So back in the Vietnam War, US prisoners of war would be isolated from each other in solitary cells, where verbal communication was impossible. To make up for this, prisoners used Tap Code, an ancient Greek invention. Tap code is based off of a 5×5 table of the Latin alphabet, as shown below:
One prisoner would knock a certain number of times to determine the row, pause, and then knock a certain number of times to determine the column of the letter they were describing. The listening prisoner would mark down the two numbers, and determine which letter was being knocked out by checking the table (usually mentally). So, for example, 1 tap then 1 tap would be A, 4 taps then 2 taps would be R, and so on.
I decided to create a listening prisoner, for whom I could tap messages, and they would print them out in a console. To better visualize this process, I created an animated tap table which would show the letter being tapped. Certainly, this makes the process much easier than it was the prisoners of war in Vietnam, but I lack the time or necessity to become fluent. Here it is in action:
To complete this, I used a piezo, a vibration sensor, to detect my taps on the table. Here is my circuit (real bare bones stuff):
In Fritzing:
My Arduino code:
const int knockSensor = A0;
const int threshold = 70;
const int knockTimeMaxThreshhold = 1000;
const int knockTimeMinThreshhold = 100;
int sensorReading = 0;
int knockNum = 0;
long knockTime = 0;
int col = 0;
int row = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
sensorReading = analogRead(knockSensor);
if (millis() - knockTime > knockTimeMaxThreshhold)
{
if (knockNum == 1)
{
knockNum = 2;
knockTime = millis();
}
else if (knockNum == 2)
{
Serial.print("A");
Serial.println(100);
knockNum = 0;
col = 0;
row = 0;
}
}
if (sensorReading >= threshold && millis()-knockTime > knockTimeMinThreshhold)
{
knockTime = millis();
if (knockNum == 0)
{
knockNum = 1;
}
if (knockNum == 2)
{
row++;
Serial.print("B");
Serial.println(row);
}
else
{
col++;
Serial.print("A");
Serial.println(col);
}
}
delay(1);
}
My Processing code:
// This Processing program reads serial data for two sensors,
// presumably digitized by and transmitted from an Arduino.
// It displays two rectangles whose widths are proportional
// to the values 0-1023 received from the Arduino.
// Import the Serial library and create a Serial port handler
import processing.serial.*;
Serial myPort;
// Hey you! Use these variables to do something interesting.
// If you captured them with analog sensors on the arduino,
// They're probably in the range from 0 ... 1023:
int valueA; // Sensor Value A
int valueB; // Sensor Value B
//------------------------------------
void setup() {
size(200, 200);
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
serialChars = new ArrayList();
}
void draw() {
int prevValA = valueA;
processSerial();
if (valueA == 100){
print(tapTable(valueB-1, prevValA-1));
valueA = 0;
valueB = 0;
}
for(int i = 0; i < 5; i++)
{
float iw = width*i/5;
float ih = height*i/5;
line(iw,0,iw,height);
line(0,ih,width,ih);
for(int j = 0; j < 5; j++)
{
color c = color(0,0,0);
if (valueA - 1 == j && valueB - 1 == i)
{
c = color(255,0,0);
}
float wOffset = width/10-5;
float hOffset = height/10+3;
float jh = height*j/5;
fill(c);
text(tapTable(i,j),iw+wOffset,jh+hOffset);
}
}
}
char tapTable(int col, int row)
{
char[][] table = {{'A','B','C','D','E'},
{'F','G','H','I','J'},
{'L','M','N','O','P'},
{'Q','R','S','T','U'},
{'V','W','X','Y','Z'}};
return table[constrain(row,0,4)][constrain(col,0,4)];
}
ArrayList serialChars; // Temporary storage for received serial data
int whichValueToAccum = 0; // Which piece of data am I currently collecting?
boolean bJustBuilt = false; // Did I just finish collecting a datum?
void processSerial() {
while (myPort.available () > 0) {
char aChar = (char) myPort.read();
// You'll need to add a block like one of these
// if you want to add a 3rd sensor:
if (aChar == 'A') {
bJustBuilt = false;
whichValueToAccum = 0;
} else if (aChar == 'B') {
bJustBuilt = false;
whichValueToAccum = 1;
} else if (((aChar == 13) || (aChar == 10)) && (!bJustBuilt)) {
// If we just received a return or newline character, build the number:
int accum = 0;
int nChars = serialChars.size();
for (int i=0; i < nChars; i++) {
int n = (nChars - i) - 1;
int aDigit = ((Integer)(serialChars.get(i))).intValue();
accum += aDigit * (int)(pow(10, n));
}
// Set the global variable to the number we captured.
// You'll need to add another block like one of these
// if you want to add a 3rd sensor:
if (whichValueToAccum == 0) {
valueA = accum;
// println ("A = " + valueA);
} else if (whichValueToAccum == 1) {
valueB = accum;
// println ("B = " + valueB);
}
// Now clear the accumulator
serialChars.clear();
bJustBuilt = true;
} else if ((aChar >= 48) && (aChar <= 57)) {
// If the char is between '0' and '9', save it.
int aDigit = (int)(aChar - '0');
serialChars.add(aDigit);
}
}
}
I wanted to make a spooky game for Halloween. So I did.
This is a game where one’s goal is to run away from the ghost, the scariest series of ellipses ever. The game itself runs in Processing and interfaces with Arduino. The controller consists of a circuit which has two force sensitive resistors that are used to control the player character’s legs. (The circuit’s box, which is not unlike the casing of a mummy, was lovingly crafted from tape, cardboard, and a box of Ritz Crackers.) Initially, I was planning to have more types of enemies to run away from. It would be fun if the player character and enemies cycled, so that whoever you were were caught by last is who you play as next. It would also be interesting to implement different movement types, such as jumping, standing, and different walking/running speeds, so that each enemy has a different type of action that is best suited for a successful escape. As for minor changes to the existing game, it would be more visually pleasing if the player character had arms, knees, and a more worried expression (the latter of which could be accomplished by the inclusion of eyebrows). I’d say I spent my time 1/6th on the circuit, 1/6th on the circuit’s box, 2/6ths on the game’s mechanic, and 2/6ths on the visual look. (That’s 2/6ths physically making things and 4/6ths programming things. This does not take into account how long documentation took.)
Oh, and be sure to scroll down to the end of the code. Happy Halloween!
//Miranda Jacoby
//EMS Interactivity Section 4
//majacoby@andrew.cmu.edu
//Copyright Miranda Jacoby 2014
//Code for interfacing with Arduino provided by Golan Levin
//Big thanks to Matt for helping me
//figure out how to implement the counter
// This Processing program reads serial data for two sensors,
// presumably digitized by and transmitted from an Arduino.
// It displays two rectangles whose widths are proportional
// to the values 0-1023 received from the Arduino.
// Import the Serial library and create a Serial port handler
import processing.serial.*;
Serial myPort;
PImage gameover;
// Hey you! Use these variables to do something interesting.
// If you captured them with analog sensors on the arduino,
// They're probably in the range from 0 ... 1023:
int valueA; // Sensor Value A
int valueB; // Sensor Value B
int legPosy1;//70
int legPosy2;//70
int legPosx1;
int legPosx2;
//Gradient Variables
int Y_AXIS = 1;
color b1, b2;
color ghostCol = color(245, 249, 247);
int i = 0;
int j = 0;
int h = 150;
int toothWidth = 20;
boolean leftDown = true;
int runCounter = 50;
//------------------------------------
void setup() {
size(800, 600);
gameover = loadImage("scare.tif");
b1 = color(27, 73, 85);//color(0, 102, 153);
b2 = color(104, 20, 0);
// List my available serial ports
int nPorts = Serial.list().length;
for (int i=0; i < nPorts; i++) {
println("Port " + i + ": " + Serial.list()[i]);
}
// Choose which serial port to fetch data from.
// IMPORTANT: This depends on your computer!!!
// Read the list of ports printed by the code above,
// and try choosing the one like /dev/cu.usbmodem1411
// On my laptop, I'm using port #4, but yours may differ.
String portName = Serial.list()[5];
myPort = new Serial(this, portName, 9600);
serialChars = new ArrayList();
}
//------------------------------------
void draw() {
// Process the serial data. This acquires freshest values.
processSerial();
//A is left leg, B is right leg
valueA = (int)map(valueA, 0, 1023, 0, 100);
valueB = (int)map(valueB, 0, 1023, 0, 100);
legSwitch();
println(runCounter);
setGradient(0, 0, width, height, b1, b2, Y_AXIS);
drawScenery();
drawEnemy();
drawHill();
drawPlayer(valueA, valueB);
if (runCounter < 0){ image(gameover, 0, 0); } // draw a pink rectangle displaying valueA: //fill (255, 200, 200); //rect (0, 0, valueA, 100); // draw a blue rectangle displaying valueB: //fill (200, 200, 255); //rect (0, 100, valueB, 100); //fill (0); // draw the letters A and B: //text ("A", 20, 60); //text ("B", 20, 160); //println("A "+ valueA); //println("B "+ valueB); } //--------------------------------------------------------------- // The processSerial() function acquires serial data byte-by-byte, // as it is received, and when it is properly captured, modifies // the appropriate global variable. // You won't have to change anything unless you want to add additional sensors. /* The (expected) received serial data should look something like this: A903 B412 A900 B409 A898 B406 A895 B404 A893 B404 ...etcetera. */ ArrayList serialChars; // Temporary storage for received serial data int whichValueToAccum = 0; // Which piece of data am I currently collecting? boolean bJustBuilt = false; // Did I just finish collecting a datum? void processSerial() { while (myPort.available () > 0) {
char aChar = (char) myPort.read();
// You'll need to add a block like one of these
// if you want to add a 3rd sensor:
if (aChar == 'A') {
bJustBuilt = false;
whichValueToAccum = 0;
} else if (aChar == 'B') {
bJustBuilt = false;
whichValueToAccum = 1;
} else if (((aChar == 13) || (aChar == 10)) && (!bJustBuilt)) {
// If we just received a return or newline character, build the number:
int accum = 0;
int nChars = serialChars.size();
for (int i=0; i < nChars; i++) { int n = (nChars - i) - 1; int aDigit = ((Integer)(serialChars.get(i))).intValue(); accum += aDigit * (int)(pow(10, n)); } // Set the global variable to the number we captured. // You'll need to add another block like one of these // if you want to add a 3rd sensor: if (whichValueToAccum == 0) { valueA = accum; // println ("A = " + valueA); } else if (whichValueToAccum == 1) { valueB = accum; // println ("B = " + valueB); } // Now clear the accumulator serialChars.clear(); bJustBuilt = true; } else if ((aChar >= 48) && (aChar <= 57)) {
// If the char is between '0' and '9', save it.
int aDigit = (int)(aChar - '0');
serialChars.add(aDigit);
}
}
}
void setGradient(int x, int y, float w, float h, color c1, color c2, int axis ) {
noFill();
if (axis == Y_AXIS) { // Top to bottom gradient
for (int i = y; i <= y+h; i++) {
float inter = map(i, y, y+h, 0, 1);
color c = lerpColor(c1, c2, inter);
stroke(c);
line(x, i, x+w, i);
}
}
}
void drawPlayer(int legPosy1, int legPosy2) {
noStroke();
pushMatrix();
translate(250, 210);
//shadow
fill(5, 40, 4);
ellipse(0, 275, 200, 100);
//player's right leg top
stroke(0);
strokeWeight(20);
pushMatrix();
translate(40, 170);
rotate(6);
line(0, 0, 0, 100 - legPosy1); //70 is variable
popMatrix();
//player's right leg bottom
//player's left leg top
stroke(0);
strokeWeight(20);
pushMatrix();
translate(-40, 170);
rotate(-6);
line(0, 0, 0, 100 - legPosy2); //70 is variable
popMatrix();
//player's left leg top
//player's body
noStroke();
fill(88, 3, 1);
triangle(0, -20, 100, 170, -100, 170);
//player's head
fill(147, 125, 97);
ellipse(0, 0, 130, 90);
//player's eyes
//player's right eye
pushMatrix();
translate(30, 0);
fill(255);
ellipse(0, 0, 40, 40);
fill(0);
ellipse(10, 0, 15, 15);
popMatrix();
//player's left eye
pushMatrix();
translate(-30, 0);
fill(255);
ellipse(0, 0, 40, 40);
fill(0);
ellipse(10, 0, 15, 15);
popMatrix();
popMatrix();
}
void drawEnemy() {
noStroke();
fill(ghostCol);
pushMatrix();
translate(600, 300);
scale(2 * (1.0 - runCounter/100.0));
//ghost's body
ellipse(0, 0, 210, 400);
ellipse(0, 100, 210, 200);
ellipse(0, 0, 200, 400);
for (j = 0; j < 4; j++) {
if ((j == 1) || (j == 2)) {
h = 170;
} else if ((j == 0) || (j == 3)) {
h = 150;
}
ellipse(-75 +(j*50), h, 60, 90);
}
//ghost's eyes
//ghost's right eye
pushMatrix();
translate(-40, 0);
fill(250, 200, 200);
ellipse(0, -100, 50, 50);
fill(200, 150, 150);
ellipse(0, -100, 40, 40);
fill(150, 100, 100);
ellipse(0, -100, 30, 30);
fill(100, 50, 50);
ellipse(0, -100, 20, 20);
fill(50, 0, 0);
ellipse(0, -100, 10, 10);
popMatrix();
//ghost's left eye
pushMatrix();
translate(40, 0);
fill(250, 200, 200);
ellipse(0, -100, 50, 50);
fill(200, 150, 150);
ellipse(0, -100, 40, 40);
fill(150, 100, 100);
ellipse(0, -100, 30, 30);
fill(100, 50, 50);
ellipse(0, -100, 20, 20);
fill(50, 0, 0);
ellipse(0, -100, 10, 10);
popMatrix();
//ghost's mouth
fill(250, 200, 200);
ellipse(0, 20, 125, 175);
fill(200, 150, 150);
ellipse(0, 20, 105, 155);
fill(150, 100, 100);
ellipse(0, 20, 85, 135);
fill(100, 50, 50);
ellipse(0, 20, 65, 115);
fill(50, 0, 0);
ellipse(0, 20, 45, 95);
for (i = 0; i < 5; i++) { fill(ghostCol); ellipse(-45 +(i*23), -52, 30, 45); } //ghost's right row of teeth fill(250, 250, 225); ellipse(-46, 35, toothWidth, 65); fill(250, 250, 200); ellipse(-46 +(10), 50, toothWidth, 65); fill(250, 250, 225); ellipse(-46 +(23), 65, toothWidth, 65); fill(250, 250, 200); ellipse(-46 +(33), 70, toothWidth, 65); //ghost's left row of teeth fill(250, 250, 225); ellipse(-46 +(92), 35, toothWidth, 65); fill(250, 250, 200); ellipse(-46 +(79), 50, toothWidth, 65); fill(250, 250, 225); ellipse(-46 +(69), 65, toothWidth, 65); fill(250, 250, 200); ellipse(-46 +(59), 70, toothWidth, 65); //ghost'sfront tooth fill(250, 250, 225); ellipse(-46 +(46), 75, toothWidth, 65); popMatrix(); } void legSwitch() { if (valueA > valueB + 50 && leftDown==false) {
leftDown = true;
runCounter+=10;
} else if (valueB > valueA + 50 && leftDown==true) {
leftDown = false;
runCounter+=10;
}
runCounter = constrain(runCounter, 0, 100);
ghostApprocah();
}
void ghostApprocah() {
runCounter--;
}
void drawHill(){
noStroke();
fill(5, 47, 4);
ellipse(width/2.5, height + (height/8), 1000, 700);
}
void drawScenery(){
noStroke();
fill(5, 27, 24);
ellipse(width/2, height, 500, 700);
ellipse(width/4, height, 600, 850);
}
/* ---------------------------------------------------------
* | Arduino Experimentation Kit Example Code |
* | CIRC-10 .: Temperature :. (TMP36 Temperature Sensor) |
* ---------------------------------------------------------
*
* A simple program to output the current temperature to the IDE's debug window
*
* For more details on this circuit: http://tinyurl.com/c89tvd
*/
//TMP36 Pin Variables
int temperaturePin = 0; //the analog pin the TMP36's Vout (sense) pin is connected to
//the resolution is 10 mV / degree centigrade
//(500 mV offset) to make negative temperatures an option
/*
* setup() - this function runs once when you turn your Arduino on
* We initialize the serial connection with the computer
*/
void setup()
{
Serial.begin(9600); //Start the serial connection with the copmuter
//to view the result open the serial monitor
//last button beneath the file bar (looks like a box with an antenae)
}
void loop() // run over and over again
{
float temperature = getVoltage(temperaturePin); //getting the voltage reading from the tem
//perature sensor
temperature = (temperature - .5) * 100; //converting from 10 mv per degree wit 500
// mV offset
//to degrees ((volatge - 500mV) times 100)
Serial.println(temperature); //printing the result
delay(1000); //waiting a second
}
/*
* getVoltage() - returns the voltage on the analog input defined by
* pin
*/
float getVoltage(int pin){
return (analogRead(pin) * .004882814); //converting from a 0 to 1023 digital range
// to 0 to 5 volts (each 1 reading equals ~ 5 milliv
//olts
}
/*
Analog Input
Demonstrates analog input by reading an analog sensor on analog pin 0 and
turning on and off a light emitting diode(LED) connected to digital pin 13.
The amount of time the LED will be on and off depends on
the value obtained by analogRead().
The circuit:
* Potentiometer attached to analog input 0
* center pin of the potentiometer to the analog pin
* one side pin (either one) to ground
* the other side pin to +5V
* LED anode (long leg) attached to digital output 13
* LED cathode (short leg) attached to ground
* Note: because most Arduinos have a built-in LED attached
to pin 13 on the board, the LED is optional.
Created by David Cuartielles
modified 30 Aug 2011
By Tom Igoe
This example code is in the public domain.
http://arduino.cc/en/Tutorial/AnalogInput
*/
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// turn the ledPin on
digitalWrite(ledPin, HIGH);
// stop the program for milliseconds:
delay(sensorValue);
// turn the ledPin off:
digitalWrite(ledPin, LOW);
// stop the program for for milliseconds:
delay(sensorValue);
}
/*
* Force Sensitive Resistor Test Code
*
* The intensity of the LED will vary with the amount of pressure on the sensor
*/
int sensePin = 2; // the pin the FSR is attached to
int ledPin = 9; // the pin the LED is attached to (use one capable of PWM)
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT
}
void loop() {
int value = analogRead(sensePin) / 4; //the voltage on the pin divded by 4 (to scale from 10 bits (0-1024) to 8 (0-255)
analogWrite(ledPin, value); //sets the LEDs intensity proportional to the pressure on the sensor
Serial.println(value); //print the value to the debug window
}