Hello there,
I have a similar problem and I think all the posts above should give me the answer, but I can't seem to find it.
I want to build a light saber for a school project, but haven't really worked with microcontrollers in the past.
When I 'power up' the lightsaber, the LEDs work, but the sound stops playing very shortly after and tries to start again multiple times a second. I tried implementing the things posted above and switched from FastLED to Paul's WS2812Serial library, but I must still do something wrong. The basic test example, included in the library, worked just fine.
I use a Teensy 3.2 with a prop shield (no LC).
I am german, so I hope you understand me. Thank you very much!
Paul
I have a similar problem and I think all the posts above should give me the answer, but I can't seem to find it.
I want to build a light saber for a school project, but haven't really worked with microcontrollers in the past.
When I 'power up' the lightsaber, the LEDs work, but the sound stops playing very shortly after and tries to start again multiple times a second. I tried implementing the things posted above and switched from FastLED to Paul's WS2812Serial library, but I must still do something wrong. The basic test example, included in the library, worked just fine.
I use a Teensy 3.2 with a prop shield (no LC).
I am german, so I hope you understand me. Thank you very much!
Paul
Code:
#include <WS2812Serial.h> //LED
#include <Audio.h> //Audio
#include <NXPMotionSense.h> //Motion
#include <Wire.h> //Audio, Motion
#include <EEPROM.h> //Motion
#include <SPI.h> //Audio
#include <SD.h> //Audio
#include <SerialFlash.h> //Audio
//LED strip
const int numled = 144;
const int pin = 1;
byte drawingMemory[numled*3]; // 3 bytes per LED
DMAMEM byte displayMemory[numled*12]; // 12 bytes per LED
WS2812Serial leds(numled, displayMemory, drawingMemory, pin, WS2812_GRB);
#define RED 0xFF0000
#define GREEN 0x00FF00
#define BLUE 0x0000FF
#define LBLUE 0x00FFFF
#define ORANGE 0xFF8000
#define BLACK 0x000000
// Taster, DG
#define POWER_PIN 15
#define COLOR_PIN 17
//Audio
#define SDCARD_CS_PIN 4
#define SDCARD_MOSI_PIN 11
#define SDCARD_SCK_PIN 13
// Variable for changing colors
int d = 0;
int LAST_POWER_STATUS=LOW;
// lightsaber status
bool RUNNING = false;
NXPMotionSense imu;
NXPSensorFusion filter;
//Audio
AudioPlaySdWav playSdWav1;
AudioOutputAnalog dac1;
AudioConnection patchCord1(playSdWav1, 0, dac1, 0);
AudioConnection patchCord2(playSdWav1, 1, dac1, 1);
//Motion
int a;
int acc_rms;
//function power up lightsaber
void einschalten(int color, int wait){
playSdWav1.play("TeensySF/out01.wav");
delay(10); // wait for library to parse WAV info
for (int i=0; i < leds.numPixels(); i++) {
leds.setPixel(i, color);
leds.show();
delayMicroseconds(wait);
}
for (int i=0; i < leds.numPixels(); i++) {
leds.setPixel(i, RED);
}
leds.show();
RUNNING = true;
}
//function power off lightsaber
void ausschalten(int color, int wait){
playSdWav1.play("TeensySF/in01.wav");
delay(10); // wait for library to parse WAV info
for (int i=144; i >= 0; i--) {
leds.setPixel(i, color);
leds.show();
delayMicroseconds(wait);
}
RUNNING = false;
int d=0;
}
void setup() {
Serial.begin(9600);
imu.begin();
filter.begin(100);
delay(1000);
pinMode(POWER_PIN, INPUT);
pinMode(COLOR_PIN, INPUT);
pinMode(pin, OUTPUT);
leds.begin();
leds.show();
//Audio
AudioMemory(8);
dac1.analogReference(EXTERNAL); // much louder!
delay(50); // time for DAC voltage stable
pinMode(5, OUTPUT);
digitalWrite(5, HIGH); // turn on the amplifier
delay(10); // allow time to wake up
SPI.setMOSI(SDCARD_MOSI_PIN);
SPI.setSCK(SDCARD_SCK_PIN);
if (!(SD.begin(SDCARD_CS_PIN))) {
while (1) {
Serial.println("Unable to access the SD card");
delay(500);
}
}
delay(1000);
} //Audio ende
void loop() {
int POWER_PIN_STATUS=digitalRead(POWER_PIN); // LOW when pressed / HIGH not
int COLOR_PIN_STATUS=digitalRead(COLOR_PIN);
Serial.println(POWER_PIN_STATUS);
int microsec = 1500 / leds.numPixels();
//Hum
float ax, ay, az;
float gx, gy, gz;
float mx, my, mz;
float roll, pitch, heading;
if (imu.available()) {
// Read the motion sensors
imu.readMotionSensor(ax, ay, az, gx, gy, gz, mx, my, mz);
// Update the SensorFusion filter
filter.update(gx, gy, gz, ax, ay, az, mx, my, mz);
// print the heading, pitch and roll
roll = filter.getRoll();
pitch = filter.getPitch();
heading = filter.getYaw();
Serial.print("Orientation: ");
Serial.print(heading);
Serial.print(" ");
Serial.print(pitch);
Serial.print(" ");
Serial.println(roll);
a=abs(roll/3);
Serial.print(" ");
acc_rms=sqrt(ax*ax+ay*ay+az*az)/3;
Serial.println(acc_rms);
//condition clash activation
if(acc_rms==1 && RUNNING)
{
for (int i=0; i < leds.numPixels(); i++) {
leds.setPixel(i, RED);
}
leds.show();
Serial.println("ShakeShakeShakeShakeShakeShakeShakeShakeShakeShakeShakeShakeShakeShakeShakeShakeShakeShakeShakeShakeShakeShakeShakeShakeShakeShakeShakeShake");
}
// power on and off button
if(POWER_PIN_STATUS != LAST_POWER_STATUS && POWER_PIN_STATUS == LOW){
if(RUNNING){
ausschalten(BLACK, microsec);
}else {
einschalten(RED, microsec);
}
}
LAST_POWER_STATUS = POWER_PIN_STATUS;
// different running colors
if(RUNNING && d==0) {
for (int i=0; i < leds.numPixels(); i++) {
leds.setPixel(i, RED);
}
leds.show();
Serial.println("Just runnin red");
}
if(RUNNING && d==1) {
for (int i=0; i < leds.numPixels(); i++) {
leds.setPixel(i, GREEN);
}
leds.show();
Serial.println("Just runnin green");
}
if(RUNNING && d==2) {
for (int i=0; i < leds.numPixels(); i++) {
leds.setPixel(i, LBLUE);
}
leds.show();
Serial.println("Just runnin light blue");
}
if(RUNNING && d==3) {
for (int i=0; i < leds.numPixels(); i++) {
leds.setPixel(i, BLUE);
}
leds.show();
Serial.println("Just runnin blue");
}
if(RUNNING && d==4) {
for (int i=0; i < leds.numPixels(); i++) {
leds.setPixel(i, ORANGE);
}
leds.show();
Serial.println("Just runnin orange");
}
if ( RUNNING) { //
Serial.println("Start playing");
playSdWav1.play("SmthJedi/hum01.wav");
delay(10); // wait for library to parse WAV info}
}
if(COLOR_PIN_STATUS==LOW && RUNNING)
{d++;
delay(200);}
Serial.println(d);
if (RUNNING == false) d=0;
if (d == 5) d = 0;
}}