sharp sensor programming

Hi to all!
I need help about my project! :o
I’m trying to create an art project where there are arduino uno r3 + adafruit wave shield + ir sharp sensor 2Y0A21 + leds.

I’ve programmed arduino uno r3 + adafruit wave shield and all work well, but now I would like to insert some fading leds which turn on gradually when the ir sensor recive a presence signal. But I don’t how to do it.
It’s my first creative project and I’m not a good programmer…please help me!!!
thanks in advice :smiley:

I attach my code below…

sketch_JUNE16_SENSORI_CERV.ino (4.01 KB)

void loop() {
   val = analogRead(ir);
  Serial.println(val);                       // print the distance

Does this actually print a value that makes ANY sense?

What was the point of creating an instance of the SharpIR class, if you are not going to use that instance?

Have you not figured out that while playcomplete() is running, NOTHING else happens?

Have you not noticed that playcomplete() simply called playfile() to start the file playing and then twiddles its thumbs waiting for the file to complete playing?

If you want to do ANYTHING else while the music is playing, like fade some LEDs, you will NEVER call playcomplete(). Since you will NEVER call it, you might as well banish that function to the nether regions of the bit bucket.

int ledPin = 11;            // LED connected to digital pin 11
int ir = A0;                 // analog pin for reading the IR sensor
int val = 0;                 // analog pin for reading the IR sensor

#include <SharpIR.h>

#define ir A0
#define model 1080

boolean done=false;

SharpIR sharp(ir, 25, 93, model);

#include "WaveHC.h"
#include <FatReader.h>
#include <SdReader.h>
#include <avr/pgmspace.h>
#include "WaveUtil.h"

WaveHC wave;      // This is the only wave (audio) object, since we will only play one at a time
SdReader card;    // This object holds the information for the card
FatVolume vol;    // This holds the information for the partition on the card
FatReader root;   // This holds the information for the filesystem on the card
FatReader f;      // This holds the information for the file we're play

void sdErrorCheck(void)
{
  if (!card.errorCode()) return;
  putstring("\n\rSD I/O error: ");
  Serial.print(card.errorCode(), HEX);
  putstring(", ");
  Serial.println(card.errorData(), HEX);
  while(1);
}

void setup() {
  Serial.begin(9600);                            // start the serial port
  pinMode (ir, INPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  
  if (!card.init()) {         //play with 8 MHz spi (default faster!)  
  putstring_nl("Card init. failed!");  // Something went wrong, lets print out why
  sdErrorCheck();
  while(1);                            // then 'halt' - do nothing!
  }
  
  card.partialBlockRead(true);
  
  // Now we will look for a FAT partition!
  uint8_t part;
  for (part = 0; part < 5; part++) {     // we have up to 5 slots to look in
    if (vol.init(card, part)) 
      break;                             // we found one, lets bail
  }
  if (part == 5) {                       // if we ended up not finding one  :(
    putstring_nl("No valid FAT partition!");
    sdErrorCheck();      // Something went wrong, lets print out why
    while(1);                            // then 'halt' - do nothing!
  }
  
  // Lets tell the user about what we found
  putstring("Using partition ");
  Serial.print(part, DEC);
  putstring(", type is FAT");
  Serial.println(vol.fatType(),DEC);     // FAT16 or FAT32?
  
  // Try to open the root directory
  if (!root.openRoot(vol)) {
    putstring_nl("Can't open root dir!"); // Something went wrong,
    while(1);                             // then 'halt' - do nothing!
  }
}

void loop() {
   val = analogRead(ir);
  Serial.println(val);                       // print the distance


  if(val > 250){
  playcomplete("BAA1.WAV");
  }
else{
  //do nothing
  }
  delay(500);

val = analogRead(ir);
  Serial.println(val);                       // print the distance

  delay(500);    // it gives you time to open the serial monitor after you upload the sketch

  
 if (done==false){  // it only runs the loop once
  

  unsigned long pepe1=millis();  // takes the time before the loop on the library begins
  
  

  int dis=sharp.distance();  // this returns the distance to the object you're measuring


  Serial.print("Mean distance: ");  // returns it to the serial monitor
  Serial.println(dis);
  
  unsigned long pepe2=millis()-pepe1;  // the following gives you the time taken to get the measurement
  Serial.print("Time taken (ms): ");
  Serial.println(pepe2);  


done=true;
  
}

}


// Plays a full file from beginning to end with no pause.
void playcomplete(char *name) {
  // call our helper to find and play this name
  playfile(name);
  while (wave.isplaying) {
  // do nothing while its playing
  }
  // now its done playing
}
void playfile(char *name) {
  // see if the wave object is currently doing something
  if (wave.isplaying) {// already playing something, so stop it!
    wave.stop(); // stop it
  }
  // look in the root directory and open the file
  if (!f.open(root, name)) {
    putstring("Couldn't open file "); Serial.print(name); return;
  }
  // OK read the file and turn it into a wave object
  if (!wave.create(f)) {
    putstring_nl("Not a valid WAV"); return;
  }
  
  // ok time to play! start playback
  wave.play();
}

Thanks PaulS for your reply.

So, do you tell me to delate the code parte that you reply me in your post?

About the playcomplete() command, I delate it. What command do you suggest me?
I'm sorry but I am new in the Arduino's world!
:frowning:

I'm sorry but I am new in the Arduino's world!

Then, you need to study some examples first. When you can see what a block of code is doing, THEN you can lift it and stuff it into your sketch. If you don't understand something, read the reference material, or ask.

I am almost certain that the SharpIR library came with examples. I am almost certain that the analog reading is NOT the distance.

Hello !

This int ir = A0;                // analog pin for reading the IR sensor
and this #define ir A0
use only one !

int
#define

Check this: map() - Arduino Reference

void setup() {}

void loop()
{
   int val = analogRead(0);
   val = map(val, 0, 1023, 0, 255);
   analogWrite(9, val);
}

Thanks Henins for your help!

I paste your code but nothing change. My led is only turn on.

Before Pauls suggest me do not use playcomplete() command because:

"If you want to do ANYTHING else while the music is playing, like fade some LEDs, you will NEVER call playcomplete(). Since you will NEVER call it, you might as well banish that function to the nether regions of the bit bucket."

But I don't know how to change this...

void setup() {}

void loop()
{
   int val = analogRead(0);
   val = map(val, 0, 1023, 0, 255);
   analogWrite(9, val);
}

You must update this code. I posted it for example!

void loop()
{
   int val = analogRead(0); //  read analog input pin 0  You can change it !
   val = map(val, 0, 1023, 0, 255); // val is analog read values!  (I don't know your output values.) 0, 1023 is output values from potentiometer. 0, 255 is values what you want get out for pin fading ! 
   analogWrite(9, val); // 9 is output pin who can do PWM to fade LED  
}

Tutorial/Fade

Start from bottom with Basics

@heninsh: Why do you keep posting this nonsense? It is NOT how to get data from a Sharp IR sensor.