Trying To Use a UV Sensor to control a Servo

I’m working on an Arduino Uno for an engineering project. I’m trying to use the values from a UV sensor breakout to control a servo like a knob. I was able to correctly set everything up. I am able to read the values from the UV sensor using analog read, but am having trouble using it to control the sensor. The issue is that I can’t program my way out of a paper bag, so I’m stuck at this stage of my project. If anyone could offer me any help or advice at all, that would be greatly appreciated.

*/

//Hardware pin definitions

int UVOUT = A0; //Output from the sensor

int REF_3V3 = A1; //3.3V power on the Arduino board

#include <Servo.h>

Servo myservo; // create servo object to control a servo

int UVpin = 0; // analog pin used to connect the potentiometer

int runningValue; // variable to read the value from the analog pin

void setup()

{

myservo.attach(9); // attaches the servo on pin 9 to the servo object

Serial.begin(9600);

pinMode(UVOUT, INPUT);

pinMode(REF_3V3, INPUT);

Serial.println("MP8511 example");

}

void loop()

{

int uvLevel = averageAnalogRead(UVOUT);

int refLevel = averageAnalogRead(REF_3V3);

//Use the 3.3V power pin as a reference to get a very accurate output value from sensor

float outputVoltage = 3.3 / refLevel * uvLevel;

float uvIntensity = mapfloat(outputVoltage, 0.99, 2.9, 0.0, 15.0);

Serial.print("MP8511 output: ");

Serial.print(uvLevel);

Serial.print(" MP8511 voltage: ");

Serial.print(outputVoltage);

Serial.print(" UV Intensity (mW/cm^2): ");

Serial.print(uvIntensity);

Serial.println();

delay(150);

}

//Takes an average of readings on a given pin

//Returns the average

int averageAnalogRead(int pinToRead)

{

byte numberOfReadings = 8;

unsigned int runningValue = 0;

for(int x = 0 ; x < numberOfReadings ; x++)

runningValue += analogRead(pinToRead);

runningValue /= numberOfReadings;

return(runningValue);

}

//The Arduino Map function but for floats

//From: http://forum.arduino.cc/index.php?topic=3922.0

float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)

{

return (x ­ in_min) * (out_max ­ out_min) / (in_max ­ in_min) + out_min;

runningValue = analogRead(UVpin); // reads the value of the potentiometer (value between 0 and 1023)

runningValue = map(runningValue, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)

myservo.write(runningValue); // sets the servo position according to the scaled value

delay(15); // waits for the servo to get there

}

Describe the problem.

jremington:
Describe the problem.

The problem, obviously, is that the value from the sensor is never actually used to move the servo.

Yes, I see now. There is that "return" in front of the code that would have moved the servo...

float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)

{

return (x ­ in_min) * (out_max ­ out_min) / (in_max ­ in_min) + out_min;

runningValue = analogRead(UVpin); // reads the value of the potentiometer (value between 0 and 1023)

runningValue = map(runningValue, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)

myservo.write(runningValue); // sets the servo position according to the scaled value