led and ping)))

Hi there,

First of, i'm new around here. So i will introduce myself. I'm Michael and i'm from the netherlands. And currently i am working on a project that will let me end my carreer as a student.

For this project i want to make some different things using Arduino's. So i orderd 6 and some sensors and the "Getting started with Arduino" book from Make magazine.

I have read the whole book and i worked out the little tutorials that are talked about. So thats going quite well :-).

Now i want to start of with my own project and like all the other noobs there are some questions involved.

Im starting with the Ping sensor. Looks cool and works like a charm, i got all the output on my screen. So of course that was a milestone for me, except for the fact that i did not code a bit...

Anyway i hooked up 6 leds and the ping sensor and i want to read the ping data and light up more leds when you get closer.

Sounds easy, and i'm working on it. But electronics frighten me, especially when its hooked up to my macbook pro... I stripped down an old pc that i had and removed all the leds from it.

Currently im running the blink sketch to see if all my leds work, and they do they blink one after another. But the leds that i removed from the computer are not as bright as the ones that i got with the starter kit.

How is that possible? I have the 10 kohm resistors between them, is that the issue? Can i just hook those leds up without the resistors? And how about the ping sensor, do i need to use a resistor? When i did the online lamp sketch with the light sensor i had to use one, but i dont really know why, that was not told in the book.

Sorry for the long story, just wanted to do the whole, AA meeting, introduction :-)

Thanks in advance for the reply :-)

I have the 10 kohm resistors between them, is that the issue

Yes, that's an issue - depending on the colour of LED, they should be in the hundreds of ohms range. No, you can't wire them without resistors.

Ok, but 10 kohm is equal to 100 ohm right?

And even if the resistors are to weak, then the led would be very bright? Please correct me if im wrong though.

Is there a way to find out what resistor i have to use? A volt meter maybe?

[edit]

The leds are red and green

Ok, but 10 kohm is equal to 100 ohm right?

No, 10 k ohm is 10 000 ohms (ten thousand ohms) - one hundred times the resistance of a 100 ohm resistor.

And even if the resistors are to weak, then the led would be very bright?

Not only that, but the excess current could damage your processor.

Assume a current of 20mA, then the formula to determine the resistance is R = (Vsupply - Vfwd) /20mA.

So 5V supply, say a Vf of 2V for the LED, then (5 - 2) / 0.020 = 150 ohms.

Allright! That looks alot better now :)

Thanks for the math, the fact that i suck at math is going to kill me on this project ;-)

What about the Ping sensor, do i need to resistor that puppy to?

So i followed the BarGraph sketch because thats what i want to do with the leds. But it was a simple thought to replace the pot with the ping sensor, so that doesnt work.

I can understand why, the pins sensor is way more complicated then the pot.

So im kinda stuck on some questions.

  1. Can i connect the ping sensor to a analog pin on the Arduino?
  2. If so, how do i write code for this? Are there any examples on this, some piece of code that shows me how to use the ping sensor without the feedback to the monitor in the arduino software?

  3. If i have to connect it to the digital pins, how do i get the data to do its magic on the leds without the use of a computer?

Thanks again for the help :)

Have you looked at the http://www.arduino.cc/en/Tutorial/Ping page?

Well yes, but i dont realy get it...

I cant grasp the logic behind the sending recieving stuff and doing this without sending the output to a computer. Its kinda frustrating and i saw this exact line in another thread, i understand the code but i cant seem to type it out.

I made this to make it a bit more clear (i hope)

(I am using the 3 pins ping sensor not another one, i could find it...)

sending recieving stuff

All the " sending recieving stuff " is doing is transforming the data from one representation (the value of the range) to another (ASCII characters in a string).

All you have to do is transform (map, if you like) the data from the range information to a number in the range 1 to 5, or better still, 0 to 4, and then use that value to pick one LED from five and light it.

Well in theory i know exactly what has to be done… Now the programming. I got stuck on:

/*
 Based on the LED bar graph sketch and the Ping sketch
*/
 


