Offline
Newbie
Karma: 0
Posts: 13
|
 |
« on: March 01, 2011, 12:32:32 pm » |
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); } } }
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Faraday Member
Karma: 15
Posts: 2852
Gorm deficient
|
 |
« Reply #1 on: March 01, 2011, 03:09:37 pm » |
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
|
|
|
|
« Last Edit: March 01, 2011, 03:17:20 pm by Groove »
|
Logged
|
Per Arduino ad Astra
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 13
|
 |
« Reply #2 on: March 03, 2011, 12:55:09 pm » |
Thanks for the reply, Groove. I recently got some help in the code dept from a professor, and our collaboration yielded this. 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?
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Faraday Member
Karma: 15
Posts: 2852
Gorm deficient
|
 |
« Reply #3 on: March 03, 2011, 03:17:08 pm » |
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?
|
|
|
|
|
Logged
|
Per Arduino ad Astra
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 13
|
 |
« Reply #4 on: March 03, 2011, 04:53:48 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Faraday Member
Karma: 15
Posts: 2852
Gorm deficient
|
 |
« Reply #5 on: March 03, 2011, 05:03:37 pm » |
So it isn't all that ultra-bright?
|
|
|
|
|
Logged
|
Per Arduino ad Astra
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 13
|
 |
« Reply #6 on: March 03, 2011, 06:29:41 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19007
I don't think you connected the grounds, Dave.
|
 |
« Reply #7 on: March 04, 2011, 02:30:27 am » |
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?
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 13
|
 |
« Reply #8 on: March 04, 2011, 02:55:25 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Faraday Member
Karma: 15
Posts: 2852
Gorm deficient
|
 |
« Reply #9 on: March 04, 2011, 03:52:08 pm » |
I'd probably suggest a commercial constant-current driver.
|
|
|
|
|
Logged
|
Per Arduino ad Astra
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 13
|
 |
« Reply #10 on: March 04, 2011, 05:44:26 pm » |
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?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 13
|
 |
« Reply #11 on: March 13, 2011, 11:59:04 am » |
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?
|
|
|
|
|
Logged
|
|
|
|
|
|