Showing posts with label Arduino. Show all posts
Showing posts with label Arduino. Show all posts

Sunday, October 18, 2009

Inarticulate Eloquence Opens and Closes with the Sun

Here is the code for our Hoberman Surface. For include statements, blogger likes to take the <> as HTML code, so I just took them out. Also, if statements with > < etc are changed to 'is less than' 'is greater than' etc to prevent the same issues.

The Stepper Motor - Arduino
#include afmotor.h

AF_Stepper LeftMotor(48, 1);
int position = 1;

void setup() {
Serial.begin(9600);
Serial.println("Stepper test!");

LeftMotor.setSpeed(250);
}

void loop() {
if(Serial.available()){
Serial.println("Available");
char ch = Serial.read();

switch(ch){
case '+':
if (position == 0){
LeftMotor.step(2250, FORWARD, SINGLE);
position = 1;
Serial.println(position);
}
break;
case '-':
if (position == 1){
LeftMotor.step(2250, BACKWARD, SINGLE);
position = 0;
Serial.println(position);
}
break;
}
}
}

The LDR - Arduino
int PhotoA = 3;
int LEDpin = 13;
int valueA = 0;
int threshold = 500;

void setup(){
pinMode(PhotoA, INPUT);
pinMode(LEDpin, OUTPUT);

Serial.begin(9600);
}

void loop(){
valueA = analogRead(PhotoA);

if (valueA is less than threshold){
digitalWrite(LEDpin, HIGH);
Serial.print('+');
}
else {
digitalWrite(LEDpin, LOW);
Serial.print('-');
}

delay(1000);
}

The Link - Processing
import processing.serial.*;

Serial MotorPort;
Serial LDRPort;

void setup()
{
size(720, 720);
colorMode(RGB, 1.0);
noStroke();
rectMode(CENTER);
frameRate(100);

println(Serial.list());
MotorPort = new Serial(this, Serial.list()[0],9600);
LDRPort = new Serial(this, Serial.list()[2],9600);
}

void draw()
{

char ch = LDRPort.readChar();
MotorPort.write(ch);

}

Fairly simple code this time. Got the job done.

Thursday, October 1, 2009

Here Comes The Sun

Our team project for the week was to design and implement a modular, scalable solar tracker.

The project started out the same way that last week's project did - I stepped into coding like it is my true calling or something. The sun follows a path defined by two variables - azimuth and altitude. Therefore, we developed a design very similar to the design that Eric's group and my group both utilized last week, with two axes - rotation and tilt. We chose to utilize the base scheme that his group developed, as it was much more stable and easier to work with for this implementation.

The Arduino code for our project is as follows:

#include
#include

Servo servo1; Servo servo2;
Potentiometer potentiometer1 = Potentiometer(3);
Potentiometer potentiometer2 = Potentiometer(4);

void setup() {
servo1.attach(9);
servo2.attach(10);
pinMode(13,OUTPUT);
Serial.begin(9600);
Serial.println("Ready");
potentiometer1.setSectors(180);
potentiometer2.setSectors(180);
}

void loop() {

static int v = 0;
if (Serial.available()) {
char ch = Serial.read();

switch(ch) {
case '0'...'9':
v = v * 10 + ch - '0';
digitalWrite(13,LOW);
break;
case 's':
servo1.write(v);
v = 0;
break;
case 'w':
servo2.write(v);
v = 0;
break;
case 'p':
do{
servo1.write(potentiometer1.getSector());
servo2.write(potentiometer2.getSector());
digitalWrite(13,HIGH);
}
while(Serial.read()!='i');
break;
}
}
}

It allows for both serial input and potentiometer utilization to control the servos. Really quite simple, but it does so much. I prefer to have the Arduino as simple as possible whenever possible, and to utilize the computer to do any real processing and calculations. Therefore, the remainder of the code was written in Processing. Here is a snippet from the Processing code.

YrFract = 2*PI/365*(doy-1+(Hour-12)/24); // Fractional Year, Radians
eqtime = 229.18*(0.000075+0.001868*cos(YrFract)-0.032077*sin(YrFract)-0.014615*cos(2*YrFract)-0.040849*sin(2*YrFract)); // Equation of Time, Minutes
decl = 0.006918-0.399912*cos(YrFract)+0.070257*sin(YrFract)-0.006758*cos(2*YrFract)+0.000907*sin(2*YrFract)-0.002697*cos(3*YrFract)+0.00148*sin(3*YrFract); // Declination, Radians
time_offset = eqtime-4*longitude+60*5; // Solar Time Offset, Minutes
tst = Hour*60+Min+Sec/60+time_offset; // True Solar Time, Minutes
ha = (float)Math.toRadians((tst/4)-180); // Solar Hour Angle, Radians
za = acos(sin(latitude)*sin(decl)+cos(latitude)*cos(decl)*cos(ha)); // Zenith Angle, Radians
azim = acos(-(sin(latitude)*cos(za)-sin(decl))/(cos(latitude)*sin(za))); // Azimuth, Radians, Clockwise from North

