Course Description

Art B2050, Fall 2013, DIAP at CCNY
A survey of modern electromechanical construction. Lessons interweave hardware, firmware, software and networking. Specific focus on paper and cardboard prototyping.

Wednesday, November 20, 2013

Week 12, 11/20: Motor IO

It's your job to apply the code from the Light IO tutorial to a servo motor and potentiometer.

Code for a Standard Servo
#include <Servo.h> 
Servo SDservo;
int servoPin = 9;
int potPin = 0;
int potVal = 0;

void setup(){ 
  SDservo.attach(servoPin);


void loop(){ 
  controlServo();
  delay(15);


void controlServo(){
  potVal = analogRead(potpin);
  int mappedVal = map(potVal, 0, 1023, 0, 179);
  SDservo.write(mappedVal);
}

Code for a Continuous Rotation Servo
#include <Servo.h>

Servo CRservo;
const int halt = 90;

void setup(){
  CRservo.attach(7);
}

void loop(){
  CRservo.write(halt+20);
  delay(500);
  CRservo.write(halt-20);
  delay(500);
  CRservo.write(halt);
  delay(2000);
}

Code for Four Standard Servos
#include <Servo.h> 

Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;

int pos = 0;

void setup(){ 
  Serial.begin(9600);
  servo1.attach(3);
  servo2.attach(4);
  servo3.attach(5);
  servo4.attach(6);

void loop() { 
  sweep(servo1, 1, 80, 120, 30);
  sweep(servo2, 1, 80, 160, 15);
  sweep(servo3, 1, 100, 160, 15); 
  sweep(servo4, 1, 60, 140, 15);

void sweep(Servo s, int inc, int smin, int smax, int del){
  for(int pos=smin; pos < smax; pos += inc){
    s.write(pos);
    delay(del);
  }
}

No comments:

Post a Comment