情人节马上就到了,朋友圈即将被秀恩爱攻占,然而情人节的我依然是一只狗做了一个互动装置来表达心里苦
(以及快写完的时候没保存真是气死了)
视频:
材料:
数字触摸传感器 df商城
ADXL345三轴加速度传感器 df商城 SEN0032
Flex单向弯曲传感器 df商城 SEN0086
Arduino Uno df商场
IO扩展板 df商城
复模硅胶 淘宝
木板,亚克力板 淘宝
doge贴纸 淘宝
绝缘胶带,铝箔纸 五金店
软件:arduino,processing
“我”制作过程:
做这部分的时候忘记记录了,因为传感器老是出问题[哭]……简单的来说就是用激光切割在很多块亚克力板上切了“我”,然后把它们叠在一起,在下面垫一块完整的亚克力板,这样就出现了一个凹槽,先倒一半的硅胶,等它干了贴绝缘胶带,放传感器,在触摸传感器上贴铝箔纸,再贴绝缘胶带,倒另一半硅胶。
注意在边缘留一点空白,硅胶无法和绝缘胶带黏合。
Arduino代码:
- #include <Wire.h>
-
- #define DEVICE (0x53) //ADXL345 device address
- #define TO_READ (6) //num of bytes we are going to read each time (two bytes for each axis)
-
- byte buff[TO_READ] ; //6 bytes buffer for saving data read from the device
- char str[512]; //string buffer to transform data before sending it to the serial port
- int regAddress = 0x32; //first axis-acceleration-data register on the ADXL345
- int x, y, z; //three axis acceleration data
- double roll = 0.00, pitch = 0.00; //Roll & Pitch are the angles which rotate by the axis X and y
- //in the sequence of R(x-y-z),more info visit
- // <a href="https://www.dfrobot.com/wiki/index.php?title=How_to_Use_a_Three-Axis_Accelerometer_for_Tilt_Sensing#Introduction" target="_blank">https://www.dfrobot.com/wiki/ind ... ensing#Introduction</a>
-
- int sensor1 = 0; // digital sensor
- int sensor2= 0;
- int sensor3 = 0;
- int sensor4 = 0;
- int inByte = 0; // incoming serial byte
- int ledPin = 13; // Connect LED on pin 13, or use the onboard one
- int KEY = 3; // Connect Touch sensor on Digital Pin 2
- int Bend1 = 0;
- int Bend2 = 0;
- String state = "stop0";
- int preState;
-
- void setup(){
- Wire.begin(); // join i2c bus (address optional for master)
- Serial.begin(9600);
- attachInterrupt(0,eventSensor,RISING);
- attachInterrupt(1,eventSensor,RISING);
- //Turning on the ADXL345
- writeTo(DEVICE, 0x2D, 0);
- writeTo(DEVICE, 0x2D, 16);
- writeTo(DEVICE, 0x2D, 8);
-
- pinMode(ledPin, OUTPUT); // Set ledPin to output mode
- pinMode(KEY, INPUT); //Set touch sensor pin to input mode
- pinMode(9, INPUT);
- pinMode(10, INPUT);
- pinMode(11, INPUT);
- }
-
- void loop(){
- sensor1 = digitalRead(KEY);
- sensor2 = digitalRead(9);
- sensor3 = digitalRead(10);
- sensor4 = digitalRead(11);
- Bend1 = analogRead(3);
- Bend2 = analogRead(2);
- readFrom(DEVICE, regAddress, TO_READ, buff); //read the acceleration data from the ADXL345
- //each axis reading comes in 10 bit resolution, ie 2 bytes. Least Significat Byte first!!
- //thus we are converting both bytes in to one int
- x = (((int)buff[1]) << 8) | buff[0];
- y = (((int)buff[3])<< 8) | buff[2];
- z = (((int)buff[5]) << 8) | buff[4];
- Serial.println(sensor3);
- //we send the x y z values as a string to the serial port
- //Serial.print("The acceleration info of x, y, z are:");
- sprintf(str, "%d %d %d %d %d\t",x, y, z, Bend1,Bend2);
- Serial.print(str);
- Serial.println(state);
- if (state == "play")
- {
- delay(2000);
- state = "stop";
- }
-
- //It appears that delay is needed in order not to clog the port
- delay(50);
- }
-
-
-
- //---------------- Functions
- //Writes val to address register on device
- void writeTo(int device, byte address, byte val) {
- Wire.beginTransmission(device); //start transmission to device
- Wire.write(address); // send register address
- Wire.write(val); // send value to write
- Wire.endTransmission(); //end transmission
- }
-
- //reads num bytes starting from address register on device in to buff array
- void readFrom(int device, byte address, int num, byte buff[]) {
- Wire.beginTransmission(device); //start transmission to device
- Wire.write(address); //sends address to read from
- Wire.endTransmission(); //end transmission
-
- Wire.beginTransmission(device); //start transmission to device
- Wire.requestFrom(device, num); // request 6 bytes from device
-
- int i = 0;
- while(Wire.available()) //device may send less than requested (abnormal)
- {
- buff = Wire.read(); // receive a byte
- i++;
- }
- Wire.endTransmission(); //end transmission
- }
-
- //calculate the Roll&Pitch
- void RP_calculate(){
- double x_Buff = float(x);
- double y_Buff = float(y);
- double z_Buff = float(z);
- roll = atan2(y_Buff , z_Buff) * 57.3;
- pitch = atan2((- x_Buff) , sqrt(y_Buff * y_Buff + z_Buff * z_Buff)) * 57.3;
- }
-
- void eventSensor()
- {
- state = "play";
-
- }
-
-
-
-
-
- Processing代码:
- import ddf.minim.*;
- import processing.serial.*;
- Serial myPort; // The serial port
- Minim minim;
-
- AudioPlayer[] touchplayer;
- AudioPlayer[] bendplayer;
- AudioPlayer[] shakeplayer;
-
- PImage img1;// background
- PImage img2;//touch sensor
- PImage img3;//bend sensor
- PImage img4;//accel sensor
-
- String myString = null;
-
- int lf = 10; // Linefeed in ASCII
- int x_cur = 0;
- int x_pre = x_cur; // for accel sensor
- int z_cur = 0;
- int z_pre = z_cur; // for accel sensor
- int bend1=0;
- int bend2=0;
- int index1=0; // for touch sensor
- int index2=0; // for bend sensor
- int index3=0; // for accel sensor
-
-
- void setup() {
- size(1440,900); //size of a MacBook
- printArray(Serial.list()); // List all the available serial ports
- myPort = new Serial(this, Serial.list()[3], 9600); // Open the port you are using at the rate you want:
- myPort.clear();
- // Throw out the first reading, in case we started reading
- // in the middle of a string from the sender.
- myString = myPort.readStringUntil(lf);
- myString = null;
- minim= new Minim(this);
-
- touchplayer=new AudioPlayer[8];
- touchplayer[0]=minim.loadFile("touch.wav");
- touchplayer[1]=minim.loadFile("touch02.wav");
- touchplayer[2]=minim.loadFile("touch03.wav");
- touchplayer[3]=minim.loadFile("touch04.wav");
- touchplayer[4]=minim.loadFile("touch05.wav");
- touchplayer[5]=minim.loadFile("touch06.wav");
- touchplayer[6]=minim.loadFile("touch07.wav");
- touchplayer[7]=minim.loadFile("touch08.wav"); //audio for touch sensors
- for (int i=0;i<8;i++){
- touchplayer.loop();
- touchplayer.mute();
- } //loop and mute all the audio for touch sensors
-
- bendplayer=new AudioPlayer[1];
- bendplayer[0]=minim.loadFile("howl.wav"); // audio for bend sensors
- bendplayer[0].loop();
- bendplayer[0].mute(); //loop and mute all the audio for bend sensors
-
-
- shakeplayer= new AudioPlayer[1];
- shakeplayer[0]=minim.loadFile("shake01.wav"); //audio for shake sensors
- shakeplayer[0].loop();
- shakeplayer[0].mute(); //loop and mute all the audio for shake sensors
-
- img1=loadImage("background1.png");
- img2=loadImage("background2.png");
- img3=loadImage("bend.png");
- img4=loadImage("shake.png"); //load all the backgrounds
- }
-
-
- void draw() {
- while (myPort.available() > 0) {
- myString = myPort.readStringUntil(lf);
- if(myString != null) {
- String[] list = split(myString, "\t");
- if(list.length == 2){
- String state = list[1].replaceAll("[^a-zA-Z]","");
- if (state.equals("stop")==true){
- touchplayer[index1].rewind();
- touchplayer[index1].mute();
- image(img1,0,0);
- if(index1<6){
- index1++;
- } else{
- index1=0;
- }
- } else{
- println("is touching");
- touchplayer[index1].unmute();
- image(img2,0,0);
- }
- int[] accel=int(split(list[0], " ")); //accelerator=[x,y,z,bend1,bend2];
-
- if(accel.length==5){
- x_cur = accel[1];
- z_cur = accel[2];
-
- bend1 = accel[3];
- bend2 = accel[4];
- println(bend1);
- println(bend2);
- if(bend1>345 ||bend2>=675){
- bendplayer[index2].unmute();
- image(img3,0,0);
- println("is bended");
- } else{
- bendplayer[index2].rewind();
- bendplayer[index2].mute();
- }
-
- if(abs(x_pre - x_cur) > 70 ||abs(z_pre - z_cur)>70){ // two axixs detect
- shakeplayer[0].unmute();
- println("is shaking");
- image(img4,0,0);
- } else{
- shakeplayer[0].rewind();
- shakeplayer[0].mute();
- }
- }
- }
- x_pre = x_cur;
- z_pre = z_cur;
- }
- }
- }
复制代码
这个装置的灵感来源于日本的一个叫“ah”的互动文字装置:https://vimeo.com/52555492
最后,祝大家情人节快乐~单身狗也可以自己找乐~
|