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);
  }
}

Thursday, November 14, 2013

Week 11, 11/13: Light IO



Code Version 1

int LEDpin = 9;
int PRpin = A0;
int PRval, LEDval;

void setup(){
  pinMode(LEDpin, OUTPUT);
  pinMode(PRpin, INPUT);
  Serial.begin(9600);
}

void loop(){
  readPR();
  writeLED();
}

void readPR(){
  PRval = analogRead(PRpin);
  Serial.print("PRval = ");
  Serial.print(PRval);
}

void writeLED(){
  LEDval = map(PRval, 0, 450, 0, 255);
  analogWrite(LEDpin, LEDval);
  Serial.print(", LEDval = ");
  Serial.println(LEDval); 
}

Code Version 2

int LEDpin = 9;
int PRpin = A0;
int PRval, LEDval, num;

void setup(){
  pinMode(LEDpin, OUTPUT);
  pinMode(PRpin, INPUT);
  Serial.begin(9600);
}

void loop(){
  readPR();
  Serial.print("PRval = ");
  Serial.print(PRval);
  if(PRval < 10) num = 1;
  else if(PRval > 300) num = 3;
  else num = 2;
  switch(num){
    case 1: // PR is in shadow
      Serial.println("shadow");
      digitalWrite(LEDpin, LOW);
      break;
    case 2: // PR is in ambient light
      Serial.println("ambient");
      writeLED();
      break;
    case 3: // PR is in bright light
      Serial.println("bright");
      digitalWrite(LEDpin, LOW);
      break;
  }
  delay(20);
}

void readPR(){
  PRval = analogRead(PRpin);
}

void writeLED(){
  LEDval = map(PRval, 0, 450, 255, 0);
  analogWrite(LEDpin, LEDval);
}

Wednesday, November 6, 2013

Week 10, 11/6: Debugging

So far we've seen all the ways a physical computing project can be difficult. Here's a troubleshooting checklist to help you figure out what's going wrong.

1. Is the code correct?
Code is the most complex and most edited part of a project, so it's the most likely source of a bug. This is why it's so important to keep your code clean and well-organized. Give each component its own function, keep variables local, and give the functions and variables obvious names. Use Serial.print() and Serial.println() liberally (then comment them out when you're done).

2. Did it work before you changed something?
If yes, isolate the change. Comment things out until you've located the problem. Save working versions of the code with clear version numbers (and don't save non-working versions). It may be that the newest change revealed an older bug, but that's less likely. If the code has never worked, and you're sure the code is right, check the wiring.

3. Is the wiring correct?
Visually inspect all soldered and solderless connections. Use the multimeter on the continuity setting to ensure conduction is happening where it's needed, and not happening elsewhere. Use the multimeter on the DC voltage setting to make sure the right amount of power is getting to each component. Isolate each component to make sure they're not interfering with each other.