在将序列发送到Arduino方面有所不同

时间:2019-06-11 21:51:04

标签: arduino processing kinect led

我正在建立一个八角形的LED网格。它有8个面,由Kinect上某人的X轴触发。

它向Arduino发送8个串行信号,以触发八边形一侧的LED。这些LED全部为(255,0,0)红色。

我想做到这一点,以便在处理中可以在蓝色,绿色和红色之间切换。有人对如何做这样的事情有任何提示吗?

我当时正在考虑创建三个按钮(R,G,B)并使用鼠标按下以在要发送到Arduino的序列之间变化

唯一的问题是我对处理有点陌生。有人知道如何解决吗?

这是我要处理的代码


import org.openkinect.freenect.*;
import org.openkinect.processing.*;
import processing.serial.*;


// The kinect stuff is happening in another class
KinectTracker tracker;
Kinect kinect;
Serial myPort;

void setup() {
  size(640, 480);
  kinect = new Kinect(this);
  tracker = new KinectTracker();
  String portName = Serial.list()[2]; //change the 0 to a 1 or 2 etc. to match your port
  myPort = new Serial(this, portName, 9600);
}

void draw() {

  tracker.track();
  tracker.display();

  PVector v1 = tracker.getPos();
  fill(50, 100, 250, 200);
  noStroke();
  ellipse(v1.x, v1.y, 50, 50);




   //1
    if (v1.x > 0 && v1.x < 80 ){
    println('8');
    //rect(0,200,200,200);
    myPort.write('8'); }
    //println('0'); }

    //2
  if (v1.x > 80 && v1.x < 160){
    println('7');
    myPort.write('7'); }   
 //3
   if (v1.x > 160 && v1.x < 240 ){
    println('6');
    myPort.write('6'); }

    //4
   if (v1.x > 240 && v1.x < 320 ){
    println('5');
   myPort.write('5'); }

    //5
   if (v1.x > 320 && v1.x < 400 ){
    println('4');
    myPort.write('4'); }

    //6
   if (v1.x > 400 && v1.x < 480 ){
    println('3');
    myPort.write('3'); }

    //7
   if (v1.x > 480 && v1.x < 560 ){
    println('2');
    myPort.write('2'); }

    //8
   if (v1.x > 560 && v1.x < 620 ){
    println('1');
    myPort.write('1'); }
}

这是Arduino代码:


#include <FastLED.h>

#define LED_PIN     6
#define NUM_LEDS    360

CRGB leds[NUM_LEDS];

void setup()
{
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);

  Serial.begin(9600);

  FastLED.clear();
  FastLED.show();
}

void loop()
{

  if (Serial.available() > 0)
  {
    int incomingByte = Serial.read();

    if (incomingByte > '0' && incomingByte < '9')
    {
      FastLED.clear();   //  <-----<<<<    USE clear
    }


    if (incomingByte == '1')
    for(int i = 0; i<=44; i++){
          leds[i] = CRGB(255, 0, 0);
      }
    else if (incomingByte == '2')
    for(int i = 45; i<=89; i++){
          leds[i] = CRGB(255, 0, 0);
    } 
    else if (incomingByte == '3')
    for(int i = 90; i<=134; i++){
          leds[i] = CRGB(255, 0, 0);
    }

  else if (incomingByte == '4')
    for(int i = 135; i<=179; i++){
          leds[i] = CRGB(255, 0, 0);
    }
    else if (incomingByte == '5')
    for(int i = 180; i<=224; i++){
          leds[i] = CRGB(255,0, 0);
    }
    else if (incomingByte == '6')
    for(int i = 225; i<=269; i++){
          leds[i] = CRGB(255, 0, 0);
    }
    else if (incomingByte == '7')
    for(int i = 270; i<=314; i++){
          leds[i] = CRGB(255, 0, 0);
    }
    else if (incomingByte == '8')
    for(int i = 315; i<=359; i++){
          leds[i] = CRGB(255, 0, 0);

 }

    FastLED.show();
  }
}

谢谢大家!

1 个答案:

答案 0 :(得分:0)

如果您需要资源来制作Processing中的GUI,我对ControlP5很幸运。这里有很多例子,这是一种非常时尚的美学。您应该可以使用此处的示例更改通过串行发送的值以选择颜色。