|
Intro to Physical Computing Syllabus Research & Learning Other Class pages
ITP Help Pages |
Tom Igoe ClassNotes from Tom Igoe's classesHello class! Whoopee! Interactive guide to resistor color codes
int lastSwitchState = LOW;
boolean blinkingOrNot = false;
int ledState = 0;
long lastTimeBlinked = 0;
long interval = 1000;
void setup() {
pinMode(2, INPUT);
pinMode(11, OUTPUT);
Serial.begin(9600);
}
void loop() {
// listen for an input
int switchState = digitalRead(2);
// compare the input to its previous state
if (switchState != lastSwitchState) {
if (switchState == HIGH) {
Serial.println("switch is pressed!");
blinkingOrNot = !blinkingOrNot;
}
else {
// if the current state is LOW then the button
// wend from on to off:
Serial.println("off");
}
}
lastSwitchState = switchState;
// change the output based on the change
if (blinkingOrNot == true) {
if (millis() - lastTimeBlinked > interval) {
doSomething();
lastTimeBlinked = millis();
}
}
}
void doSomething() {
digitalWrite(11, ledState);
ledState = !ledState;
}
Suzanne's gun program, in progress
const int tiltSwitch = 2;
const int hammerSwitch = 3;
int tiltSwitchState = LOW;
int hammerSwitchState = LOW;
int lastHammerSwitchState = LOW;
boolean gunIsHorizontal = false;
boolean gunIsFired = false;
long timeStamp = 0;
void setup() {
pinMode(tiltSwitch, INPUT);
pinMode(hammerSwitch, INPUT);
Serial.begin(9600);
}
void loop() {
// read inputs:
tiltSwitchState = digitalRead(tiltSwitch);
hammerSwitchState = digitalRead(hammerSwitch);
// led light turns on, start a clock
// person pulls out gun
// tilt switch flips when gun is horizontal
if (tiltSwitchState == LOW) {
gunIsHorizontal = true;
}
else {
gunIsHorizontal = false;
}
// check if the hammer fired:
if (hammerSwitchState != lastHammerSwitchState) {
if ( hammerSwitchState == HIGH) {
gunIsFired = true;
Serial.println("Fired!");
long howLongToFire = millis() - timeStamp;
Serial.println(howLongToFire);
} else {
gunIsFired = false;
Serial.println("cocked!");
timeStamp = millis();
}
}
lastHammerSwitchState = hammerSwitchState;
//number of milliseconds is captured
// read switch at the hammer, see when it goes from open to close
if (gunIsHorizontal ) {
if (gunIsFired) {
reportTheScore();
}
}
// report the time
}
void reportTheScore() {
}
Stuff from 10/20/10Processing code:
import processing.serial.*;
char thisChar = 0;
Serial myPort; // The serial port
int graphXPos = 1; // the horizontal position of the graph:
void setup () {
size(400, 300); // window size
// List all the available serial ports
println(Serial.list());
// I know that the fisrt port in the serial list on my mac
// is usually my Arduino module, so I open Serial.list()[0].
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[0], 9600);
// set inital background:
background(48,31,65);
}
void draw () {
// nothing happens in draw. It all happens in SerialEvent()
}
void serialEvent (Serial myPort) {
// get the byte:
int inByte = myPort.read();
// print it:
println(inByte);
// set the drawing color. Pick a pretty color:
stroke(123,128,158);
// draw the line:
line(graphXPos, height, graphXPos, height - inByte);
// at the edge of the screen, go back to the beginning:
if (graphXPos >= width) {
graphXPos = 0;
// clear the screen:
background(48,31,65);
}
else {
// increment the horizontal position for the next reading:
graphXPos++;
}
myPort.write(thisChar);
println("I am being called");
}
void keyReleased() {
thisChar = key;
myPort.write(thisChar);
println("I am being called");
}
Arduino code:
void setup() {
Serial.begin(9600);
while( Serial.available()<= 0 ) {
Serial.println("Hello");
}
}
void loop() {
int sensorValue = 0;
if (Serial.available() > 0 ) {
int inByte = Serial.read();
if (inByte == 'x') {
sensorValue = analogRead(A0);
Serial.write(sensorValue);
}
else if (inByte == 'y') {
sensorValue = analogRead(A1);
Serial.write(sensorValue);
}
else if (inByte == 'z') {
sensorValue = analogRead(A2);
Serial.write(sensorValue);
}
}
}
|