您的位置:首页 > 产品设计 > UI/UE

arduino与processing交互--电位器控制视频播放

2017-01-20 21:21 281 查看
功能:通过arduino读取电位器的值,控制七彩灯带,并把指令传给processing,由processing控制播放具体视频

注意点:arduino print传输的是字符形式,比如说数据是'12'的时候,在processing端使用read只是读取一个字符,而非数字,这里通过bufferUtil函数指定了结束符,实现了数据的正确传输。

具体代码如下:

arduino

#include <Adafruit_NeoPixel.h>

#define NUMPIXELS      30

// use array to store potentionmeter node
// the value should be adjusted by the device
float positionNodeVal[] = {
0, // 0
42.7, // movie clip 1
85.3, // movie clip 2
128.0, // movie clip 3
170.7, // movie clip 4
213.3, // movie clip 5
256.0, // movie clip 6
298.7, // movie clip 7
341.3, // movie clip 8
384.0, // movie clip 9
426.7,
469.3,
1024.0
};

int colorLedPin = 6;
int sensorPin = A0;    // select the input pin for the potentiometer
int ledPin = 13;      // select the pin for the LED
float sensorValue = 0;  // variable to store the value coming from the sensor
int ledNum = 0;
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, colorLedPin, NEO_GRB + NEO_KHZ800);

int delayval = 50; // delay for half a second

int getMovieClipNum(float sensorVal)
{
for (int i = 0; i < sizeof(positionNodeVal)/sizeof(positionNodeVal[0]); i++)
{
if (sensorVal > positionNodeVal[i] && sensorVal < positionNodeVal[i+1])
{
return i;
}
}
return 0; // default value : 0
}

void setup() {
// declare the ledPin as an OUTPUT:
//pinMode(colorLedPin, OUTPUT);
pixels.begin();
Serial.begin(9600);
}

void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
int movieClipNum = getMovieClipNum(sensorValue);
char strToProcessing[3];
sprintf(strToProcessing,"%d",movieClipNum);
//Serial.println(sensorValue);
Serial.print(movieClipNum);
Serial.print('\n');
ledNum = 1.0 * sensorValue / 1024 * 30;
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
for (int i=0;i<NUMPIXELS;i++) {
if (((int)(ledNum)) == i)
pixels.setPixelColor(i, pixels.Color(0,50,0)); // Moderately bright green color.
else
pixels.setPixelColor(i, pixels.Color(0,0,0));
}
pixels.show(); // This sends the updated pixel color to the hardware.

delay(delayval); // Delay for a period of time (in milliseconds).
}


processing端的代码

import processing.video.*;
import processing.serial.*;
import java.util.Map;
import java.util.HashMap;
import java.util.List;

// Step 1. define an object of Movie
Movie movie;
Serial port;
static int cmd_val = 0; // get the value from arduino
float start_t, end_t;
String str_tmp;

// use array to store Movie time
float[][] time_arr = {
{0.1, 0.2}, // movie clip 0
{0.2, 0.3}, // movie clip 1
{0.3, 0.4}, // movie clip 2
{0.4, 0.5}, // movie clip 3
{0.5, 0.6}, // movie clip 4
{0.6, 0.7}, // movie clip 5
{0.7, 0.8}, // movie clip 6
{0.5, 0.6}, // movie clip 7
{0.5, 0.6}, // movie clip 8
{0.5, 0.6}, // movie clip 9
{0.5, 0.6}, // movie clip 10
{0.5, 0.6}, // movie clip 11
};

void setup() {
size(1024, 768);

port = new Serial(this, "COM1", 9600);

port.bufferUntil('\n'); // buffer until meet '\n', then call the event listener

// Step 2. create an instance of Movie
movie = new Movie(this, "2.mov");

// Step 3.
movie.loop();
}

void draw() {

// get value from arduino

int video_f = 1;
if (video_f == 1)
{
// get the length of movie
float md = movie.duration();
start_t = md * time_arr[cmd_val][0];
end_t = md * time_arr[cmd_val][1];
println("start time: ", start_t);
println("end time", end_t);
movie_loop(start_t, end_t);

} else
{
movie.jump(0);
}

// Step 5.
image(movie, 0, 0);
}

// loop movie from start_time to end_time
void movie_loop(float start_time, float end_time)
{
// get the value of current time
float mt = movie.time();
println("current time: ", mt);
if (mt < start_time || mt > end_time)
{
movie.jump(start_time);
}
}

//*
void serialEvent(Serial port) {
String inString = port.readString(); // read all the data include '\n'
String[] list = split(inString, '\n');
cmd_val = int(list[0]);
println("cmd_val: ", cmd_val);
}//*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息