Sunday, November 25, 2012

Arduino Challenge Problem

Problem Statement


Make a servo rotate 180 degrees in one direction, then pause for half of a second and rotate back, pausing another half of a second then repeating.  At close to the 0 degree position (I chose the 1 degree position), a red light should turn on, and at the 180 degree position, a green light should turn on.

Key Facts
  • if commands are important for making lights turn on at specific points
  • for the servo wire, the orange connects to your digital control pin, the red connects to the +5 volts pin, the brown connects to the ground
  • be wary of special servo functions
Solution

Hardware




Code

#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos = 0;    // variable to store the servo position 
 
void setup() 
  pinMode(13, OUTPUT);
  pinMode(11, OUTPUT);
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
 
 
void loop() 
  if (pos = 1)                  // when servo is at 1 degree an led on pin 11 turns on 
{
  digitalWrite(11, HIGH);
  delay(500);
  digitalWrite(11, LOW);
}
  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  if (pos = 180)                    // when servo at 180 degrees, led on pin 13 turns on
  { 
 
  digitalWrite(13, HIGH);
delay(500);
  digitalWrite(13, LOW);
  }
  
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  

Problem Documentation

Author:  Austin Tanner 
(servo sketch primarily based off of "Sweep" by BARRAGAN <http://barraganstudio.com>)
Date Created:  11/25/2012




No comments:

Post a Comment