This code is the calculation for solar position, given inputs (doy is day of year, the rest is fairly self-explanatory). The azimuth is always positive, so a switch in the code must be utilized to switch between rising and setting. I'll post the full code on Friday morning at the start of class, it's not 100% yet.

I feel like this team has meshed the best out of all the teams that I have worked with so far. Neil and I worked on the electronics portion, and we work very well together. The design process was universal, with everyone inputting, but the implementation process essentially was niche, with engineers doing circuitry and coding, architects doing Schmigital Schmroject and some construction, and artists doing construction. Overall, the project has worked really well, and I look forward to the final result.

Friday, September 18, 2009

SmartSurfaces, Day Two, 9/18/09

Day two was a massive improvement over day one, in every way.

We led out by introducing my lamp, which was pretty awesome I suppose, but I didn't expect to be in the spotlight for it... other people had much better lamps, especially the rangefinder - that was extremely impressive.

I am a huge fan of the work with physical computing, and I have been experimenting with the Arduino since we got it. I found a program called Processing, which can be set up to interact with inputs from the Arduino and uses the data for other things, such as compiling graphics. I intend to look deeper into this, hopefully designing an extremely neat program.

Day Two: Project

Our project for the day was to design and construct a light tracking device utilizing photoresistors to detect where a light source is. We were divided into groups, and almost immediately our group was off to the races. I had been working with code for Processing which interacted with the photoresistors, and was able to help jumpstart the coding. Marc and Damien jumped into developing the code for interacting with the servos, and the remaining members of the group either helped me work with the photoresistors or worked with the cardboard, whatever was needed.

The worst part about this project was the amount of time given - we only had an hour, and only discovered the code for the servos existed already 45 minutes in. Our group pulled together well, however. Everyone did their parts, and we were able to come up with a finalized design with finalized code, at the very least, even though we did not construct the actual unit completely. Other groups were in approximately the same boat as us.

After the presentations, we were told to meet later on to finish construction of our unit. We met Sunday night, and after a couple of extra hours and several wires that wouldn't stay where we wanted them, we were able to construct our unit and get it operational, if a little jittery. The unit will track light, though it is not the best at tracking small point light sources - it is just not feasible without a full array of sensors. Also, ambient light is the enemy of this project, so we built a shade for it in order to keep ambient light off of it.

I felt our group worked much better than last week's group. There was a definite cohesion, everyone knew what they had to do, and there were very few moments where someone was 'lost'. The work got done, and we were all very proud of the final result.

Day Two: Thoughts Going Forward

I have worked with Allison twice now, and I am hoping she winds up in my final group - she has done a great job each week to learn quickly when she doesn't understand and to do what needs to get done when she does. She brings great ideas to the table, and has been a valuable member of each group.

I feel like the Arduino is going to be great to work with, and look forward to whatever this class has in store for us next. I haven't quite gotten to play with Design Studio, that is my next project, for tomorrow.

Tuesday, September 15, 2009

Introduction to Arduino


Our first major task outside of class was to prepare an Arduino lamp.

I have never used an Arduino before this, and let me just say that I am extremely impressed with how versatile the little thing is. I found a large collection of Arduino projects online that are inexpensive and yet extremely useful, as well as things that are just plain neat. If I had the time or patience, I would develop something controlled by a Wii Remote.

Development of the lamp was fairly straightforward, following the tutorials I came up with this (shredded toilet paper in place of tissue paper).


After I got everything set up, I started to consider the neat things that I could do with it. I had a Guitar Hero controller sitting right in front of me, and the button colors got me thinking - why not have it play a Guitar Hero song?

I went on a hunt online for an idea for what song to use, and came up with this guitar hero pattern, pulled from a full-length pattern at SlowHero:

Which corresponds to the opening to Guns N' Roses' Sweet Child O' Mine. I can't think of anything that would be more fitting... too bad I don't yet know how to get the Arduino to play music (the Arduino can interact with AppleScript, so it may be possible to get it timed to play the song with iTunes, and restart it every time the loop resets).

The code for the loop is really quite easy, though it took a little bit of experimentation to get the Arduino timed to the music correctly. I used a constant set of on/off triggers to get through the notes, and then set it up so that the loop would restart properly, without leaving a light on at the end.

Two issues came up. One, timing. It took me about 10 minutes to tweak and record until I got the Arduino to line up to the music for 15 seconds, which was all I really needed. Second, the cover. I came up with something that I thought would look better than it does, but on the plus side that isn't a cube.

Overall, I'm happy with the lamp, especially with the fact that it syncs up to Sweet Child O' Mine... Enjoy!