Friday, September 25, 2009

Arduino Solar Detector

So, now that the project is over, I have decided to post the code that directed our Arduino units, in the interest of keeping this class as open source as possible. Our code was mostly derived from the Arduino meets Processing project.

/*
* ap_LDRAiming
*
* Reads an analog input from the input pin and sends the value
* followed by a line break over the serial port.
*
* This file is part of the Arduino meets Processing Project:
* For more information visit http://www.arduino.cc.
*
* copyleft 2005 by Melvin Ochsmann for Malm� University
*
*/
#include
Servo myServo;

// variables for input pin and control LED
int PhotoA = 3;
int PhotoB = 4;
int PhotoC = 5;
int LEDpin = 13;
int pos = 0;

// variable to store the value
int valueA = 0;
int valueB = 0;
int valueC = 0;
// a threshold to decide when the LED turns on
int threshold = 750;
void setup(){

// declaration of pin modes
pinMode(PhotoA, INPUT);
pinMode(PhotoB, INPUT);
pinMode(PhotoC, INPUT);
pinMode(LEDpin, OUTPUT);
myServo.attach(9);
// begin sending over serial port
Serial.begin(9600);
}

void loop(){
// read the value on analog input
valueA = analogRead(PhotoA);
valueB = analogRead(PhotoB);
valueC = analogRead(PhotoC);

// if value greater than threshold turn on LED
if (valueA <>

// print out value over the serial port
Serial.print(valueA);
Serial.print(",");
Serial.print(valueB);
Serial.print(",");
Serial.print(valueC);
// and a signal that serves as seperator between two values
Serial.print(";");
// Checks if right is greater than left, if so move to right.

if (valueB > (valueC +20))
// +20 is the deadzone, so it wont jiggle back and forth.
{
if (179 > pos)
pos++;
myServo.write(pos);
}

// Checks if left is greater than right, if so move to left.
if (valueC > (valueB +20))
// +20 is the deadzone, so it wont jiggle back and forth.
{
if (pos > 1)
pos -= 1;
myServo.write(pos);
}

// wait for a bit to not overload the port
delay(10);
}

The code includes a serial input that we used to judge whether or not our solar detection was working properly in a numerical sense. By slowing the program down to a delay of 250, we were able to tweak the code in order to ensure that the readings were proper, and that we could utilize the changes in direction properly.

The biggest issue was ambient lighting, which we were able to counteract with a shade of sorts while in design, but in final display we could not, because of the amount of ambient light in the center room. We set our thresholds slightly too high for the situation that we were in.

No comments:

Post a Comment