Hello, im working on a project that requires me to play a st of notes. My current problem is that my loops wont work simultaneously and my speaker has a lot of noise/static.
here my current code and wiring
code logic
- if there is a change in distance activate solenoid,MIDI, and lights
- after a period of time (~2.5s) release solenoid, lights off, notes finished.
///////////////////////// Sensor Vatriables
const int trigPin = 12;
const int echoPin = 11;
long duration;
int distance;
int sensor = 0;
unsigned long sensortime;
unsigned long time;
/////////////////////////////Solenoid Variable
int solenoidPin = 13;
/////////////////////////////Light Variables
#include <FastLED.h>
#define LED_PIN 10
#define NUM_LEDS 60
#define BRIGHTNESS 64
#define LED_TYPE WS2811
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
#define UPDATES_PER_SECOND 100
CRGBPalette16 currentPalette;
TBlendType currentBlending;
extern CRGBPalette16 myRedWhiteBluePalette;
extern const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM;
///////////////////////////////MIDI Variables
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
byte note = 0; //The MIDI note value to be played
byte resetMIDI = 4; //Tied to VS1053 Reset line
byte ledPin = 13; //MIDI traffic inidicator
int instrument = 0;
/////////////////////////////// to run once
void setup() {
/////////////////sensor
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
////////////////solenoid
pinMode(solenoidPin, OUTPUT);
////////////////lights
delay( 300 ); // power-up safety delay
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.setBrightness( BRIGHTNESS );
currentPalette = RainbowColors_p;
currentBlending = LINEARBLEND;
/////////////////music
Serial.begin(57600);
//Setup soft serial for MIDI control
mySerial.begin(31250);
//Reset the VS1053
pinMode(resetMIDI, OUTPUT);
digitalWrite(resetMIDI, LOW);
delay(100);
digitalWrite(resetMIDI, HIGH);
delay(100);
talkMIDI(0xB0, 0x07, 120); //0xB0 is channel message, set channel volume to near max (127)
}
//////////////////////////////to run repeatedly
void loop() {
sensorloop();
if ( sensor == 1) {
lightloop();
musicloop();
solenoidloop();
}
}
void sensorloop() {
time = millis();
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
if (distance > 100) {
sensor = 1;
sensortime = time;
Serial.print(sensor);
digitalWrite(solenoidPin, HIGH);
}
if (time-sensortime > 2300){
digitalWrite(solenoidPin, LOW);
sensor =0;
}
}
void solenoidloop() {
digitalWrite(solenoidPin, HIGH);
if (time-sensortime > 2300){
digitalWrite(solenoidPin, LOW);
}
}
void lightloop() {
ChangePalettePeriodically();
static uint8_t startIndex = 0;
startIndex = startIndex + 1; /* motion speed */
FillLEDsFromPaletteColors( startIndex);
FastLED.show();
FastLED.delay(1000 / UPDATES_PER_SECOND);
}
void musicloop() {
Serial.println("Basic Instruments");
talkMIDI(0xB0, 0, 0x00); //Default bank GM1
//Change to different instrument
for (instrument = 0 ; instrument < 127 ; instrument++) {
Serial.print(" Instrument: ");
Serial.println(instrument, DEC);
talkMIDI(0xC0, instrument, 0); //Set instrument number. 0xC0 is a 1 data byte command
//Play notes from F#-0 (30) to F#-5 (90):
for (note = 30 ; note < 40 ; note++) {
Serial.print("N:");
Serial.println(note, DEC);
//Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
noteOn(0, note, 60);
delay(50);
//Turn off the note with a given off/release velocity
noteOff(0, note, 60);
delay(50);
}
delay(100); //Delay between instruments
}
}
//call on functions midi
//Send a MIDI note-on message. Like pressing a piano key
//channel ranges from 0-15
void noteOn(byte channel, byte note, byte attack_velocity) {
talkMIDI( (0x90 | channel), note, attack_velocity);
}
//Send a MIDI note-off message. Like releasing a piano key
void noteOff(byte channel, byte note, byte release_velocity) {
talkMIDI( (0x80 | channel), note, release_velocity);
}
//Plays a MIDI note. Doesn't check to see that cmd is greater than 127, or that data values are less than 127
void talkMIDI(byte cmd, byte data1, byte data2) {
digitalWrite(ledPin, HIGH);
mySerial.write(cmd);
mySerial.write(data1);
//Some commands only have one data byte. All cmds less than 0xBn have 2 data bytes
//(sort of: MIDI Communication Protocol)
if ( (cmd & 0xF0) <= 0xB0)
mySerial.write(data2);
digitalWrite(ledPin, LOW);
}
void FillLEDsFromPaletteColors( uint8_t colorIndex)
{
uint8_t brightness = 255;
for ( int i = 0; i < NUM_LEDS; i++) {
leds = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
- colorIndex += 3;*
- }*
}
void ChangePalettePeriodically()
{ - uint8_t secondHand = (millis() / 1000) % 60;*
- static uint8_t lastSecond = 99;*
- if ( lastSecond != secondHand) {*
- lastSecond = secondHand;*
- if ( secondHand == 0) {*
- currentPalette = RainbowColors_p;*
- currentBlending = LINEARBLEND;*
- }*
- if ( secondHand == 1) {*
- currentPalette = RainbowStripeColors_p;*
- currentBlending = NOBLEND;*
- }*
- if ( secondHand == 2) {*
- currentPalette = RainbowStripeColors_p;*
- currentBlending = LINEARBLEND;*
- }*
- }*
}
