Wednesday, November 14, 2012

Arduino Challenge Problem

Problem Statement
Use an arduino to operate a Servo by moving it back and forth.

This skill is very important to Engineers because so far in this class we have not worked with Servo's, but it is a skill we should know how to do and there is lots of info online for how to do this.  I am keeping the task simple since it is a new concept that we should learn.

Key Facts

Power

Connect the red from servo to +5V on arduino.

Ground

Connect black/brown from servo to Gnd on arduino.

Signal

Connect white/orange from servo to Analog in 0 on arduino.


Solution (Physical World)A working moving servo.





Code
#include <Servo.h>

Servo servo1; Servo servo2;


void setup() {

  pinMode(1,OUTPUT);
  servo1.attach(14); //analog pin 0
  //servo1.setMaximumPulse(2000);
  //servo1.setMinimumPulse(700);

  servo2.attach(15); //analog pin 1
  Serial.begin(19200);
  Serial.println("Ready");

}

void loop() {

  static int v = 0;

  if ( Serial.available()) {
    char ch = Serial.read();

    switch(ch) {
      case '0'...'9':
        v = v * 10 + ch - '0';
        break;
      case 's':
        servo1.write(v);
        v = 0;
        break;
      case 'w':
        servo2.write(v);
        v = 0;
        break;
      case 'd':
        servo2.detach();
        break;
      case 'a':
        servo2.attach(15);
        break;
    }
  }

  Servo::refresh();

}



Problem Documentation
Author: Chris Jerue

Date: 11/14/12

No comments:

Post a Comment