// these constants won't change:
const int digitalPin = 12;    // the pin that the potentiometer is attached to
const int ledCount = 6;    // the number of LEDs in the bar graph

int ledPins[] = { 
  2, 3, 4, 5, 6, 7 };   // an array of pin numbers to which LEDs are attached


void setup() {
  // loop over the pin array and set them all to output:
  for (int thisLed = 0; thisLed < ledCount; thisLed++) {
    pinMode(ledPins[thisLed], OUTPUT); 
  }
}

void loop() {
  // read the Ping sensor:
  int sensorReading = digitalRead(digitalPin);
  
  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // map the result to a range from 0 to the number of LEDs:
  int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);

  // loop over the LED array:
  for (int thisLed = 0; thisLed < ledCount; thisLed++) {
    // if the array element's index is less than ledLevel,
    // turn the pin for this element on:
    if (thisLed < ledLevel) {
      digitalWrite(ledPins[thisLed], HIGH);
    } 
    // turn off all pins higher than the ledLevel:
    else {
      digitalWrite(ledPins[thisLed], LOW); 
    }
  }
}

And i get a error message, of course…

 In function 'void loop()':
error: 'pingPin' was not declared in this scope

Not surprisingly, that's because you haven't defined anything called "pingPin".

Ok you are right, i was so tiered yesterday that i ignored the error message…

So now i am fresh and i fixed the error message, still my sketch doesnt seem to talk with the leds and the sensor. So what is the next step that i have to take?

/*
 Based on the LED bar graph sketch and the Ping sketch
*/
 


// these constants won't change:
const int pingPin = 12;    // the pin that the potentiometer is attached to
const int ledCount = 6;    // the number of LEDs in the bar graph

int ledPins[] = { 
  2, 3, 4, 5, 6, 7 };   // an array of pin numbers to which LEDs are attached


void setup() {
  // loop over the pin array and set them all to output:
  for (int thisLed = 0; thisLed < ledCount; thisLed++) {
    pinMode(ledPins[thisLed], OUTPUT); 
  }
}

void loop() {
  // read the Ping sensor:
  int sensorReading = digitalRead(pingPin);

  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  long duration;
  
  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // map the result to a range from 0 to the number of LEDs:
  int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);

  // loop over the LED array:
  for (int thisLed = 0; thisLed < ledCount; thisLed++) {
    // if the array element's index is less than ledLevel,
    // turn the pin for this element on:
    if (thisLed < ledLevel) {
      digitalWrite(ledPins[thisLed], HIGH);
    } 
    // turn off all pins higher than the ledLevel:
    else {
      digitalWrite(ledPins[thisLed], LOW); 
    }
  }
}

I'd put some serial debug prints in and see what values you're getting back.

Euhm.. Where and what should i debug? I pasted a

{
return sensorReading;
}

But i got errors back while compiling

"return" from what?

duration = pulseIn(pingPin, HIGH);

  // map the result to a range from 0 to the number of LEDs:
  int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);

Where does "sensorReading" get assigned a value? What do you do with the value of "duration"?

Well a few lines higher.

Thats why i thought it would be usefull if the sensorReading value was used.

  int sensorReading = digitalRead(pingPin);

You're doing a digitalRead (which returns only a one or a zero) and then mapping that to a much larger range.

What are you doing to the value of "duration"?

That is the thing i am stuck on, i dont know what to do with that value.

I have no clue of the output of the sensor and how i should modify the data to the leds. :-/

"duration" is the value you should be mapping.

A lot of debugging is about questioning yourself: the processor will do pretty much anything you tell it, assuming you can compile it. If it isn't doing what you want it to, it's probably because you told it to do the wrong thing. You have to ask yourself "Why?".

Cool! The leds are HIGH!

And now the leds are on, but they are freaking me out haha. So now i have to filter out alot of noise, right?

So if i am correct, the "long" is giving me a high data string that it recieves from the sensor. Well i am really unsure about this...

What should i do to reduce the noise?