Go Down

Topic: Audio fx trigger (Read 2263 times) previous topic - next topic

Sarah1234

Hi I need to find a way to control an adafruit audio fx sound board I have set up as per their video, it works fine when I ground the switch or Send commands via serial but I need to set up a Nano or Uno to replace the ground switch trigger  method , does any one know a way to do it?

marine_hm

I have mine connected to a wemos D1 mini, the other d1 mini connected to capacitive touch switch.
The 2 wemos was much cheaper than a remote button and an rf module and does not require a micro controller.

sound fx sketch:
Code: [Select]

#include <ESP8266WiFi.h>

const char* ssid = "SoundFx";
const char* password = "Adafruit";

int ledPin = D1;
int triggerPin = D7;
WiFiServer server(80);

void setup() {
  Serial.begin(115200);
  delay(10);

  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);


  pinMode(triggerPin, OUTPUT);
  digitalWrite(triggerPin, HIGH);

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

  WiFi.begin(ssid, password);
  WiFi.mode(WIFI_STA);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    digitalWrite(ledPin, HIGH);
    Serial.print(".");
    delay(50);
    digitalWrite(ledPin, LOW);
  }
  Serial.println("");
  Serial.println("WiFi connected");

  // Start the server
  server.begin();
  Serial.print("Server started on ");
  Serial.println(WiFi.localIP());

  digitalWrite(ledPin, HIGH);
  delay(500);
  digitalWrite(ledPin, LOW);

}

void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (client) {

    // Wait until the client sends some data
    Serial.println("new client");
    while (!client.available()) {
      delay(1);
    }

    // Read the first line of the request
    String request = client.readStringUntil('\r');
    Serial.println(request);
    client.flush();

    // Match the request

    int value = LOW;
    if (request.indexOf("/LED=ON") != -1) {
      digitalWrite(ledPin, HIGH);
      digitalWrite(triggerPin, LOW);
      value = HIGH;
    }
    if (request.indexOf("/LED=OFF") != -1) {
      digitalWrite(ledPin, LOW);
      digitalWrite(triggerPin, HIGH);
      value = LOW;
    }

    // Set ledPin according to the request
    //digitalWrite(ledPin, value);

    // Return the response
    client.println("HTTP/1.1 200 OK");
    client.println("Content-Type: text/html");
    client.println(""); // do not forget this one

    client.print("Led pin is now: ");

    if (value == HIGH) {
      client.print("On");
    } else {
      client.print("Off");
    }
    client.println(""); // do not forget this one
    client.flush();
    client.stop();
    Serial.println("Client disonnected");
    Serial.println("");
  }

}



The remote (WiFi) button sketch:
Code: [Select]

#include <ESP8266WiFi.h>

extern "C" {
#include "user_interface.h"
}

char apName[] = "SoundFx";
char apPassword[] = "Adafruit";

void setup() {
  Serial.begin(115200);
  delay(100);

  WiFi.softAP(apName, apPassword);
  WiFi.mode(WIFI_AP);

  pinMode(D4, INPUT);// not sure what to put here
  Serial.println();
  Serial.println("AP Started");
}

int value = 0;
int stations = -1;

void loop() {

  int stationsNow = wifi_softap_get_station_num();
  if (stations != stationsNow) {
    stations = stationsNow;
    Serial.print("Station count: ");
    Serial.println(stations);
  }

  int newValue = digitalRead(D4);

  if (newValue != value) {
    value = newValue;
    Serial.print("value=");
    Serial.println(value);

    struct station_info *stat_info;
    struct ip_addr *IPaddress;
    unsigned long uintaddress;

    Serial.println("Updating connected stations...");

    stat_info = wifi_softap_get_station_info();
    while (stat_info != NULL) {

      IPaddress = &stat_info->ip;
      uintaddress = IPaddress->addr;
      String host = String(ip4_addr1(&uintaddress)) + "." + ip4_addr2(&uintaddress) + "." + ip4_addr3(&uintaddress) + "." + ip4_addr4(&uintaddress);

      Serial.print("connecting to ");
      Serial.println(host);

      // Use WiFiClient class to create TCP connections
      WiFiClient client;
      const int httpPort = 80;
      if (!client.connect(host.c_str(), httpPort)) {
        Serial.println("connection failed");
      }
      else {

        // We now create a URI for the request
        String url;
        if (value == HIGH) {
          url = "/LED=ON";
        }
        else {
          url = "/LED=OFF";
        }
        Serial.print("Requesting URL: ");
        Serial.println(url);

        // This will send the request to the server
        client.print(String("GET ") + url + " HTTP/1.1\r\n" +
                     "Host: " + host + "\r\n" +
                     "Connection: close\r\n\r\n");
        delay(50);

        // Read all the lines of the reply from server and print them to Serial
        while (client.available()) {
          String line = client.readStringUntil('\r');
          Serial.print(line);
        }

        Serial.println();
        Serial.println("closing connection");
      }
      client.flush();
      client.stop();
      stat_info = STAILQ_NEXT(stat_info, next);
    }
    Serial.println("All connected stations updated.");
  }
}

Sarah1234

Thank you for your response, you clearly know a lot more than I will ever understand I will try and work my way through your sketch and see if I can adapt it to my requirements, can you simplify the sketch so that you can trigger the adf from the Uno.

Grumpy_Mike

Quote
but I need to set up a Nano or Uno to replace the ground switch trigger  method , does any one know a way to do it?
Just connect an output from the board you want to act as a trigger along with the ground. Write a zero to that output and you trigger the sound.

Sarah1234

Thanks grump that's what I was looking for, what would I write for the sketch, ?

Grumpy_Mike

Quote
what would I write for the sketch,
It depends on what event you want to trigger the sound with.
Say it is the light level. Connect a photo resistor between an analogue input and ground. Add a pull up resistor and read the value from the Anologue pin. When it exceeds ( or drops below ) a certain level do a digital write LOW on your trigger pin output.

marine_hm

#6
Nov 19, 2016, 01:36 pm Last Edit: Nov 19, 2016, 01:44 pm by marine_hm
Sarah:

Grumpy_Mike explained it well, I waited for your response...  How you want to trigger it would help.  Push button, motion/vibration detector, light sensor, water level?  Possibilities are by your imagination.

Look at the blink sketch as an example.  Your turning an led on, thus making the value HIGH(normal OFF state for the sound  board).  Then turn the led off by making the value LOW(this would be your trigger), wait a few miliseconds, then turn it back on.

Sarah1234

Wow thanks for the help, what I want to do is when my stepper motor starts up it plays a sound, then after the motor reaches its stop it waits 4 seconds then returns and plays the sound again. Hope that explains it a bit better

Grumpy_Mike

Quote
Hope that explains it a bit better
Well that adds a whole new level of information you never told us about.
So :- what stepping motor? What causes it to start? What causes it to stop?
Can you post what you have written already please.

marine_hm

#9
Nov 20, 2016, 02:25 am Last Edit: Nov 20, 2016, 02:30 am by marine_hm
I have not played with a stepper motor just yet.  but here is a servo sketch that may get you close to what you're looking for.  Just copy and paste lines 36-38 where you want sound played.  maybe like line 57???

Not sure how to initiate pinmode 3 state as HIGH


Code: [Select]

#include <Button.h>
int BUTTON = 2;           //  Variable to store value for HIGH/LOW of button press
int pos = 0;              //  Variable to store the servo position

// Creating speed settings for the servos sort of like PWM
int servoDelay1 = 25;     //  Creates servo delay speed=25 in miliseconds
int servoDelay2 = 50;     //  Creates servo delay speed=50 in miliseconds
int servoDelay3 = 150;    //  Creates servo delay speed=150 in miliseconds

#include <Adafruit_SoftServo.h>
#define SERVOPIN 0        //  Creates servo object used to pour the fluid
#define SERVOPIN 1        //  Creates servo object used to tilt the container
Adafruit_SoftServo OneServo, TwoServo;// Naming servos instead of servo1, servo2

void setup() {

  pinMode(2, INPUT_PULLUP);//******************************************
  pinMode(3, OUTPUT);

  OneServo.attach(0);   // attaches the servo on pin 0 to pour the fluid
  TwoServo.attach(1);  // attaches the servo on pin 1 to tilt the container

  TwoServo.write(90);   // THESE TWO LINES SET THE SERVO POSITIONS
  OneServo.write(25);    // AS SOON AS THE ARDUINO IS POWERED UP
}   // END OF SETUP


void loop() {

  // ************ WAIT FOR BUTTON TO BE PRESSED **********


  if (digitalRead(2) == LOW) {  // If button pressed, RUN LOOP CYCLE


    digitalWrite(3, LOW);   // trigger pin for soundboard
    delay(500);              // wait for half a second
    digitalWrite(3, HIGH);    // reset trigger pin to high, should finish playing recorded sound.






    for (pos = 90; pos >= 60; pos -= 1) {
      TwoServo.write(pos);   // Tilt servo at 60 deg. to recieve fluid(-30)
      delay(servoDelay2);
    }


    // POUR fluid FROM BOTTLE
    for (pos = 25; pos <= 125; pos += 1) { // Tilts fluid bottle to 125 degrees
      OneServo.write(pos);   // Tilt container at 60 deg. to recieve fluid(-30)
      delay(servoDelay3);
    }
    delay(3000);       //  WAIT 3 SECONDS

    // RETURN BOTTLE TO ORIGINAL POSISTION
    for (pos = 125; pos >= 25; pos -= 1) { // Tilts fluid bottle to 125 degrees
      OneServo.write(pos);   // Tilt container at 60 deg. to recieve fluid(-30)
      delay(servoDelay1);
    }


    // TILT container BACK UP
    for (pos = 60; pos <= 90; pos += 1) {
      TwoServo.write(pos);   // Tilt container at 60 deg. to recieve fluid(-30)
      delay(servoDelay3);
    }
  }
}// Back to top of loop

Grumpy_Mike

Not sure how to initiate pinmode 3 state as HIGH
What?
You can't initialise pins as tristate ( if that is what you mean by 3 state ) on an Arduino.
Even if you could then a tristate high is meaningless.

The closest you can get to a tristate on an Arduino is an input. Even then I don't see how this is going to help.

marine_hm

Sorry Mike.  That is not what I meant at all.  But I have to admit, that's what it looks like I said.
What I mean is.  How to set pin 3; HIGH at startup :smiley-confuse:

Here:  insert this on line 19
  digitalWrite(3, HIGH);

That worked....

Grumpy_Mike

Ok fine, remember what goes up must at some time come down.

Sarah1234

Thanks Mike, sorry I didn't fully explain how it works , basically a switch starts the motor it runs to its prescribed step waits 4 secs then returns back to its start position. Will try and insert my code , but I'm not sure how to do that !its a girly thing.

Grumpy_Mike

So post your code, using code tags, and we can tell you how to add it.

Go Up