Close

Week 14

A project log for Dynamic Interactive - Urbanscapes

This is a school project that follows Urbanscapes Malaysia brief, to create an interactive design related to Malaysia art and culture.

aprilmomo0416aprilmomo0416 12/20/2018 at 04:520 Comments

20th Dec, 2018- Thursday

Final Submission

We get our audio play on trigger by using Processing, and here is the code we used:

Arduino:

const int BtnFanIN = 2;
const int TSHerb1IN = 3;
const int TSHerb2IN = 4;
const int BtnHorseIN = 5;
const int TSDrinks1IN = 6;
const int TSDrinks2IN = 7;


const int BtnFanMOTOR = 8;
//const int TSHerb1AUD = 7;
//const int TSHerb2AUD = 8;
const int BtnHorseMOTOR = 8;
//const int BtnHorseAUD = 10
//const int TSDrinksAUD = 11;  

bool TSHerb1_1 = false;
bool TSHerb1_2 = false;

bool TSHerb2_1 = false;
bool TSHerb2_2 = false;

bool TSDrinks1_1 = false;
bool TSDrinks1_2 = false;

bool TSDrinks2_1 = false;
bool TSDrinks2_2 = false;

 
 void setup() {

 Serial.begin(9600);                    // Start serial communication at 9600 bps

 pinMode(BtnFanIN, INPUT);   // Set pin 0 as an input
 pinMode(TSHerb1IN, INPUT);   
 pinMode(TSHerb2IN, INPUT);   
 pinMode(BtnHorseIN, INPUT);   
 pinMode(TSDrinks1IN, INPUT);  
 pinMode(TSDrinks2IN, INPUT);  

 digitalWrite(TSHerb1IN,HIGH);
 digitalWrite(TSHerb2IN,HIGH);
 digitalWrite(TSDrinks1IN,HIGH);
 digitalWrite(TSDrinks2IN,HIGH);

 pinMode(BtnFanMOTOR, OUTPUT);   
 //pinMode(TSHerb1AUD, OUTPUT);   
 //pinMode(TSHerb2AUD, OUTPUT);   
 pinMode(BtnHorseMOTOR, OUTPUT);   
 //pinMode(BtnHorseAUD, OUTPUT);   
 //pinMode(TSDrinksAUD, OUTPUT);   

 }
 
 void loop() { 
 if (digitalRead(BtnFanIN) == HIGH) {  // If switch is ON,
      Serial.println("fanOn");
      digitalWrite (BtnFanMOTOR, HIGH);
      delay(1800);
      //Serial.write(0);               // send 1 to Processing
      delay(100);                           
   } else {
      digitalWrite (BtnFanMOTOR,LOW);
   }

   
 if(digitalRead(BtnHorseIN) == HIGH){  // If switch is ON,
      Serial.println("horseOn");
      analogWrite(BtnHorseMOTOR,50);
      Serial.write(1);               // send 1 to Processing
      delay(100);                           
   } else {
      analogWrite (BtnHorseMOTOR,0);
   }

 if(digitalRead(TSHerb1IN) == HIGH ){
    TSHerb1_1 = true;
    checkTouch();
    TSHerb1_2 = TSHerb1_1;
    Serial.println("drawer1");
  } else{
    TSHerb1_1 = false;
    TSHerb1_2 = TSHerb1_1;
  }

  if(digitalRead(TSHerb2IN) == HIGH ){
    TSHerb2_1 = true;
    checkTouch();
    TSHerb2_2 = TSHerb2_1;
    Serial.println("drawer2");
  } else{
    TSHerb2_1 = false;
    TSHerb2_2 = TSHerb2_1;
  }

 if(digitalRead(TSDrinks1IN) == HIGH ){
    TSDrinks1_1 = true;
    checkTouch();
    TSDrinks1_2 = TSDrinks1_1;
    Serial.println("door1");
  } else{
    TSDrinks1_1 = false;
    TSDrinks1_2 = TSDrinks1_1;
  }

 if(digitalRead(TSDrinks2IN) == HIGH ){
    TSDrinks2_1 = true;
    checkTouch();
    TSDrinks2_2 = TSDrinks2_1;
    Serial.println("door2");
  } else{
    TSDrinks2_1 = false;
    TSDrinks2_2 = TSDrinks2_1;
  }
 }


void checkTouch(){
    if(TSHerb1_1 != TSHerb1_2){
      Serial.write(2);               // send 1 to Processing
      Serial.println("MED 1");
      }

      if(TSHerb2_1 != TSHerb2_2){
      Serial.write(3);               // send 1 to Processing
      Serial.println("MED 2");
      }
      
    if(TSDrinks1_1 != TSDrinks1_2){
      Serial.write(4);               // send 1 to Processing
//      ("doorOpen");    
      Serial.println("DOOR OPEN");
    }

    if(TSDrinks2_1 != TSDrinks2_2){
      Serial.write(4);   // send 1 to Processing
//      ("doorOpen");    
      Serial.println("DOOR 2 OPEN");

      }
  }

Processing:

import processing.sound.*;
import processing.serial.*;

Serial myPort;
int val;

SoundFile BGSound;
SoundFile[] Convos;

void setup() {
    printArray(Serial.list());
    String portName = Serial.list()[2];
    myPort = new Serial(this,portName,9600);
    
    BGSound = new SoundFile(this, "BG.mp3"); 
    
    Convos = new SoundFile[4];
    for (int i = 0; i < Convos.length; i ++) {
       Convos[i] = new SoundFile(this, (i + 1)+".mp3");
    }

    BGSound.loop();
}

void draw() {
  //checkSound();
  if ( myPort.available() > 0) {
    val = myPort.read();   
    checkSound();
    switch(val) {
      case 0:
         fadeOut();
         //BGSound.amp(0.3);
         playSound(0);
        break;
      case 1: 
         fadeOut();
         //BGSound.amp(0.3);
         playSound(1);
         //BGSound.amp(1.0);
         //fadeIn();
        break;
      case 2: 
         fadeOut();
         playSound(2);
         //fadeIn();
        break;
      case 3: 
         fadeOut();
         playSound(3);
         //fadeIn();
        break;
      //default:
      //  break;
    }
  }
}

void playSound(int index) {
  for (int i = 0; i < Convos.length; i++) {
    if (Convos[i].isPlaying()) {
     return; 
    }
  }
  if (!Convos[index].isPlaying()){
    Convos[index].play();    
  }
}

void checkSound() {
  for (int i = 0; i < Convos.length; i++) {
    if (!Convos[i].isPlaying()) {
      BGSound.amp(1.0);
      //fadeOut();
    }
  }
}

void fadeOut(){
  for (float i = 1.0 ; i > 0.05; i -= 0.1){
    BGSound.amp(i);
    delay(25);
  } 
}

void fadeIn(){
  for (float i = 0.05 ; i <= 1.0; i += 0.1){
    BGSound.amp(i);
    delay(25);
  } 
}

Today is the submission day for our project, we have to upload all the code and present the built as a complete work. Unfortunately due to Arduino port keep disconnecting from the computer, processing can't receive data from Arduino IO board, and we fail to showcase our work in the end. 

We tested the code right before the submission and it is actually working, but we have no idea but end this without a complete work that people can interact with.

Even it is not working for the submission, but our lecturer and tutor did appreciate our effort on this project. Me and Pei Ce, my team mate chose to make every single props by hand, and those tiny props is very time consuming to make and need efforts to make it looks good. 

We are happy with the visual presented today but it is a waste to not able to show the interactive part.

Discussions