Arduino Forum

Using Arduino => Audio => Topic started by: ShelledWalnuts on Jun 11, 2018, 04:52 am

Title: Catalex code, integration with analog reading should trigger a sound to play..
Post by: ShelledWalnuts on Jun 11, 2018, 04:52 am
I am new to arduino, I am making an interactive device that will blink when the person touches it and it will make a sound. I derived a sensor and it is connected to the arduino through pin 9, which is based on a voltage divider circuit. The human makes contact and is part of the circuit , thereby lowering the voltage input to Pin 9. This in turn makes the Catalex Mp3 player play a sound until the condition is false (there is a safety margin in the values stored where there is nothing happening to prevent crazy floating action lines 6 and 7). I'm sure there is a better way. But the real issue is my code in line 95. I tell it to send a command to play and it is constantly resending the command which I think is causing some weird sound issues. But all else, it works fine. Can someone help me? Thanks in advance!

Code: [Select]
}

                            // if the analog value is low enough (human contact), blink lights and activiate sound effect:
                            if (analogValue < lowerthreshold) {
                              sendCommand(CMD_PLAY, 0);
                              digitalWrite(ledPin, HIGH);
                              delay (50);
                              digitalWrite (ledPin, LOW);
                              delay (50);
                              }
                                   
                            // if the analog value is high enough (no contact), turn off the blinking lights and sound:
                           else //(analogValue > upperthreshold)
                            {
                              sendCommand(CMD_PAUSE, 0);
                              digitalWrite(ledPin, LOW);}
   

                            // print the analog value:
                           //Serial.println(analogValue); comment out to avoid too much information in serial, uncomment to read value ( calibrate sensors )
                            delay(1);        // delay in between reads for stability
}
Title: Re: Catalex code, integration with analog reading should trigger a sound to play..
Post by: Grumpy_Mike on Jun 11, 2018, 11:16 am
Quote
I tell it to send a command to play and it is constantly resending the command
Yes that is what you are telling it to do.
What you need to do is only tell it to play then the pin first becomes low not when it is low.
Look at the state change code in the IDE's examples to see how to detect a change rather than a logic level.
Title: Re: Catalex code, integration with analog reading should trigger a sound to play..
Post by: ShelledWalnuts on Jun 11, 2018, 12:15 pm
Thanks! I'll look into that.
Title: Re: Catalex code, integration with analog reading should trigger a sound to play..
Post by: ShelledWalnuts on Jun 11, 2018, 11:47 pm
Okay so I looked into state change code for analog inputs and found this...

Code: [Select]

    unsigned int x;
    unsigned int samples = 1000;
    unsigned long int accumulator = 0; // initialize accumulator
    for (x = 0; x < samples; x++) {
        accumulator += analogRead(channel);
    }
    return accumulator / samples;


Was this what you were referring to? I'm afraid I have reached my limit of understanding what to do here.Honestly, I just cant make the transition to apply or integrate this code because I need more brains.   Assistance is greatly appreciated!! :)
Title: Re: Catalex code, integration with analog reading should trigger a sound to play..
Post by: Grumpy_Mike on Jun 12, 2018, 12:35 am
Quote
Was this what you were referring to?
No.

Have you looked at what I said?
Title: Re: Catalex code, integration with analog reading should trigger a sound to play..
Post by: ShelledWalnuts on Jun 12, 2018, 12:46 am
Oh, Sorry but I guess not. In the IDE's examples i cant find " state change code " anywhere. So I looked on forums. Can you provide an example or point me to it?  I just looked again and didnt see anything that says this in all the examples. Thanks again sorry to be a pain
Title: Re: Catalex code, integration with analog reading should trigger a sound to play..
Post by: Grumpy_Mike on Jun 12, 2018, 12:52 am
Only on my iPad so I can't look it up but I think it is under the Files menu
files -> examples -> basics, and the second menu down from that.

Or in fact look here
https://www.arduino.cc/en/Tutorial/StateChangeDetection (https://www.arduino.cc/en/Tutorial/StateChangeDetection)
Title: Re: Catalex code, integration with analog reading should trigger a sound to play..
Post by: ShelledWalnuts on Jun 12, 2018, 01:12 am
Oh I see it now , it is under " digital ", and state change detection. I'll look into that thanks!
Title: Re: Catalex code, integration with analog reading should trigger a sound to play..
Post by: ShelledWalnuts on Jun 12, 2018, 02:07 am
Okay so I looked at the code and honestly, I don't see how to apply it with an analog reading.  :o

Code: [Select]

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;


  // turns on the LED every four button pushes by checking the modulo of the
  // button push counter. the modulo function gives you the remainder of the
  // division of two numbers:
  if (buttonPushCounter % 4 == 0) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }

}
Title: Re: Catalex code, integration with analog reading should trigger a sound to play..
Post by: Grumpy_Mike on Jun 12, 2018, 05:52 am
Quote
Okay so I looked at the code and honestly, I don't see how to apply it with an analog reading.
Your line of code:-
Code: [Select]
if (analogValue < lowerthreshold) {
Turns that analogue reading into a digital one. Once that if is triggered it is just the same as if a button push were detected with a digitalRead.
Title: Re: Catalex code, integration with analog reading should trigger a sound to play..
Post by: ShelledWalnuts on Jun 13, 2018, 05:13 am
Sorry but I do not understand what this means? Though I do still see where my code is getting hung up. Would you mind typing up some code for me? I would really appreciate it! Pulling my hair out here. Thanks in again!
Title: Re: Catalex code, integration with analog reading should trigger a sound to play..
Post by: ShelledWalnuts on Jun 13, 2018, 05:23 am
I found this code and it was posted as a solution for a similar situation. Though, I don't know if this helps or how to even integrate this.  :smiley-confuse:




Code: [Select]
boolean alreadyRun = false;

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  if (alreadyRun == false)
  {
    aFunction();
  }
  Serial.println("Still running loop()");
  delay(1000);
}

void aFunction()
{
  Serial.println("In a function");
  alreadyRun = true;
}
Title: Re: Catalex code, integration with analog reading should trigger a sound to play..
Post by: Grumpy_Mike on Jun 13, 2018, 02:38 pm
You need to do something like this :-
Code: [Select]
if (analogValue < lowerthreshold) {
         pressed = true;
else {
      pressed = false;
}

// then you need to see if it has just become pressed with something like
if( pressed == true && lastPressed == false) { // it has become pressed since last time
// now do the stuff you want to do when the thing is touched.
}

// finally at the end of the loop function put
lastPressed = pressed;


With both pressed and lastPressed being global variables.

However:-
Quote
there is a safety margin in the values stored where there is nothing happening to prevent crazy floating action lines 6 and 7
You can't do this successfully as there is no way to distinguish between a reading got from a floating input and one got from a touch or release.

Also, the level of help you are needing shows that you are not yet ready to tackle this sort of project. You need to do a lot more examples that are contained in the IDE, and understand them, to get your code skills up to speed.
Title: Re: Catalex code, integration with analog reading should trigger a sound to play..
Post by: ShelledWalnuts on Jun 14, 2018, 03:01 am
Thanks for your help.