Skip to content

Instantly share code, notes, and snippets.

@aberant
Created September 7, 2009 22:04
Show Gist options
  • Save aberant/182573 to your computer and use it in GitHub Desktop.
Save aberant/182573 to your computer and use it in GitHub Desktop.
#include <Servo.h>
Servo my_servo; // create servo object to control a servo
int servo_angle = 0;
int sensor_pin = 2;
int servo_pin = 9;
int scan_values[180];
void setup()
{
Serial.begin( 9600 );
my_servo.attach( servo_pin ); // attaches the servo on pin 9 to the servo object
}
void loop()
{
for( int i = 0; i < 180; i++ ) {
my_servo.write( i );
scan_values[i] = sensor_read();
delay( 5 );
}
my_servo.write( degree_of_max_reading( scan_values ) );
delay( 2000 );
}
int sensor_read() {
int total_samples = 5;
int sum = 0;
for( int i = 0; i < total_samples; i++ ) {
sum += analogRead( sensor_pin );
}
return sum/5;
}
int degree_of_max_reading( int values[] ) {
int my_max = 0;
int angle = 0;
for( int i = 0; i < 180; i++ ) {
if ( values[i] > my_max ) {
my_max = values[i];
angle = i;
}
}
return angle;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment