顯示具有 04160223_賴宛妤 標籤的文章。 顯示所有文章
顯示具有 04160223_賴宛妤 標籤的文章。 顯示所有文章

2018年1月22日 星期一

Week19 是賴小沫的_期末作品

✎期末作品展示

👀程式碼

Arduino

const int finish = A0;
const int hole1 = A1;
const int hole2 = A2;
const int hole3 = A3;
const int hole4 = A4;
const int hole5 = A5;
const int start = A6;

int finishValue = 0;
int hole1Value = 0;
int hole2Value = 0;
int hole3Value = 0;
int hole4Value = 0;
int hole5Value = 0;
int startValue = 0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  // Read the sensors first:
  finishValue = analogRead(finish)/50;
  hole1Value = analogRead(hole1)/50; 
  hole2Value = analogRead(hole2)/50;
  hole3Value = analogRead(hole3)/50;
  hole4Value = analogRead(hole4)/50;
  hole5Value = analogRead(hole5)/50;
  startValue = analogRead(start)/50;

  //用來判斷光敏電阻的門檻值
  /*Serial.println(finishValue);
  Serial.println(hole1Value);
  Serial.println(hole2Value);
  Serial.println(hole3Value);
  Serial.println(hole4Value);
  Serial.println(hole5Value);
  Serial.println(startValue);
  Serial.println(" ");*/

 
  if(finishValue<4) Serial.println("f");
  if(hole1Value<4) Serial.println("a");
  if(hole2Value<4) Serial.println("b");
  if(hole3Value<4) Serial.println("c");
  if(hole4Value<4) Serial.println("d");
  if(hole5Value<4) Serial.println("e");
  if(startValue>10) Serial.println("s");
  if(startValue<6) Serial.println("r");
  delay(50);
}

processing
//music
import ddf.minim.*;
Minim minim;
AudioPlayer player;

//devices
import processing.serial.*;
Serial myPort;
int val;

PImage h1,h2,h3,h4,h5,finish;
int state=0,count=0;
float time=0;
boolean game=false;

void setup(){
  size(600,800);
  //播放音樂
  minim=new Minim(this);
  player=minim.loadFile("music.mp3",2048);
  player.play();
  player.loop();

  myPort = new Serial(this,"COM9",9600);  //讀取裝置的變數

  h1=loadImage("1.png");
  h2=loadImage("2.png");
  h3=loadImage("3.png");
  h4=loadImage("4.png");
  h5=loadImage("5.png");
  finish=loadImage("F.png");
}

void draw(){
  //Game Start/Over
  if(myPort.available()>0){
      val=myPort.read();

      if(val==115){  //如果起點接收到光,遊戲開始
        game=true;
      }
      else if(val==114) game=false;  //如果起點沒有接收到光,遊戲重置
  }
  back();  //繪出場景
 
  if(game==true){  //遊戲開始
    count++;
    if(count==60 && state==0){  //計時器
      time++;
      count=0;
    }
    //判斷球掉進哪一個洞裡
    if(myPort.available()>0 && state==0){
      val=myPort.read();
      if(val==97){
        state=5;
        println("Hole5");
      }
      else if(val==98){
        state=4;
        println("Hole4");
      }
      else if(val==99){
        state=3;
        println("Hole3");
      }
      else if(val==100){
        state=2;
        println("Hole2");
      }
      else if(val==101){
        state=1;
        println("Hole1");
      }
      else if(val==102){
        state=6;
        println("Finish");
      }
    }

    textSize(25);text("Time:"+time,450,40);
    //秀出結果畫面
    if(state==1) image(h1,100,200,400,400);
    else if(state==2) image(h2,100,200,400,400);
    else if(state==3) image(h3,100,200,400,400);
    else if(state==4) image(h4,100,200,400,400);
    else if(state==5) image(h5,100,200,400,400);
    else if(state==6) image(finish,70,200,450,400);
  }
  else if(game==false){
    //reset
    state=0;
    time=0;
    count=0;
    back();
  }
}

