Processing
import processing.serial.*;
Serial duankou;
void setup() {
size(500,500);
duankou = new Serial(this, "COM3", 9600);
}
int X,a;
void draw() {
getData();
background(127);
println(X);
if(X<50&&X>0){
delay(200);
a+=1;
/// println(a);
}
fill(255,0,0);textSize(80);text("Score:"+a,100,100);
}
byte [] data=new byte[1];
void getData(){
if (duankou.available() > 0) {
duankou.readBytes(data);
X=data[0];
println(data);
}
}
2019年1月11日 星期五
Week16 昱霖的筆記
Arduino
int photocellPin = 0; // 光敏電阻 (photocell) 接在 anallog pin 2
int photocellVal = 13; // photocell variable
int a=0;
void setup() {
Serial.begin(9600);
pinMode(photocellVal, OUTPUT);
}
byte data[1];
void loop() {
// 讀取光敏電阻並輸出到 Serial Port
photocellVal = analogRead(A0);
//Serial.println(photocellVal);
delay(200);
data[0]=photocellVal;
Serial.write(data,1);
}
int photocellPin = 0; // 光敏電阻 (photocell) 接在 anallog pin 2
int photocellVal = 13; // photocell variable
int a=0;
void setup() {
Serial.begin(9600);
pinMode(photocellVal, OUTPUT);
}
byte data[1];
void loop() {
// 讀取光敏電阻並輸出到 Serial Port
photocellVal = analogRead(A0);
//Serial.println(photocellVal);
delay(200);
data[0]=photocellVal;
Serial.write(data,1);
}
2019年1月3日 星期四
2018年12月27日 星期四
Week 14 昱霖的筆記
使用範例與旋鈕座結合
旋轉到大值LED會閃爍
int sensorPin = A0; // select the input pin for the potentiometerint 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 <sensorValue> milliseconds:
delay(sensorValue);
// turn the ledPin off:
digitalWrite(ledPin, LOW);
// stop the program for for <sensorValue> milliseconds:
delay(sensorValue);
}
變成跑值
int sensorPin = A0; // select the input pin for the potentiometerint ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
pinMode(8, OUTPUT);
}
void loop() {
int value= analogRead(A0);
tone(8,value);
int c=(value/10+'0');
Serial.write(c);
delay(100);
}
加上Processing
使球可以隨著旋鈕左右移動
import processing.serial.*;
Serial myPort;
void setup(){
size (1024,600);
myPort=new Serial(this,"COM7",9600);
}
float handX=0;
void draw(){
background(255);
if(myPort.available()>0){
int c =myPort.read();
handX=(c-'0')*10;
}
ellipse(handX,500,100,100);
}
2018年12月6日 星期四
Week 13 昱霖的筆記
在processing開啟範例
void setup() {
// pinMode(switchPin, INPUT); // Set pin 0 as an input
pinMode(2,INPUT_PULLUP);
Serial.begin(9600); // Start serial communication at 9600 bps
}
void loop() {
if (digitalRead(2) == HIGH) { // If switch is ON,
Serial.write(1); // send 1 to Processing
} else { // If the switch is not ON,
Serial.write(0); // send 0 to Processing
}
delay(100); // Wait 100 milliseconds
}
按下按鈕變黑色
2018年11月22日 星期四
Week 11 昱霖的筆記
安裝驅動程式
閃燈
void setup() {
for(int i=2;i<=13;i++){
pinMode(i, OUTPUT);
}
}
void loop() {
for(int i=2;i<=13;i++){
digitalWrite( i, HIGH);
delay(300);
}
for(int i=2;i<=13;i++){
digitalWrite( i, LOW);
}
}
發出聲響Do Re Mi Fa So
int piezoPin = 8;int length = 15;
char notes[] = "ccggaag ffeeddc ggffeed ggffeed ccggaag ffeeddc "; // a space represents a rest
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int tempo = 300;
void playTone(int tone, int duration)
{
for (long i = 0; i < duration * 1000L; i += tone * 2)
{
digitalWrite(piezoPin, HIGH);
delayMicroseconds(tone);
digitalWrite(piezoPin, LOW);
delayMicroseconds(tone);
}
}
void playNote(char note, int duration)
{
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
for (int i = 0; i < 8; i++)
{
if (names[i] == note)
{
playTone(tones[i], duration);
}
}
}
void setup()
{
pinMode(piezoPin, OUTPUT);
}
void loop()
{
for (int i = 0; i < length; i++)
{
if (notes[i] == ' ')
{
delay(beats[i] * tempo);
} else
playNote(notes[i], beats[i] * tempo);
delay(tempo / 2);
}
}
按按鈕可切換音樂(只能換成聖誕歌)
int piezoPin = 8;int button = 2;
int lengths = 26;
char jbnotes[] = "eeeeeeegcde fffffeeeeddedg";
int jbbeats[] = { 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2};
int length = 15;
char ttlsnotes[] = "ccggaag ffeeddc ggffeed ggffeed ccggaag ffeeddc ";
int ttlsbeats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int tempo = 300;
void playTone(int tone, int duration)
{
for (long i = 0; i < duration * 1000L; i += tone * 2)
{
digitalWrite(piezoPin, HIGH);
delayMicroseconds(tone);
digitalWrite(piezoPin, LOW);
delayMicroseconds(tone);
}
}
void playNote(char note, int duration)
{
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
for (int i = 0; i < 8; i++)
{
if (names[i] == note)
{
playTone(tones[i], duration);
}
}
}
void setup()
{
pinMode(piezoPin, OUTPUT);
pinMode(button, INPUT_PULLUP);
}
void loop()
{
int sensorVal = digitalRead(button);
if (sensorVal == HIGH)
{
for (int i = 0; i < length; i++)
{
if (ttlsnotes[i] == ' ')
{
delay(ttlsbeats[i] * tempo);
}
else
playNote(ttlsnotes[i], ttlsbeats[i] * tempo);
delay(tempo / 2);
}
}
else
{
for (int i = 0; i < lengths; i++)
{
if (jbnotes[i] == ' ')
{
delay(jbbeats[i] * tempo);
}
else
playNote(jbnotes[i], jbbeats[i] * tempo);
delay(tempo / 2);
}
}
}
更改程式碼後可正常切換歌曲(小星星換聖誕歌,聖誕歌換小星星)
int now=0;
void loop()
{
delay(tempo/2);
if(now==0)
{
for (int i = 0; i < length; i++)
{
if (ttlsnotes[i] == ' ')
{
delay(ttlsbeats[i] * tempo);
}
else
playNote(ttlsnotes[i], ttlsbeats[i] * tempo);
if( digitalRead(button)==LOW){ now=1;break;}
delay(tempo / 2);
}
}
else
{
for (int i = 0; i < lengths; i++)
{
if (jbnotes[i] == ' ')
{
delay(jbbeats[i] * tempo);
}
else
playNote(jbnotes[i], jbbeats[i] * tempo);
if( digitalRead(button)==LOW){now=0;break;}
delay(tempo / 2);
}
}
}
2018年11月1日 星期四
2018年10月25日 星期四
Week07昱霖的筆記
做出血條(HP0)
int life=10;void setup(){
size(500,500);
}
void draw(){
background(0,0,205);
rect(50,50,100,20);
}
血條(HP100)
int life=10;void setup(){
size(500,500);
}
void draw(){
background(0,0,205);
fill(255); rect(50,50,100,20);
fill(255,0,0); rect(50,50,life*10,20);
}
點擊滑鼠會造成扣血
void mousePressed(){
life--;
}
HP歸零顯示Game Over
int life=10;
boolean gameOver=false;
void setup(){
size(500,500);
}
void draw(){
background(0,0,205);
fill(255); rect(50,50,100,20);
fill(255,0,0); rect(50,50,life*10,20);
if(gameOver){
textSize(40);
text("Game Over",150,150);
}
}
void mousePressed(){
life--;
if(life<=0) gameOver=true;
}
2018年10月18日 星期四
Week06 昱霖的筆記
播放音樂
Minim minim;
AudioPlayer player;
void setup(){
minim = new Minim(this);
player=minim.loadFile("Kalimba.mp3");
player.play();
}
可跳出一顆球
float fruitX, fruitY, fruitVX, fruitVY;
void setup(){
size(900,700);
fruitX=30; fruitVX=3;
fruitY=700; fruitVY=-35;
}
void draw(){
background(0);
ellipse(fruitX,fruitY, 100,100);
fruitX+=fruitVX;
fruitY+=fruitVY;
fruitVY+=0.98;
}
跑出多顆球
float [] fruitX=new float[20];
float [] fruitY=new float[20];
float [] fruitVX=new float[20];
float [] fruitVY=new float[20];
void setup(){
size(800,600);
for(int i=0;i<20;i++){
fruitX[i]=random(800); fruitVX[i]=3;
fruitY[i]=700; fruitVY[i]=-35;
}
}
void draw(){
background(0);
for(int i=0;i<20;i++){
ellipse(fruitX[i],fruitY[i], 100,100);
fruitX[i]+=fruitVX[i];
fruitY[i]+=fruitVY[i];
fruitVY[i]+=0.98;
}
}
使球不規則跳動,並重複出現
float [] fruitX=new float[20];
float [] fruitY=new float[20];
float [] fruitVX=new float[20];
float [] fruitVY=new float[20];
void reborn(int i){
fruitX[i]=random(800); fruitVX[i]=random(6)-3;
fruitY[i]=700; fruitVY[i]=-35;
}
void setup(){
size(800,600);
for(int i=0;i<20;i++){
reborn(i);
}
}
void draw(){
background(0);
for(int i=0;i<20;i++){
if( dist(mouseX,mouseY,fruitX[i],fruitY[i])<=50 ){
reborn(i);continue;
}
ellipse(fruitX[i],fruitY[i], 100,100);
fruitX[i]+=fruitVX[i];
fruitY[i]+=fruitVY[i];
fruitVY[i]+=0.98;
if(fruitY[i]>700) reborn(i);
}
}
將圈圈改成蘋果
加入PImage
//float fruitX, fruitY, fruitVX, fruitVY;
PImage img;
float [] fruitX=new float[20];
float [] fruitY=new float[20];
float [] fruitVX=new float[20];
float [] fruitVY=new float[20];
void reborn(int i){
fruitX[i]=random(800); fruitVX[i]=random(6)-3;
fruitY[i]=700; fruitVY[i]=-35;
}
void setup(){
size(800,600);
img = loadImage("Apple.png");
for(int i=0;i<20;i++){
reborn(i);
}
}
void draw(){
background(0);
for(int i=0;i<20;i++){
if( dist(mouseX,mouseY,fruitX[i],fruitY[i])<=50 ){
reborn(i);continue;
}
image(img,fruitX[i],fruitY[i], 100,100);
fruitX[i]+=fruitVX[i];
fruitY[i]+=fruitVY[i];
fruitVY[i]+=0.98;
if(fruitY[i]>700) reborn(i);
}
}
2018年10月11日 星期四
Week05 昱霖的筆記
做出雞蛋並讓他落下
float[] eggX=new float[100];
float[] eggY=new float[100];
boolean[] eggDie=new boolean[100];
void setup(){
size(800,800);
for(int i=0;i<100;i++){
eggX[i]=random(100,700);
eggY[i]=-random(2000);
eggDie[i]=false;
}
}
void draw(){
background(68);
fill(255);rect(mouseX-50, mouseY-25, 100, 50);
for(int i=0;i<100;i++){
if(eggDie[i])continue;
ellipse(eggX[i], eggY[i], 60,120);
eggY[i]+=5;
if (eggY[i]>800) eggY[i]=-2000+600;
if (dist(mouseX-50, mouseY-25, eggX[i],eggY[i])<50){
eggDie[i]=true;
}
}
加入分數,顯示獲得的積分
int score=0;
score+=100;///如何計算分數
fill(255,0,0); textSize(80); text("Score:"+score,100,100);
2018年10月4日 星期四
Week04 昱霖的筆記
更改顏色
fill(R,G,B)
顯示的文字
text("Start!!!",x,y)
更改字體大小
textSize()
加入 mouseX,mouseY
使Start!!! 跟隨滑鼠移動
*background() 未加會造成字體重複出現
加入bullet(只能發一顆)
if(bulletFlying)
{
ellipse(bulletX,bulletY,50,50);
bulletX+=5;
}
float bulletX=250,bulletY=250;
boolean bulletFlying=false;
void keyPressed(){
bulletFlying=true;
}
連發
for(int i=0;i<bulletN;i++)
{
if(bulletFlying[i])
{
ellipse(bulletX[i],bulletY[i],50,50);
bulletX[i]+=5;
}
}
float []bulletX=new float[100];
float []bulletY=new float[100];
int bulletN=0;
boolean []bulletFlying=new boolean[100];
void keyPressed(){
bulletFlying[bulletN]=true;
bulletX[bulletN]=100; bulletY[bulletN]=100;
bulletN++;
}
*子彈數超過宣告陣列大小會出錯
2018年9月27日 星期四
Week03 昱霖的筆記
線畫出一個圓
ellipse(a,b, c,d);
void myTriangle(int cx,int cy){
float R=25, a=PI/2;
triangle(cx+R*cos(a),cy+R*sin(a),
cx+R*cos(a+2*PI/3),cy+R*sin(a+2*PI/3),
cx+R*cos(a-2*PI/3),cy+R*sin(a-2*PI/3) );
}
void draw(){
background(255);
for (int cx=10;cx<800;cx+=40){
for (int cy=10;cy<400;cy+=40) {
myTriangle(cx,cy);
}
}
float R=20, a=-PI/2;
匯入圖片 並使他可以用鍵盤來移動
void keyPressed() {
if(keyCode==UP) {marioY-=5;}
if(keyCode==DOWN) {marioY+=5;}
if(keyCode==LEFT) {marioX-=5;}
if(keyCode==RIGHT) {marioX+=5;}
}
2018年9月20日 星期四
week02 昱霖的筆記
看別人的成果
https://www.openprocessing.org/
打出接龍程式碼
void setup(){
size(400,400);
}
int R=50;
void draw(){
ellipse(mouseX,mouseY,R,R);
R-=5;
if(R<0) R=50;
}
加入顏色
colorMode()
fill(R,G,B,Alpha)
更改為彩色
加入程式碼
H++;
if(H>=100) H=0;
將圖片導入 更改背景
訂閱:
文章 (Atom)
















































