Monday, November 26, 2012

Arduino Challenge Problem

Problem Statement

Program ARDX hobby servo to open and close circuit which lights up an LED.

Key Facts:

  1. Servo reads write data from 0 to 180.
  2. Two push buttons are the easiest way to start and stop servo.
  3. Paper clips and wire are useful in making simple switches.

Solution:

Note that this servo switch design actually has capability to connect to leads on either side of the 180 degrees of rotation as it was designed for a different purpose.  This is not necessary to close the circuit, only one lead is plugged in, as we can see from the LED activation.




CODE


//Arduino Challenge Response - Arduino Mechanical Switch
//Jenny Madsen
//11/26/2012

/*Program rotates servo 180 degrees when button
*one is running, and reverses 180 degrees when
*button two is pushed effectively opening and
*closing circuit which lights LED
*/


#include <Servo.h> //to include servo name
Servo myservo; //create boject to control servo
int pos=30; //variable to store iitial servo position
int inputPin1=2; //nuber of button input
int val1 = 0; //variable for reading pushbutton status
int inputPin2=3;
int val2=0; //variable for reading pushbutton status

void setup()  {
  pinMode(inputPin1, INPUT); //input push button 1
  pinMode(inputPin2, INPUT); //input push button 2
  myservo.attach(9); //attaches servo pin input to pin 9 to servo
}

void loop()  {
  //read state of button
  val1=digitalRead(inputPin1); //read input from user for button one
  val2=digitalRead(inputPin2);//read input from user for button two
  //check if button is pressed
 
  if (val1==LOW)  {
    myservo.write(180); //rotate servo to 180 degrees
   
  }
  else if (val2==LOW)
  {
   myservo.write(pos); //return to initial
   delay(20); //delay program 20ms for servo rotation


  }
}
 

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home