2012年2月13日 星期一

Processing + Arduino ( 2 buttons)

Program 1:
-------------------------------------
int buttonState1 = 0;
int lastButtonState1 = 0;

int buttonState2 = 0;
int lastButtonState2 = 0;

int switchPin1 = 4; // Switch connected to pin 4
int switchPin2 = 3;
void setup() {
pinMode(switchPin1, INPUT);
pinMode(switchPin2, INPUT);// Set pin 0 as an input
Serial.begin(9600); // Start serial communication at 9600 bps
}

void loop() {
int i, j, k;

buttonState1=digitalRead(switchPin1);
buttonState2=digitalRead(switchPin2);


if (buttonState1 != lastButtonState1)
if (buttonState1 == HIGH) { // If switch is ON,
i=1;
Serial.write(i);
} else { // If the switch is not ON,
i=0;
Serial.write(i);
}

if (buttonState2 != lastButtonState2)
if (buttonState2 == HIGH) { // If switch is ON,
j=2;
Serial.write(j);

} else { // If the switch is not ON,
j=3;
Serial.write(j);
}

lastButtonState1 = buttonState1;
lastButtonState2 = buttonState2;

delay(100); // Wait 100 milliseconds
}
-------------------------------------

Program 2:
-------------------------------------
int buttonState1 = 0;
int lastButtonState1 = 0;

int buttonState2 = 0;
int lastButtonState2 = 0;

int switchPin1 = 4; // Switch connected to pin 4
int switchPin2 = 3;
void setup() {
pinMode(switchPin1, INPUT);
pinMode(switchPin2, INPUT);// Set pin 0 as an input
Serial.begin(9600); // Start serial communication at 9600 bps
}

void loop() {
int i, j, k;

buttonState1=digitalRead(switchPin1);
buttonState2=digitalRead(switchPin2);


if (buttonState1 != lastButtonState1)
if (buttonState1 == HIGH) { // If switch is ON,
i=1;
} else { // If the switch is not ON,
i=0;
}

if (buttonState2 != lastButtonState2)
if (buttonState2 == HIGH) { // If switch is ON,
j=1;
} else { // If the switch is not ON,
j=0;
}

if (i==0 && j==0)
k =0;
if (i==1 && j==0)
k =1;
if (i==0 && j==1)
k =2;
if (i==1 && j==1)
k =3;

if (buttonState1 != lastButtonState1 || buttonState2 != lastButtonState2)
  Serial.write(k);

lastButtonState1 = buttonState1;
lastButtonState2 = buttonState2;

delay(100); // Wait 100 milliseconds
}
-------------------------------------

Homework 04: Arduino + Processing



Arduino and Processing are connected by using the "serial" object.

Homework: 04_01: Arduino sends a byte of message to Processing.

// Arduino code
int buttonState = 0;
int lastButtonState = 0;
int switchPin = 4; // Switch connected to pin 4
void setup() {
pinMode(switchPin, INPUT); // Set pin 0 as an input
Serial.begin(9600); // Start serial communication at 9600 bps
}
void loop() {
 int val;
 buttonState =digitalRead(switchPin);

if (buttonState != lastButtonState) { // If switch is ON,
  if (buttonState == HIGH)
     val=1;
   else  // If the switch is not ON,
     val=0;
Serial.write(val);
//Serial.println(val);
}

lastButtonState = buttonState;
delay(100); // Wait 100 milliseconds
}

// Processing code
import processing.serial.*;
Serial port; // Create object from Serial class
int val; // Data received from the serial port
void setup() {
size(200, 200);
frameRate(10);
// Open the port that the board is connected to and use the same speed (9600 bps)
println(Serial.list());
String portName = Serial.list()[0];
//where 0 is dependent on the "com" port number.

port = new Serial(this, portName, 9600);
}
void draw() {
if (0 < port.available()) { // If data is available,
val = port.read(); // read it and store it in val
}
background(255); // Set background to white
if (val == 0) { // If the serial value is 0,
fill(0); // set fill to black
} else { // If the serial value is not 0,
fill(204); // set fill to light gray
}
rect(50, 50, 100, 100);
}

// Arduino circuit
 Add a button in the bread board and connect the button to pin 4 of Arduino.


Homework:04_02: Processing sends a a byte of message to Arduino.
1. Go to Page 653 and Page 654 of the Processing_handbook pdf file.
2. Copy the code "Example 3A" and "Example 3B" for Arduino and Processing, respectively.
3. Modify the statement in Processing
int ledPin = 4;
into
int ledPin = 10;
and
port=new Serial(this, 9600)
into
println(Serial.list());
String portName = Serial.list()[0];
port = new Serial(this, portName, 9600);

where 0 is dependent on the "com" port number.

Homework:04_03:
1. Download the code form webhd.mcu.edu.tw using the group name "im2009".
2. Design a circuit with four buttons in Arduino.
3. Play the game and trace theses codes of Processing and Arduino.

2011年2月26日 星期六

Homework 02: Arduino Piano Show

HW02_01: Button
Turn on and off a LED connected to pin 13 when pressing a pushbutton connected to pin 2.

HW02_02: Speaker
A speaker can generate a specific tone when it receives an electronic wave with a specific Pulse Width. For example, you can hear tone “b” when the speaker receives the following wave.


---------------------------
Tone Table for Speaker
---------------------------
#define c 3830 // 261 Hz
#define d 3400 // 294 Hz
#define e 3038 // 329 Hz
#define f 2864 // 349 Hz
#define g 2550 // 392 Hz
#define a 2272 // 440 Hz
#define b 2028 // 493 Hz
#define C 1912 // 523 Hz

----------------------------------
Program 1: Basic sound program
----------------------------------
int speakerOut = 9;
int tone;

void setup() {
pinMode(speakerOut, OUTPUT);

}

void loop() {

tone = 2028;

digitalWrite(speakerOut,HIGH);
delayMicroseconds(tone / 2);
digitalWrite(speakerOut, LOW);
delayMicroseconds(tone / 2);

}


HW02_3: Arduino Doorbell
The doorbell includes a button and a speaker on Arduino. When you press the button, you can hear a tone from the speaker. You can design a special tone for your doorbell.


HW02_4: Arduino Piano Show
You must cooperate with your partner to finish this assignment. Every one designs four buttons with four tones. Each group has to play a song with your Arduino board. A video is necessary to present your Arduino piano work.

2011年2月15日 星期二

HW01: Arduino LED Show

HW01_01: Blinking LED

HW01_02: LED Traffic Light
Use Arduino to design a LED traffic light by using the red, amber and green color LEDs. The duration of the red, amber, and green lights is 10, 3, and 10 seconds respectively.

HW01_03: Seven Colorful LEDs
Extend the above code to seven LED lights, where every LED is turued on and then turned off one by one with a same duration.

HW01_04: Arduino LED Text Show
You must cooperate with your partner to finish this assignment.
You have to make a video to present your LED project work and upload the video to YouTube.

Arduino + OpenCV-WebCam

Goals of the course of Interactive Electronics
1. Write programs of Arduino. (8 weeks)
2. Write programs of OpenCV-WebCam. (8 weeks)

The evaluation of the score:
1. Homeworks - 40%
2. A project for Arduino - 20%
3. A project for OpenCV - 20%
3. A test for OpenCV - 20%

- Team work is encouraged in this course and two students are allowed in each team.