Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arduino 如何通过OSC消息打开/关闭NeoPixels LED动画?_Arduino_Arduino Esp8266_Osc - Fatal编程技术网

Arduino 如何通过OSC消息打开/关闭NeoPixels LED动画?

Arduino 如何通过OSC消息打开/关闭NeoPixels LED动画?,arduino,arduino-esp8266,osc,Arduino,Arduino Esp8266,Osc,目前,我正试图找出如何使用OSC控制Adafruit Neopix条带。更具体地说,我使用TouchOSC通过WiFi从连接到Neopix的NodeMCU ESP8266微控制器发送/接收消息 我下载了一些其他人制作的测试代码,用于监听OSC消息,以打开/关闭NodeMCU板上的微型LED。当控制器接收到该消息时,它会将该消息发送回TouchOSC客户端,以告知灯是否亮起(这是一个切换按钮)。这一切都很好 我已经编写了一个简单的函数,可以为连接到NodeMCU板的Neopix LED条设置动画。

目前,我正试图找出如何使用OSC控制Adafruit Neopix条带。更具体地说,我使用TouchOSC通过WiFi从连接到Neopix的NodeMCU ESP8266微控制器发送/接收消息

我下载了一些其他人制作的测试代码,用于监听OSC消息,以打开/关闭NodeMCU板上的微型LED。当控制器接收到该消息时,它会将该消息发送回TouchOSC客户端,以告知灯是否亮起(这是一个切换按钮)。这一切都很好

我已经编写了一个简单的函数,可以为连接到NodeMCU板的Neopix LED条设置动画。就其本身而言,这也非常有效

我一直在努力想办法让我的函数[名为gwBlink()]在LED打开时循环运行

我已经附上了密码。有人能告诉我我做错了什么吗??我将永远感激任何帮助!!:)

TouchOSC界面:
/**
 * Send and receive OSC messages between NodeMCU and another OSC speaking device.
 * Send Case: Press a physical button (connected to NodeMCU) and get informed about it on your smartphone screen
 * Receive Case: Switch an LED (connected to NodeMCU) on or off via Smartphone
 * Created by Fabian Fiess in November 2016
 * Inspired by Oscuino Library Examples, Make Magazine 12/2015
 */

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <OSCMessage.h>                // for sending OSC messages
#include <OSCBundle.h>                 // for receiving OSC messages
#include <Adafruit_NeoPixel.h>

#define PIN 3

Adafruit_NeoPixel strip = Adafruit_NeoPixel(58, 3, NEO_GRB + NEO_KHZ800);

char ssid[] = "XXX";                 // your network SSID (name)
char pass[] = "XXX";              // your network password

// Button Input + LED Output
const int btnPin = 12;                 // D6 pin at NodeMCU
const int ledPin = 14;                 // D5 pin at NodeMCU
const int boardLed = LED_BUILTIN;      // Builtin LED

boolean btnChanged = false;
int btnVal = 1;  

WiFiUDP Udp;                           // A UDP instance to let us send and receive packets over UDP
const IPAddress destIp(192,168,0,10);   // remote IP of the target device (i.e. THE PHONE)
const unsigned int destPort = 12000;    // remote port of the target device where the NodeMCU sends OSC to
const unsigned int localPort = 10000;   // local port to listen for UDP packets at the NodeMCU (another device must send OSC messages to this port)

unsigned int ledState = 1;             // LOW means led is *on*

void setup() {
    strip.begin();
    strip.show();
    Serial.begin(115200);

    // Specify a static IP address for NodeMCU - only needeed for receiving messages)
    // If you erase this line, your ESP8266 will get a dynamic IP address
    WiFi.config(IPAddress(192,168,0,13),IPAddress(192,168,0,1), IPAddress(255,255,255,0)); 

    // Connect to WiFi network
    Serial.println();
    Serial.print("Connecting to ");
    Serial.println(ssid);
    WiFi.begin(ssid, pass);

    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }

    Serial.println("");
    Serial.println("WiFi connected");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());

    Serial.println("Starting UDP");
    Udp.begin(localPort);
    Serial.print("Local port: ");
    Serial.println(Udp.localPort());

    // btnInput + LED Output
    pinMode(btnPin, INPUT);
    pinMode(ledPin, OUTPUT);
    pinMode(boardLed, OUTPUT); 
}

void loop() {
    receiveOSC();
    sendOSC();
}

void sendOSC(){
    // read btnInput and send OSC
    OSCMessage msgOut("/1/buttonListener");

    if(digitalRead(btnPin) != btnVal) {
        btnChanged = true;
        btnVal = digitalRead(btnPin);
    }

    if(btnChanged == true){
        btnChanged = false;
        digitalWrite(ledPin, btnVal);
        digitalWrite(boardLed, (btnVal + 1) % 2);    // strange, but for the builtin LED 0 means on, 1 means off
        Serial.print("Button: ");
        Serial.println(btnVal);
        msgOut.add(btnVal);
    }

    Udp.beginPacket(destIp, destPort);
    msgOut.send(Udp);
    Udp.endPacket();
    msgOut.empty();
    delay(100);
}

void receiveOSC(){
    OSCMessage msgIN;
    int size;
    if((size = Udp.parsePacket())>0){
        while(size--)
            msgIN.fill(Udp.read());
        if(!msgIN.hasError()){
            msgIN.route("/1/toggleLED",toggleOnOff);

        }
    }
}

