Power / Code Inquiries with Servo and Infrared Distance Sensor

Yes that is the one! Ill have a look. Thankyou.

[quote="ScruffR, post:20, topic:22294"]
Do you mean the Adafruit Audio FX Soundboard?
[/quote] Yep, with the stereo output and 2MB storage space

Here is my current code for some context.

int Soundfx = D0; // Use the onboard Uno LED
int isObstaclePin = A0;  // This is our input pin
int isObstacle = HIGH;  // HIGH MEANS NO OBSTACLE

void setup() {
  pinMode(Soundfx, OUTPUT);
  pinMode(isObstaclePin, INPUT);
  Serial.begin(9600);
  
}

void loop() {
  isObstacle = digitalRead(isObstaclePin);
  if (isObstacle == HIGH)
  {
    Serial.println("OBSTACLE!!, OBSTACLE!!");
    digitalWrite(Soundfx, HIGH);
  }
  else
  {
    Serial.println("clear");
    digitalWrite(Soundfx, LOW);
  }
  delay(200);
}

I was referring to this which i believe you sent me earlier in the thread!

if distance > min1 and distance < max1
  trigger audio1
else if distance >= max1 and distance < min2
  trigger audio2
else if distance >= min2 and distance < min3
  trigger audio3
else
  turn off all audio
end if

Can you help translate this into real code?

the whole circuit works with the sound pin audio working great to, now i just want to incorporate two more sounds on two more pins and have them all triggered individually by different lengths of measurement like above.

I can't remeber that - and can't find it in this thread either :confused:

BTW: Don't double post one question - I'll pull your other thread over here

So here is my current code.

int Soundfx = D0; // Use the onboard Uno LED
int isObstaclePin = A0;  // This is our input pin
int isObstacle = HIGH;  // HIGH MEANS NO OBSTACLE

void setup() {
  pinMode(Soundfx, OUTPUT);
  pinMode(isObstaclePin, INPUT);
  Serial.begin(9600);
  
}

void loop() {
  isObstacle = digitalRead(isObstaclePin);
  if (isObstacle == LOW)
  {
    Serial.println("OBSTACLE!!, OBSTACLE!!");
    digitalWrite(Soundfx, HIGH);
  }
  else
  {
    Serial.println("clear");
    digitalWrite(Soundfx, LOW);
  }
  delay(200);
}

I turn on the electronics, and the sound plays only if there isnt interaction.
So how do i make it play only when there is an obstacle detected??

@ScruffR !!!

Check your IF/ELSE statements?

I could, but the sensor you have got there is only a digital output (obstacle or no obstacle) but does not provide the distance - as the first sensor you had in mind did.

And about the sound playing or not, you might have to invert the logic on the Soundfx pin - or do you also get the Serial.print() messages the wrong way round?
The sensor is active LOW, so the messages should be fine, but - as said - I don't know if the sound board expects a LOW or a HIGH to play the tune.

if this sensor was able to do distance, how would i go about it?
it triggers from a LOW, i fixed this issue luckily.

just wanting to work out how to add these other two sounds in.

This would be one easy to understand way

  int reading = analogRead(sensor);             // get the ADC reading
  int voltage = 3300 * reading / 4095;          // convert to mV
  int distance = map(voltage, 0, 3300, 4, 80);  // convert full voltage range to 4cm ~ 80cm (linear despite datasheet shows slight curve)

  if      (min1 <= distance && distance < max1) // if value is between min and max
    playAudio(1);
  else if (min2 <= distance && distance < max2)
    playAudio(2);
  else if (min3 <= distance && distance < max3)
    playAudio(2);
  else
    playAudio(-1); // treat -1 as OFF