Go Down

Topic: Catalex code, integration with analog reading should trigger a sound to play..  (Read 381 times) previous topic - next topic

ShelledWalnuts

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
}

Grumpy_Mike

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.


ShelledWalnuts

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!! :)

Grumpy_Mike

Quote
Was this what you were referring to?
No.

Have you looked at what I said?

ShelledWalnuts

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

Grumpy_Mike

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

ShelledWalnuts

Oh I see it now , it is under " digital ", and state change detection. I'll look into that thanks!

ShelledWalnuts

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);
  }

}

Grumpy_Mike

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.

ShelledWalnuts

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!

ShelledWalnuts

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;
}

Grumpy_Mike

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.


Go Up