need help writing Arduino sketch for Nano to Adafruit FX mini soundboard communications?
Hello everyone. I am new here, and fairly new to arduino. Im an electrician/ communications tech by trade, but have only been working with code as a hobby for a few weeks (have wanted to learn this for a long time). I am working on a project that would require an Motion sensor input to play an audio track at random, whilst illuminating a corresponding PWM LED. said LED should fade in sync with the audio file. ITs a robot of sorts that will detect movement and speak a random audio track. Some nice (0-5) and some not so nice (6-12). it will have a a diffused glow when on, blue when speaking nice, and red when speaking not so nice. hence the audio input to act as an anologRead and output to the PWM's I know, a lot to take on for a noob. but go big or go home. I have put together what I thought was a good start sketch, but it doesnt even illuminate an LED, communicate with the soundboard, or serial link on the monitor. I think I am on the right track in regards to the parts and overall Idea. And im pretty sure my sketch needs a lot of work, but i am trying. so here are the parts I had intended to use. (if im going down the wrong path, or you see another way, feel free to say so. I expect some criticism, I can handle it) Arduino elegoo Nano V3.0, CH340/ATmega328p Adafruit Audio FX Mini soundboard w/o amp Adafruit Mono 2.5W Class D amplifier (PAM8302) Sodial mini IR motion sensor module, 3-pin LED's are undecided for finished product, but i was thinking RGB 6302's (5-7) speaker is an 8 ohm 2W power will be supplied by a small 3.7v lipo (thats the plan) My wiring idea should be easily discernible from the sketch, but i can draw my idea, and add it if need be. I did install the soundboard _library. but thats the only one (didnt think i needed others) Any and ALL help is greatly appreciated. the sketch... [code] /* Menu driven control of a sound board over UART. Connect UG to ground to have the sound board boot into UART mode */ #include #include #define SFX_TX 9 // Defines pin D9 as TX to FX Mini #define SFX_RX 10 // Defines pin 10 as RX to FX Mini #define PLAYING 11 // Connect to the ACT pin on the FX Mini #define SFX_RST 13 // Connect to the RST pin on the FX Mini SoftwareSerial ss = SoftwareSerial(SFX_TX, SFX_RX); // Passes Software Serial to FX Mini SoftwareSerial mySerial(SFX_RX, SFX_TX); Adafruit_Soundboard sfx = Adafruit_Soundboard(&ss;, NULL, SFX_RST); // can also try hardware serial with // Adafruit_Soundboard sfx = Adafruit_Soundboard(&Serial1;, NULL, SFX_RST); int incomingAudio; // Audio input assigned from FX Mini Right audio output int RedLED = 5; // the PWM pin the Red LED is attached to int BlueLED = 3; // the PWM pin the Blue LED is attached to int LED = 4; // the output the normal state LED is attached to int Button = 12; // pushbutton test play track pin int Track = random(0,13); int inputPin = 2; // choose the input pin (for PIR sensor) int pirState = LOW; // we start, assuming no motion detected int val = 0; // variable for reading the pin status void setup() { Serial.begin(115200); // softwareserial at 115200 baud Serial.println("Adafruit Sound Board!"); ss.begin(115200); pinMode(LED, OUTPUT); // assigning LED as an output pinMode(RedLED, OUTPUT); // assigning RedLED as an output pinMode(BlueLED, OUTPUT); // assigning BlueLED as an output pinMode(incomingAudio, INPUT); // assigning incomingAudion as an input pinMode(Button, INPUT); // assigning Button as an input pinMode(inputPin, INPUT); // declare sensor as input if (!sfx.reset()) { Serial.println("Not found"); while (1); } Serial.println("SFX board found"); } void loop(){ digitalWrite(LED, HIGH); // assigning LED to start with +5V incomingAudio = analogRead(A0); //read voltage at A0 incomingAudio = (incomingAudio+1)/4 - 1; //scale from 10 bit (0-1023) to 8 bit (0-255) if (incomingAudio<0){ //deal with negative numbers incomingAudio = 0; } PORTD = incomingAudio; Serial.println(PORTD); val = digitalRead(inputPin); // read input value if (val == HIGH) { // check if the input is HIGH sfx.playTrack(Track); Serial.println(Track); if (pirState == LOW) { // we have just turned on Serial.println("Motion detected!"); // We only want to print on the output change, not state pirState = HIGH; } } else { if (pirState == HIGH){ // we have just turned of Serial.println("Motion ended!"); // We only want to print on the output change, not state pirState = LOW; } } if (Button=HIGH){ // pin D12 to button, ground thru 10K res, and +5v sfx.playTrack(Track); Serial.println(Track); } if (incomingAudio>=128 &&Track;>=0 &&Track;<=6){ digitalWrite(RedLED, incomingAudio-128); digitalWrite(LED, LOW); } else if (incomingAudio>=128 &&Track;>=7 &&Track;<=12){ digitalWrite(BlueLED, incomingAudio-128); digitalWrite(LED, LOW); } else{ digitalWrite(LED, HIGH); digitalWrite(RedLED, LOW); digitalWrite(BlueLED, LOW); delay(15000); } } [/code]
Question by supramp