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.

Go Up