//Scene
void back(){
  background(#F7AAAA);
  strokeWeight(5); fill(255); rect(50,50,500,700);
  strokeWeight(5); stroke(0);
  //row1
  line(335,680,550,680);
  line(100,680,265,680);
  //row2
  line(120,610,265,610);
  //row3
  line(50,540,215,540);
  line(265,540,450,540);
  //row4
  line(120,470,480,470);
  //row5
  line(450,400,550,400);
  line(190,400,380,400);
  //row6
  line(380,330,480,330);
  //row8
  line(50,190,380,190);
  //row9
  line(140,120,480,120);
  //cou1
  line(335,680,335,610);
  //cou2
  line(450,610,450,540);
  line(265,610,265,540);
  //cou3
  line(120,470,120,260);
  //cou5
  line(190,400,190,260);
  line(380,400,380,330);
  //cou6
  line(280,190,280,330);
  //cou7
  line(380,190,380,260);
  line(480,120,480,260);
  //hole
  fill(0,0,255);
  ellipse(90,90,60,60); //finish
  fill(0);
  ellipse(415,575,40,40);  //hole1
  ellipse(415,365,40,40);  //hole2
  ellipse(85,295,40,40);  //hole3
  ellipse(445,155,40,40);  //hole4
  ellipse(410,95,35,35);  //hole5
  //start
  fill(200);ellipse(510,715,60,60);
  fill(0);textSize(20);text("Start",490,720);
  //timer
  textSize(25);text("Time:"+time,450,40);
}

👀實體裝置





👀展示結果








https://youtu.be/kFIuPNTb-sw


2018年1月8日 星期一

Week17 是賴小沫的

✎期末作品

👀製作實體裝置

準備材料:
剪刀*1
美工刀*1
膠帶*1
紙盒*1
薄紙板數張
彈珠*1

尚未挖洞的實體關卡裝置

2017年12月25日 星期一

Week15 是賴小沫的

✎光敏電阻

👀透過光敏電阻改變燈泡顏色

Arduino_ColorMixingLamp範例檔
/*
  Arduino Starter Kit example
 Project 4  - Color Mixing Lamp

 This sketch is written to accompany Project 3 in the
 Arduino Starter Kit

 Parts required:
 1 RGB LED
 three 10 kilohm resistors
 3 220 ohm resistors
 3 photoresistors
 red green and blue colored gels

 Created 13 September 2012
 Modified 14 November 2012
 by Scott Fitzgerald
 Thanks to Federico Vanzati for improvements

 http://www.arduino.cc/starterKit
 This example code is part of the public domain
 */

const int greenLEDPin = 9;    // LED connected to digital pin 9
const int redLEDPin = 10;     // LED connected to digital pin 10
const int blueLEDPin = 11;    // LED connected to digital pin 11
const int redSensorPin = A0;  // pin with the photoresistor with the red gel
const int greenSensorPin = A1;   // pin with the photoresistor with the green gel
const int blueSensorPin = A2;   // pin with the photoresistor with the blue gel
int redValue = 0; // value to write to the red LED
int greenValue = 0; // value to write to the green LED
int blueValue = 0; // value to write to the blue LED
int redSensorValue = 0; // variable to hold the value from the red sensor
int greenSensorValue = 0; // variable to hold the value from the green sensor
int blueSensorValue = 0; // variable to hold the value from the blue sensor
void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
  // set the digital pins as outputs
  pinMode(greenLEDPin, OUTPUT);
  pinMode(redLEDPin, OUTPUT);
  pinMode(blueLEDPin, OUTPUT);
}
void loop() {
  // Read the sensors first:
  // read the value from the red-filtered photoresistor:
  redSensorValue = analogRead(redSensorPin);
  // give the ADC a moment to settle
  delay(5);
  // read the value from the green-filtered photoresistor:
  greenSensorValue = analogRead(greenSensorPin);
  // give the ADC a moment to settle
  delay(5);
  // read the value from the blue-filtered photoresistor:
  blueSensorValue = analogRead(blueSensorPin);
  // print out the values to the serial monitor
  Serial.print("raw sensor Values \t red: ");
  Serial.print(redSensorValue);
  Serial.print("\t green: ");
  Serial.print(greenSensorValue);
  Serial.print("\t Blue: ");
  Serial.println(blueSensorValue);
  /*
  In order to use the values from the sensor for the LED,
  you need to do some math. The ADC provides a 10-bit number,
  but analogWrite() uses 8 bits. You'll want to divide your
  sensor readings by 4 to keep them in range of the output.
  */
  redValue = redSensorValue / 4;
  greenValue = greenSensorValue / 4;
  blueValue = blueSensorValue / 4;
  //  print out the mapped values
  Serial.print("Mapped sensor Values \t red: ");
  Serial.print(redValue);
  Serial.print("\t green: ");
  Serial.print(greenValue);
  Serial.print("\t Blue: ");
  Serial.println(blueValue);
  /*
  Now that you have a usable value, it's time to PWM the LED.
  */
  analogWrite(redLEDPin, redValue);
  analogWrite(greenLEDPin, greenValue);
  analogWrite(blueLEDPin, blueValue);
}

👀組裝

👀測試結果
此為所有光敏電阻照射到光的結果
 此為控制藍色數值的光敏電阻照射到光的結果
 此為控制紅色數值的光敏電阻照射到光的結果
 此為控制綠色數值的光敏電阻照射到光的結果

