Pages: [1]   Go Down
Author Topic: MaxSonar EZ1 and Arduino Uno  (Read 937 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi guys, I'm programming the MaxSonar sonar sensor to sense objects within a range. When an object enters the range, a LED module that is wired up to the PWM pin 11 will light up accordingly with the distance to the sensor. I used the PWM as I have a MOSFET in between the module and the Uno as it needed 3.1V

This is my programm. Every time I try to delay the adapting of the brightness so it won't flicker, the cm measurements will not drop back to real values. Can anyone help me?

Code:
#include <math.h>

//Analog pin 1 for reading in the analog voltage from the MaxSonar device.
//PWM pin 11 is for lighting the LED module
//This variable is a constant because the pin will not change throughout execution of this code.
const int anPin = 1;
const int LEDs = 11;

//variables needed to store values
float anVolt, inches, cm;
int sum=0;//Create sum variable so it can be averaged
int avgrange=60;//Quantity of values to average (sample size)
float brightness, oldbrightness, goalbrightness;// for setting the brightness of the LED
float minbright=10.0;//the dimmed light
float maxbright=200.0;//the maximum light the LEDs will gave
float longrange=50.0;//set the outer range

void setup() {
  //This opens up a serial connection to shoot the results back to the PC console
  Serial.begin(9600);
}
void loop() {
  pinMode(LEDs, OUTPUT);
  pinMode(anPin, INPUT);
  //MaxSonar Analog reads are known to be very sensitive.
  //A simple fix is to average out a sample of n readings to get a more consistant reading.\\
  //This loop gets 60 reads and averages them

  for(int i = 0; i < avgrange ; i++)
  {
    //Used to read in the analog voltage output that is being sent by the MaxSonar device.
    //Scale factor is (Vcc/512) per inch. A 5V supply yields ~9.8mV/in
    //Arduino analog pin goes from 0 to 1024, so the value has to be divided by 2 to get the actual inches
    anVolt = analogRead(anPin)/2;
    sum += anVolt;
    delay(7);
  } 
  inches = sum/avgrange;
  cm = inches * 2.54;
 
  if (cm < longrange)
  {
     goalbrightness = minbright + (maxbright - (maxbright * (cm / longrange)));
  }
  else
  {
     goalbrightness = minbright;
  }
 
  if (abs(goalbrightness - oldbrightness) > 5)
  {
   brightness += 0.3 * (goalbrightness - oldbrightness);
  }
 
  analogWrite(LEDs, brightness);
 
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm, ");
  Serial.print(brightness);
  Serial.println();
 
  //reset sample total
  sum = 0;
  oldbrightness = brightness;
  //delay(10);
}
Logged

Ontario, Ohio
Offline Offline
Full Member
***
Karma: 1
Posts: 208
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What is the mosfet for? You can make a voltage divider circuit with a couple resistors if you want 3.1 volts to go to the sensor, but it'll also just use the 5 volts from the arduino just fine (see the data sheet). Try that, and see what happens. There are also much simpler ways to write your code. Was that code written for some other function that you adapted for this purpose?
This string is about a ping sensor and fading an led, but it should give you some ideas: http://arduino.cc/forum/index.php/topic,86560.0.html
« Last Edit: June 29, 2012, 07:36:21 pm by TeslaIaint » Logged

Ontario, Ohio
Offline Offline
Full Member
***
Karma: 1
Posts: 208
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

check out this tutorial:
http://www.jeremyblum.com/2011/01/24/arduino-tutorial-4-analog-inputs/
Logged

Pages: [1]   Go Up
Jump to: