New to Arduino Coding..HELP

I have recently recieved an Arduino Uno, Sharp Infrared Proximity Sensor, and LEDs.

I am a beginner and I would like just some simple code to allow the LED, (and eventually a motor to turn on) to light when my hand is over the sensor.

If anyone could give the code, it would be much help?

Thank you,
Shawn

I have tried Google searches to NO avail.

If you have found a website that could show me the code. It would be great if you could direct me to it.

Thank you

http://luckylarry.co.uk/arduino-projects/arduino-using-a-sharp-ir-sensor-for-distance-calculation/

among top 5 when I asked google for arduino sharp ir :]

Thank you

It prints values, but the LED light does not go off.

The Arduino Psychic Hotline has closed for the week. If you’d like to leave a message, think positive thoughts, and we’ll get back to you as soon as possible.

On the other hand, if you’d post your code, help could come even sooner.

Why is everyone so angry?

If you do not want to help me then don't.

If I had code I would post it. I simply copied what the website another person gave me which just prints distance values using the infrared proximity sensor.

int IRpin = 1; // analog pin for reading the IR sensor void setup() { Serial.begin(9600); // start the serial port } void loop() { float volts = analogRead(IRpin)*0.0048828125; // value from sensor * (5/1024) - if running 3.3.volts then change 5 to 3.3 float distance = 65*pow(volts, -1.10); // worked out from graph 65 = theretical distance / (1/Volts)S - luckylarry.co.uk Serial.println(distance); // print the distance delay(100); // arbitary wait time. }

But instead of printing values to my computer, I want it to light an LED light.

If you had not notice, I am a true beginner, not someone lazy and not wanting to learn in a class or not attempt homework. This is a small portion of a large project.

Thank you, Shawn

We're not angry! :)

But as a beginner, you might want to do some self-teaching before embarking on a project that is evidently too much for you right now.

This is a good place to start: http://arduino.cc/en/Tutorial/HomePage

Since you already have a measure for distance I would suggest you have a look at http://arduino.cc/en/Reference/If from the good resource: http://arduino.cc/en/Reference/HomePage

"If anyone could give the code, it would be much help?" If you try to write the code yourself, you might be of help for someone else later on :) Just a thought 8-)

Good luck with your project!

You said

It prints values, but the LED light does not go off.

. This implies that you have some code that is supposed to turn the LED on or off. It also implies that that code is not working correctly. Further, it implies that you would like help understanding why it does not.

And yet you want us to guess what code you have, and why it does not work. No can do.

You are printing the distance. This means that you know some values. Under what circumstances do you want the LED to come on? Under what circumstances do you want the LED to go off.

If you want to turn an LED on, use digitalWrite(), specify a pin number as the 1st argument, and use HIGH as the 2nd argument. If you want it to go off, specify LOW as the 2nd argument. Blinking LEDs is usually the 1st thing that people learn how to do, so it is a not unreasonable assumption that you know how to do this.

You can use an if statement to conditionally turn the LED on or off:

if(distance > 4 && distance < 39)
{
   // Turn the LED on
}
else
{
   // Turn the LED off
}

Spending a few minutes reading stuff on this site http://arduino.cc/en/Reference/HomePage will prove useful.

This is a small portion of a large project.

Then you definitely need to learn the basics before you try anything larger.

Actually, that's some nicely commented code you pasted.. and since it's short, I'd recommend you look it up, line by line.. you'll get a basic grasp of how easy Arduino can be to program.

The program is in two sections.. setup() , and loop(). The things in the setup section are run once at startup of your program, and the stuff contained in loop().. meaning the stuff between the curly braces below, gets looped through over and over as long as the Arduino has power.

void loop() { do something do something do something };

Now, given that information, take a few breaths and walk through the program, even just reading the comments.. it's very well commented code, so you should be able to see where your changes should probably go pretty quickly.

Since you know what you want the output to be, take a glance at the Output section of the reference... you'll find what you need there.

Any more information and we'd be doing it for ya ;) ... but if you try and actually get stuck, we're around :)

Why is everyone so angry?

If you do not want to help me then don't.

Please review this:

http://www.catb.org/esr/faqs/smart-questions.html

Thank you.

8-)

When I was learning I did the tutorials here: http://www.ladyada.net/learn/arduino/lesson2.html It will teach you about if statements and more.

Thank you guys for stirring me in the right direction.

A little tough love is what I needed to actually go used the tools you guys gave me and doing a little research of my own.

It turn out to not be as hard as I thought.

I have taking several tutorials and codes and developed the code that I needed.

Here is the finished product:

int IRpin = 1; // analog pin for reading the IR sensor
void setup() {
Serial.begin(9600); // start the serial port
pinMode(13, OUTPUT); // Pin 13 has an LED connected on most Arduino boards: initialize the digital pin as an output.
}
void loop() {
float volts = analogRead(IRpin)0.0048828125; // value from sensor * (5/1024) - if running 3.3.volts then change 5 to 3.3
float distance = 65
pow(volts, -1.10); // worked out from graph 65 = theretical distance / (1/Volts)S - luckylarry.co.uk
Serial.println(distance); // print the distance
delay(100); // arbitary wait time.
if(distance < 60)
{

digitalWrite(13, HIGH); // set the LED on// Turn the LED on
}
else
{
digitalWrite(13, LOW);// Turn the LED off
}

}

Thanks again…Works great

Good job, coolshawn!

PS: use the [#] next time you post code, it is much easier to read then :]

Awesome. Don't stop now.. the fun is just getting started.

See if you can figure out how you would do the following, by modifying your code:

Instead of hard-coding your threshold of 60.. how might you go about making it adjustable by turning a knob?

Exact statements not important.. analyzing and thinking through the problem, that's what needed. Arduino is a great learning tool, because it encourages you to try and figure out "what if I..." for all those little ideas that pop into your head.