✎期末作品

👀關卡設計

2017年12月18日 星期一

Week14 是賴小沫的

✎期末作品討論

👀討論主題

由於期中作品不適合進階為互動裝置,故更換主題,以下為討論的主題

吉他:
螢幕顯示和弦按法,並根據玩家彈奏後切換下一個和弦,用來學如何彈吉他

平衡球:
球在軌道上滾動,當球掉進洞裡代表失敗,到達終點代表成功,並計算使用秒數

123木頭人:
用腳踏的方式前進,搖桿控制方向,閃避前方的障礙物

👀決定主題

經過討論後,決定平衡球為期末作品題目

2017年12月11日 星期一

Week13 是賴小沫的

✎旋轉編碼器

👀參考他人的程式碼,並測試其結果

Arduino

#define SERIAL_BAUDRATE 115200
#define CLK_PIN 2 // 定義連接腳位
#define DT_PIN 3
#define SW_PIN 4
#define interruptA 0 // UNO腳位2是interrupt 0,其他板子請見官方網頁
volatile long count = 0;
unsigned long t = 0;
void setup() {
  Serial.begin(SERIAL_BAUDRATE);
  // 當狀態下降時,代表旋轉編碼器被轉動了
  attachInterrupt(interruptA, rotaryEncoderChanged, FALLING);
  pinMode(CLK_PIN, INPUT_PULLUP); // 輸入模式並啟用內建上拉電阻
  pinMode(DT_PIN, INPUT_PULLUP);
  pinMode(SW_PIN, INPUT_PULLUP);
}
void loop(){
  if(digitalRead(SW_PIN) == LOW){ // 按下開關,歸零
    count = 0; 
    Serial.println("count reset to 0");
    delay(300);
 }
}
void rotaryEncoderChanged(){ // when CLK_PIN is FALLING
  unsigned long temp = millis();
  if(temp - t < 200) // 去彈跳
    return;
  t = temp;
  // DT_PIN的狀態代表正轉或逆轉
  count += digitalRead(DT_PIN) == HIGH ? 1 : -1;
  Serial.println(count);
}

將程式上傳至開發板,並觀看其執行結果

👀如何連接UNO與旋轉編碼器

參考此網站,並連接UNO與旋轉編碼器

示意圖
實際組裝

✎旋轉編碼器連結processing

👀processing程式碼

import processing.serial.*;
Serial myPort;
void setup(){
  size(500,500);
  myPort = new Serial(this,"COM7",115200);
}
int x=0;
int lf=10; //line feed,new line
void draw(){
  background(0);
  if(myPort.available()>0){
    String now = myPort.readStringUntil(lf);
    x=int(split(now,'\r')[0]);
    println(x+" "+now);
  }
  ellipse(width/2+x*20,height/2,100,100);
}

按下左上角的三角形或Ctrl+R,即可執行看結果

✎knob馬達

👀組裝

示意圖
實際組裝

👀程式碼-開啟範例檔Knob


將程式上傳至開發版,觀察操作結果

執行結果如下

2017年12月4日 星期一

Week12 是賴小沫的

所

✎Arduino COM Drivers讀取設定

👀接上Mega板

👀找到無法辨識的裝置

從我的電腦裡找到系統內容的選項
 開啟裝置管理員
 找到無法辨識的裝置
 點擊滑鼠右鍵,更新驅動程式軟體
 手動選擇驅動程式軟體
 設置Arduino資料夾中的drivers資料夾為路徑,勾選包含子資料夾
 要記得勾選永遠信任這個選項

 更新完畢後,便可偵測到該裝置
 開啟Arduino
 設定開發版及序列埠(根據使用的板子及讀取到的COM做設定)

✎搖桿裝置

👀組裝

示意圖
 實際組裝
 👀程式碼
複製官方提供的程式碼

Arduino
將剛剛複製的程式碼貼上

上傳至Mega板,觀看其結果

Processing
讓Arduino所顯示的結果,可以顯示在processing

按下左上角的三角形或Ctrl+R,即可執行看結果

因為結果為亂碼,故須修改程式碼內容
讓讀取到的資料轉為字串

按下左上角的三角形或Ctrl+R,即可執行看結果

✎搖桿按鈕應用

👀Arduino

只輸出搖桿按鈕的資料

👀Processing
利用按鈕來控制背景顏色

按下左上角的三角形或Ctrl+R,即可執行看結果

按下按鈕,背景切為紅色
 放開按鈕,背景切成綠色
✎搖桿XY軸應用
👀Arduino
讀取搖桿X軸與Y軸的數值

👀Processing
透過搖桿來控制圓的位置

按下左上角的三角形或Ctrl+R,即可執行看結果