void gwBlink() {
  // LED strip animation
  uint16_t i, j;
  for(i = 0; i < strip.numPixels(); i = i + 2) {
    strip.setPixelColor(i, 0, 182, 90);
  }
  for(j = 1; j < strip.numPixels(); j = j + 2) {
    strip.setPixelColor(j, 128, 128, 128);
  }
  strip.show();
  delay(100);

  for(i = 0; i < strip.numPixels(); i = i + 2) {
    strip.setPixelColor(i, 128, 128, 128);
  }

  for(j = 1; j < strip.numPixels(); j = j + 2) {
    strip.setPixelColor(j, 0, 182, 90);
  }
  strip.show();
  delay(100);
}

void toggleOnOff(OSCMessage &msg, int addrOffset){
  ledState = (boolean) msg.getFloat(0);

  digitalWrite(boardLed, (ledState + 1) % 2);   // Onboard LED works the wrong direction (1 = 0 bzw. 0 = 1)
  digitalWrite(ledPin, ledState);               // External LED

  if (ledState) {
    Serial.println("LED on");
    gwBlink();
  }
  else {
    Serial.println("LED off");
    strip.clear();
  }
  ledState = !ledState;                         // toggle the state from HIGH to LOW to HIGH to LOW ...
}
/**
*在NodeMCU和另一个OSC语音设备之间发送和接收OSC消息。
*发送案例:按下一个物理按钮(连接到NodeMCU)并在智能手机屏幕上获得相关信息
*接收案例:通过智能手机打开或关闭LED(连接到NodeMCU)
*由Fabian Fiess于2016年11月创建
*《Make》杂志2015年12月,灵感来源于奥西诺图书馆的例子
*/
#包括
#包括
#包含//用于发送OSC消息
#包含//用于接收OSC消息
#包括
#定义引脚3
Adafruit_Neopix条=Adafruit_Neopix条(58,3,NEO_GRB+NEO_KHZ800);
字符ssid[]=“XXX”;//您的网络SSID(名称)
字符传递[]=“XXX”;//您的网络密码
//按钮输入+LED输出
常数int btnPin=12;//节点处的D6引脚
常数int ledPin=14;//节点处的D5引脚
const int boardLed=LED_内置;//内置发光二极管
布尔值btnChanged=false;
int btnVal=1;
WiFiUDP Udp;//一个UDP实例,允许我们通过UDP发送和接收数据包
警察地址destIp(192168,0,10);//目标设备(即电话)的远程IP
常量unsigned int destPort=12000;//NodeMCU向其发送OSC的目标设备的远程端口
常量unsigned int localPort=10000;//用于在NodeMCU上侦听UDP数据包的本地端口(另一个设备必须将OSC消息发送到此端口)
无符号int-ledState=1;//低表示指示灯*亮起*
无效设置(){
strip.begin();
strip.show();
序列号开始(115200);
//为NodeMCU指定静态IP地址(仅接收消息时需要)
//如果删除此行,您的ESP8266将获得一个动态IP地址
WiFi.config(IPAddress(192168,0,13)、IPAddress(192168,0,1)、IPAddress(255255,0));
//连接到WiFi网络
Serial.println();
串行打印(“连接到”);
序列号println(ssid);
WiFi.begin(ssid,pass);
while(WiFi.status()!=WL_已连接){
延迟(500);
连续打印(“.”);
}
Serial.println(“”);
Serial.println(“WiFi连接”);
Serial.println(“IP地址:”);
Serial.println(WiFi.localIP());
Serial.println(“启动UDP”);
开始(localPort);
串行打印(“本地端口:”);
Serial.println(Udp.localPort());
//btnInput+LED输出
引脚模式(btnPin,输入);
引脚模式(LED引脚,输出);
引脚模式(带板,输出);
}
void循环(){
receiveOSC();
sendOSC();
}
void sendOSC(){
//读取btnInput并发送OSC
OSCMessage msgOut(“/1/buttonListener”);
如果(数字读取(btnPin)!=btnVal){
btnChanged=真;
btnVal=数字读取(btnPin);
}
如果(btnChanged==true){
btnChanged=假;
数字写入(ledPin、btnVal);
digitalWrite(带板,(btnVal+1)%2);//奇怪,但对于内置LED,0表示打开,1表示关闭
串行打印(“按钮:”);
序列号println(btnVal);
msgOut.add(btnVal);
}
Udp.beginPacket(destp,destPort);
msgOut.send(Udp);
Udp.endPacket();
msgOut.empty();
延迟(100);
}
无效接收OSC(){
oscmsgin;
整数大小;
如果((size=Udp.parsePacket())>0){
而(大小--)
msgIN.fill(Udp.read());
如果(!msgIN.hasError()){
msgIN.route(“/1/toggled”,toggleOnOff”);
}
}
}
void gwBlink(){
//LED条形动画
uint16_t i,j;
对于(i=0;i

这段代码实际上可以很好地打开/关闭电路板上的小LED,但我的动画功能没有被触发。

我只使用Neopix很短时间,但在尝试同时使用它们和串行监视器时遇到了问题。一旦我禁用了串行命令(//serial.begin(115200);),它们就工作得很好。文档中没有明确说明这一点,我在一个线程中发现了它,Adafruit管理员在尝试时提到使用串行
*if (Serial.available()) {
int inByte = Serial.read();
Serial1.print(inByte, DEC);
}*