arduino与processing交互--电位器控制视频播放
2017-01-20 21:21
281 查看
功能:通过arduino读取电位器的值,控制七彩灯带,并把指令传给processing,由processing控制播放具体视频
注意点:arduino print传输的是字符形式,比如说数据是'12'的时候,在processing端使用read只是读取一个字符,而非数字,这里通过bufferUtil函数指定了结束符,实现了数据的正确传输。
具体代码如下:
arduino
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); }//*/
相关文章推荐
- 微信Android自动播放视频(可交互,设置层级,无控制条,非X5)ffmpeg,jsmpeg.js,.ts视频
- 不完全解决Android微信HTML5 播放视频的问题(不显示控制条,可交互)
- 从零开始学习音视频编程技术(十) FFMPEG Qt视频播放器之播放控制
- 控制webview使用html5的video播放视频不全屏(inline)的方法
- opencv学习笔记(四)-视频播放控制
- OpenCV视频进度播放控制
- 基于OpenCV 、VS2008 MFC对话框的USB摄像头的控制和视频播放、跟踪(logitech sphere AF网络摄像头)
- UIWebView 通过JS控制页面视频播放
- 从零开始学习音视频编程技术(十) FFMPEG Qt视频播放器之播放控制
- OpenCV初学之视频播放控制
- opencv 之 视频播放控制
- OpenCV笔记(二)实现获取视频并控制视频的播放速度
- OpenCV播放视频并控制位置
- 12.1.2 控制视频播放
- AS3中音频、视频的播放控制
- opencv之视频播放控制
- Arduino学习笔记一,电位器控制步进电机。
- 【opencv学习笔记3】视频播放控制
- arduino学习之led相关--跑马灯+电位器控制led亮灭
- unity3d笔记(4)——声音,视频播放控制