Go Down

Topic: Newbie using a Ping sensor to control a fade LED function (Read 1 time) previous topic - next topic

elbiondo

Hi all,

I'm really new to Arduino code and am having trouble with my current project.

I want to use a Parallax Ping sensor to control how bright or dim an LED becomes. The closer that a person approaches, the brighter the light. The farther away, the dimmer the light.

I found a simplified Ping code on the Arduino site, but don't know how to modify it for my project. Any suggestions?

Here is the code I have so far:

//working Ping project

unsigned long echo = 0;
int ultraSoundSignal = 7; // Ultrasound signal pin
unsigned long ultrasoundValue = 0;

void setup()
{
Serial.begin(9600);
pinMode(ultraSoundSignal,OUTPUT);
pinMode(11, OUTPUT);       //sets up LED as output
}

unsigned long ping(){
pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output
digitalWrite(ultraSoundSignal, LOW); // Send low pulse
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
delayMicroseconds(5); // Wait for 5 microseconds
digitalWrite(ultraSoundSignal, LOW); // Holdoff
pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input
digitalWrite(ultraSoundSignal, HIGH); // Turn on pullup resistor
  echo = pulseIn(ultraSoundSignal, HIGH); //Listen for echo
  ultrasoundValue = (echo / 58.138) * .39; //convert to CM then to inches
return ultrasoundValue;
}

void loop()
{
int x = 0;
x = ping();
  Serial.println(x);
  delay(250); //delay 1/4 seconds.

if (ping() > 84)
  {
    for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=1)
  {
   
    //sets the value (range from 0 to 255);
    analogWrite(11, fadeValue);
    //wait for 30 milliseconds to see the dimming effect
    delay(30);
  }
   
    }
  else
  {
     for(int fadeValue = 255 ; fadeValue>= 0; fadeValue -=1)
  {
    //sets the value (range from 0 to 255):
    analogWrite(11, fadeValue);
    //wait for 30 milliseconds to see dimming effect
    delay(30);
 
}
}
}

Groove

#1
Mar 01, 2011, 09:09 pm Last Edit: Mar 01, 2011, 09:17 pm by Groove Reason: 1
Code: [Select]
int x = 0;
x = ping();

What is the return type of "ping"?
But then again, you don't do anything with it, so I guess it is OK.

Not normally a good idea to return a global value.
It will work, but as your software grows, it may cause you problems
Per Arduino ad Astra

elbiondo

Thanks for the reply, Groove.

I recently got some help in the code dept from a professor, and our collaboration yielded this.

Code: [Select]
int ledpin = 11;



int val;

const int pingPin = 7;

void setup() {
 
// initialize serial communication:
  Serial.begin(9600);
  delay(1000);

  pinMode(ledpin, OUTPUT);

}

void loop() {

   long duration, inches, cm;

   pinMode(pingPin, OUTPUT);
   digitalWrite(pingPin, LOW);
   delayMicroseconds(2);
   digitalWrite(pingPin, HIGH);
   delayMicroseconds(5);
   digitalWrite(pingPin, LOW);
   pinMode(pingPin, INPUT);
   duration = pulseIn(pingPin, HIGH);
   
  // convert the time into a distance
   cm = microsecondsToCentimeters(duration);

  // alberto's debugging block
Serial.print("duration = ");
Serial.print(duration);
Serial.print("\t");
Serial.print("cm = ");
Serial.println(cm);
Serial.println("----------");

if (cm > 200)
{
   analogWrite(ledpin, 0);
}
else{

  val = cm;               // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 200, 10, 0, 255);        // scale it to use it with the LED (value between 0 and 255)

 
  analogWrite(ledpin, val);
  delay(10);

}

if (cm > 61)
{
  analogWrite(ledpin, HIGH);
}
 





}

// The following functions won't compile if not used in the code so no need to comment any out.
// Your code left out the closing curly bracket, not sure how it cmpiled without it but maybe
// it was only in the version you emailed me.
long microsecondsToInches(long microseconds) {
return microseconds / 74 / 2;
}


long microsecondsToCentimeters(long microseconds) {
return microseconds / 29 / 2;
}


The code works, which is great, but now there's another problem...The LED is not functioning correctly. It lights up when it is supposed to but has this annoying habit of flickering, especially when someone moves in front of the sensor. My professor said i should get an external power supply for it (it's an ultra-bright 1 Watt LED)...

...any ideas?

Groove

Quote
My professor said i should get an external power supply for it (it's an ultra-bright 1 Watt LED)...

What are you using to power the LED?
Per Arduino ad Astra

elbiondo

I'm using the Arduino itself (the 5V pin) to power the LED. I have the LED on a breadboard right now with a 220 resistor.

Groove

Per Arduino ad Astra

elbiondo

Not particularly, no. It also flickers a lot when the sensor reads past 61 cm (I set the LED to stay at 255 when the sensor reads closer than 61 cm).

By the end of the project, I need around 12 of those LEDs to be lit in order to get the obnoxious amount of brightness required. Thus, my problem with needing a power source will be a lot larger by the end.

AWOL

If it is a 1 watt LED, why are you driving it from the Arduino?
Don't you want it to be as bright as possible?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

elbiondo

To be honest, it's because I'm really new to this and wasn't aware that the LED would need any external power.

Thanks again for your input, Groove and AWOL! I take it from your responses that I SHOULD use external power. This being said, since I need to hook up 12 of these 1 Watt LEDs, I believe I need a 12A power source. Are there other electrical implementations I should use with this--i.e. a transistor, a voltage regulator, etc.? Again, I'm really new to this, so I apologize if my questions are really elementary.

Groove

I'd probably suggest a commercial constant-current driver.
Per Arduino ad Astra

elbiondo

Thank you, Groove! I had never heard of those (again, really new). I'll check that out.

I talked to my professor again, and he suggested to only use 3 of these LEDs, then to create a circuit with a 5V 3A power supply, an LM317 to lower the voltage, and a transistor.

Do you think that running 5V/3.0A to an Arduino would be ok? Is there anything else I should consider?

elbiondo

Also, I'm unsure if the transistor or the power regulator need anything else on the circuit in order to function properly, i.e. resistor, diode, etc.

Forgive my Newbie questions, but is there anything else I should put on my circuit boards?

Go Up