Go Down

Topic: Controlling LEDs with Ultrasonic range finder (Read 1 time) previous topic - next topic

nkitch

I am a student of product design and am completely new to arduino.

What I need is to be able to control LEDs based on the distance of a viewer from a project.
I am using an arduino uno smd with a matbotix LV, a 7 segment display, and white LEDs.
I need the final project to display the distance of the person from the board in inches and for the leds to dim as they approach i.e. as the distance decreases, the LED brightness decreases.

Please Help!

Thank you.

AWOL

You need to show us what you've done before we can advise why it isn't working.
"Pete, it's a fool (who) 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.
I speak for myself, not Arduino.

nkitch

#2
May 20, 2012, 08:19 pm Last Edit: May 20, 2012, 08:24 pm by AWOL Reason: 1
So what I have right now is just a digital output of the distance in inches on the 7 segment display, I have no idea how to incorporate the LEDs.

Code: [Select]
#include <SoftwareSerial.h>

#define SensorPin 0
#define TXPin 3
#define RXPin 2
#define AVG_COUNT 10

unsigned int inches[AVG_COUNT];
unsigned int average;
unsigned char dig0, dig1, dig2, dig3;

SoftwareSerial Serial7Seg = SoftwareSerial(RXPin, TXPin);

void setup()
{
  pinMode(RXPin, INPUT);
  pinMode(TXPin, OUTPUT);
 
  Serial7Seg.begin(9600);
 
  Serial7Seg.print("v"); 
  Serial7Seg.print("-"); 
  Serial7Seg.print("H");
  Serial7Seg.print("I");
  Serial7Seg.print("-");
}

void loop()
{
  average = 0;
 
  for (int i = 0; i < AVG_COUNT; i++)
  {
    inches[i] = analogRead(SensorPin);
    inches[i] /= 2;  // convert to inches
    delay(50);  // update rate is 20Hz
  }
  for (int i = 0; i<AVG_COUNT; i++)
    average += inches[i];
  average /= AVG_COUNT;

  if (AVG_COUNT <= 50)
    delay(100-AVG_COUNT/2);
 

  dig0 = average/1000;
  dig1 = average/100 - dig0*10;
  dig2 = average/10 - dig0*100 - dig1*10;
  dig3 = average - dig0*1000 - dig1*100 - dig2*10;
 
  Serial7Seg.print("v");
  Serial7Seg.print(dig0);
  Serial7Seg.print(dig1);
  Serial7Seg.print(dig2);
  Serial7Seg.print(dig3);
}

Moderator edit: There are really good reasons we ask people to post code in code tags.

AWOL

"Pete, it's a fool (who) 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.
I speak for myself, not Arduino.

nkitch

Can I use both the PWM output and the analog output on the matbotix at the same time?

One to control the LEDs and one to give output to the 7 segment display?

AWOL

The PWM output is the closest there is to analogue output.
"Pete, it's a fool (who) 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.
I speak for myself, not Arduino.

nkitch

The matbotix actually has both a PWM output and an analog output, the analog output give off a certain voltage depending on the distance, so it tends to run a little smoother with arduino...

Go Up