Hallowuino

— golan @ 7:33 pm

Arduino controlling 3 servos.
Caution: use more than 2 servos and you’re probably gonna run out of juice off the USB. If so, you’re going to need a 9v battery.

 

#include  
 
Servo servo1;
Servo servo2;
Servo servo3;
 
int pos1 = 0;    // variable to store the servo position 
int pos2 = 90;
int pos3 = 0;
 
int crement1 = 15;
int crement2 = 10;
int crement3 = 2;
 
void setup() 
{ 
  servo1.attach(3);  // attaches the servo on pin 3 to the servo object 
  servo2.attach(5);
  servo3.attach(6);
  Serial.begin(9600);
} 
 
 
void loop() 
{ 
 
  servo1.write(pos1);
  servo2.write(pos2);
  servo3.write(pos3);
 
  pos1 = pos1 + crement1;
  if (pos1 <= 0 || pos1 >= 180) {
    crement1 = -crement1;
  }
 
  pos2 = pos2 + crement2;
  if (pos2 <= 0 || pos2 >= 180) {
    crement2 = -crement2;
  }
  
  pos3 = pos3 + crement3;
  if (pos3 <= 0 || pos3 >= 180) {
    crement3 = -crement3;
  }
 
  delay(5);
  
  Serial.print(pos1);
  Serial.print(" ");
  Serial.print(pos2);
  Serial.print(" ");
  Serial.println(pos3);
 
}

Light sensor controlled twitchy servo. Servo twitches randomly when it gets dark.


#include  
Servo servo1;
int pos1 = 0;
int period = 500;
int sensorValue = 0;
unsigned long lastDidItTime = 0;

void setup() { 
  servo1.attach(3);  // attaches the servo on pin 3 to the servo object 
  Serial.begin(9600);
} 


void loop() {
  sensorValue = analogRead(A0);
  unsigned long presentTime = millis();
  if(sensorValue < 90){  // if it's dark...
    unsigned long elapsedAmount = presentTime - lastDidItTime;
    if (elapsedAmount > period){  // do something spooky
      pos1 = random (0, 180); 
      servo1.write(pos1);
      lastDidItTime = presentTime;
    }
  }

}

Use the arduino to trigger a sound via Processing:

Arduino code:

void setup() {
  Serial.begin(9600);
}

void loop() {
  int sensorValue = analogRead(A0);
  if(sensorValue >100){
    Serial.println(0); 
  }else{
    Serial.println(1); // send a 1 if sensor is tripped
  }
}

Processing code:


import ddf.minim.*;
import processing.serial.*;

AudioPlayer player;
Minim minim;
Serial myPort;
int serialInput = 0;

void setup()
{
  size(512, 200, P2D);

  minim = new Minim(this);
  println(Serial.list());
  myPort = new Serial(this, Serial.list()[0], 9600);  

  // load a file, give the AudioPlayer buffers that are 2048 samples long
  player = minim.loadFile("groove.mp3", 2048);  // point to your audio file
  // play the file
}

void draw()
{
  background(0);
  stroke(255);

  if (serialInput == 1) { // if a 1 is detected, play the sound
    player.play();
  } 
  else {
    player.pause(); // otherwise, stop and rewind the track
    player.rewind();
  }
}

void stop()
{
  // always close Minim audio classes when you are done with them
  player.close();
  minim.stop();  
  super.stop();
}


void serialEvent(Serial myPort) {
  // get the ASCII string:
  String inString = myPort.readStringUntil('\n');

  if (inString != null) {
    // trim off any whitespace:
    inString = trim(inString);
    serialInput = int(inString);
  }
}

0 Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

You must be logged in to post a comment.

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.
(c) 2019 CMU Electronic Media Studio II, Fall 2011, Section A | powered by WordPress with Barecity