голосовой контроль RGB пикселей
- Войдите на сайт для отправки комментариев
Ср, 01/05/2013 - 15:44
Доброго дня.
Имеется в наличии следующий голосовой модуль: http://www.ebay.com/itm/Voice-Recognition-Module-Arduino-Compatible-with-USB-TTL-module-/130766424140?pt=LH_DefaultDomain_0&hash=item1e7249484c
Arduino UNO, Adafruit RGB pixels: http://learn.adafruit.com/36mm-led-pixels и готовая библиотека для них.
Хочу управлять голосом RBG пикселями. (Для начала просто: говорю red - загораются все красным и жду дальнейших указаний, говорю blue - пикселю горят синим и ждут указаний.
Голосовые команды на модуль закинул. Не могу догнать пока как программные части между собой соединить.
Код для голосового модуля: int redPin = 11; // R petal on RGB LED module connected to digital pin 11 int greenPin = 9; // G petal on RGB LED module connected to digital pin 9 int bluePin = 10; // B petal on RGB LED module connected to digital pin 10 byte com = 0; //reply from voice recognition void setup() { Serial.begin(9600); pinMode(redPin, OUTPUT); // sets the redPin to be an output pinMode(greenPin, OUTPUT); // sets the greenPin to be an output pinMode(bluePin, OUTPUT); // sets the bluePin to be an output delay(2000); Serial.write(0xAA); Serial.write(0x37); delay(1000); Serial.write(0xAA); Serial.write(0x21); } void loop() // run over and over again { while(Serial.available()) { com = Serial.read(); switch(com) { case 0x11: color(255,255,255); // turn RGB LED on -- white break; case 0x12: color(255, 0, 0); // turn the RGB LED red break; case 0x13: color(0,255, 0); // turn the RGB LED green break; case 0x14: color(0, 0, 255); // turn the RGB LED blue break; case 0x15: color(0,0,0); // turn the RGB LED off break; } } } void color (unsigned char red, unsigned char green, unsigned char blue) // the color generating function { analogWrite(redPin, red*102/255); analogWrite(bluePin, blue*173/255); analogWrite(greenPin, green*173/255); } #include "SPI.h" #include "Adafruit_WS2801.h" int light_data = 2; // Yellow wire on Adafruit Pixels int light_clk = 3; // Green wire on Adafruit Pixels WS2801 strip = WS2801(20, light_data, light_clk); void setup() { // Initialize Serial Connection (for debugging) Serial.begin(9600); //Initialize strip strip.begin(); // Update LED contents, to start they are all 'off' strip.show(); } void loop() { // Some example procedures showing how to display to the pixels colorWipe(Color(255, 0, 0), 50); colorWipe(Color(0, 255, 0), 50); colorWipe(Color(0, 0, 255), 50); rainbow(20); rainbowCycle(20); } void rainbow(uint8_t wait) { int i, j; for (j=0; j < 256; j++) { // 3 cycles of all 256 colors in the wheel for (i=0; i < strip.numPixels(); i++) { strip.setPixelColor(i, Wheel( (i + j) % 255)); } strip.show(); // write all the pixels out delay(wait); } } // Slightly different, this one makes the rainbow wheel equally distributed // along the chain void rainbowCycle(uint8_t wait) { int i, j; for (j=0; j < 256 * 5; j++) { // 5 cycles of all 25 colors in the wheel for (i=0; i < strip.numPixels(); i++) { // tricky math! we use each pixel as a fraction of the full 96-color wheel // (thats the i / strip.numPixels() part) // Then add in j which makes the colors go around per pixel // the % 96 is to make the wheel cycle around strip.setPixelColor(i, Wheel( ((i * 256 / strip.numPixels()) + j) % 256) ); } strip.show(); // write all the pixels out delay(wait); } } // fill the dots one after the other with said color // good for testing purposes void colorWipe(uint32_t c, uint8_t wait) { int i; for (i=0; i < strip.numPixels(); i++) { strip.setPixelColor(i, c); strip.show(); delay(wait); } } /* Helper functions */ // Create a 24 bit color value from R,G,B uint32_t Color(byte r, byte g, byte b) { uint32_t c; c = r; c <<= 8; c |= g; c <<= 8; c |= b; return c; } //Input a value 0 to 255 to get a color value. //The colours are a transition r - g -b - back to r uint32_t Wheel(byte WheelPos) { if (WheelPos < 85) { return Color(WheelPos * 3, 255 - WheelPos * 3, 0); } else if (WheelPos < 170) { WheelPos -= 85; return Color(255 - WheelPos * 3, 0, WheelPos * 3); } else { WheelPos -= 170; return Color(0, WheelPos * 3, 255 - WheelPos * 3); } }примерно так:
#include <SPI.h> #include <Adafruit_WS2801.h> int light_data = 2; // Yellow wire on Adafruit Pixels int light_clk = 3; // Green wire on Adafruit Pixels Adafruit_WS2801 strip = Adafruit_WS2801(20, light_data, light_clk); //Код для голосового модуля: /* int redPin = 11; // R petal on RGB LED module connected to digital pin 11 int greenPin = 9; // G petal on RGB LED module connected to digital pin 9 int bluePin = 10; // B petal on RGB LED module connected to digital pin 10 */ byte com = 0; //reply from voice recognition /*void color (unsigned char red, unsigned char green, unsigned char blue) // the color generating function { analogWrite(redPin, red*102/255); analogWrite(bluePin, blue*173/255); analogWrite(greenPin, green*173/255); } */ void setup() { Serial.begin(9600); //pinMode(redPin, OUTPUT); // sets the redPin to be an output //pinMode(greenPin, OUTPUT); // sets the greenPin to be an output //pinMode(bluePin, OUTPUT); // sets the bluePin to be an output delay(2000); Serial.write(0xAA); Serial.write(0x37); delay(1000); Serial.write(0xAA); Serial.write(0x21); /// //Initialize strip strip.begin(); // Update LED contents, to start they are all 'off' strip.show(); } ///////////// void loop() // run over and over again { while(Serial.available()) { com = Serial.read(); switch(com) { case 0x11: //color(255,255,255); // turn RGB LED on -- white rainbow(20); rainbowCycle(20); break; case 0x12: colorWipe(Color(255, 0, 0), 50); //color(255, 0, 0); // turn the RGB LED red break; case 0x13: colorWipe(Color(0, 255, 0), 50); //color(0,255, 0); // turn the RGB LED green break; case 0x14: colorWipe(Color(0, 0, 255), 50); //color(0, 0, 255); // turn the RGB LED blue break; case 0x15: //color(0,0,0); // turn the RGB LED off break; } } } void rainbow(uint8_t wait) { int i, j; for (j=0; j < 256; j++) { // 3 cycles of all 256 colors in the wheel for (i=0; i < strip.numPixels(); i++) { strip.setPixelColor(i, Wheel( (i + j) % 255)); } strip.show(); // write all the pixels out delay(wait); } } // Slightly different, this one makes the rainbow wheel equally distributed // along the chain void rainbowCycle(uint8_t wait) { int i, j; for (j=0; j < 256 * 5; j++) { // 5 cycles of all 25 colors in the wheel for (i=0; i < strip.numPixels(); i++) { // tricky math! we use each pixel as a fraction of the full 96-color wheel // (thats the i / strip.numPixels() part) // Then add in j which makes the colors go around per pixel // the % 96 is to make the wheel cycle around strip.setPixelColor(i, Wheel( ((i * 256 / strip.numPixels()) + j) % 256) ); } strip.show(); // write all the pixels out delay(wait); } } // fill the dots one after the other with said color // good for testing purposes void colorWipe(uint32_t c, uint8_t wait) { int i; for (i=0; i < strip.numPixels(); i++) { strip.setPixelColor(i, c); strip.show(); delay(wait); } } /* Helper functions */ // Create a 24 bit color value from R,G,B uint32_t Color(byte r, byte g, byte b) { uint32_t c; c = r; c <<= 8; c |= g; c <<= 8; c |= b; return c; } //Input a value 0 to 255 to get a color value. //The colours are a transition r - g -b - back to r uint32_t Wheel(byte WheelPos) { if (WheelPos < 85) { return Color(WheelPos * 3, 255 - WheelPos * 3, 0); } else if (WheelPos < 170) { WheelPos -= 85; return Color(255 - WheelPos * 3, 0, WheelPos * 3); } else { WheelPos -= 170; return Color(0, WheelPos * 3, 255 - WheelPos * 3); } }вашу библиотеку adafruit не нашел, использовал эту:
http://learn.adafruit.com/12mm-led-pixels/code
примерно так:
Эту часть он видит как комментарий, её так и оставлять?
да, это теперь не нужно. это из примера к голосовой плате для включения светодиодов, теперь должны "пиксели" включаться
да, это теперь не нужно. это из примера к голосовой плате для включения светодиодов, теперь должны "пиксели" включаться
Включаются, спасибо. но правда в разнобой. Проверю заново запись команд.
на белый повесил радугу.
цвета вроде правильно должны включаться, но с библиотекой не разбирался, может там порядок rgb перепутан.
код "0x15" - не задействован
А как сделать цвет пульсирующим. К примеру красный, который просто загорается во всю длину... Быстрее, медленее ?
примерно так:
void loop() { redFlash(10) ; } void redFlash(uint8_t wait) { for(int b=1;b<255;b++){ showColor(Color(b,0,0),wait); } for(int b=254;b>0;b--){ showColor(Color(b,0,0),wait); } } void showColor(uint32_t c, uint8_t wait){ for (int i=0; i < strip.numPixels(); i++) { strip.setPixelColor(i, c); } strip.show(); delay(wait); } // Create a 24 bit color value from R,G,B uint32_t Color(byte r, byte g, byte b) { uint32_t c; c = r; c <<= 8; c |= g; c <<= 8; c |= b; return c; }