Servo Guitar

Submitted by stlim on Sun, 04/21/2013 - 21:57

Description

This is a stringed instrument played by four servo motors. Each of them reacts to a photocell's value, so four inputs of blocking light can make sound from four strings with different length. Users can collaboratively play this instrument by controlling one photocell for each.

 

Components Used

- 1 Arduino board

- 4 photocells

- 4 servo motors

- etc.

 

Code

 

#include <Servo.h>
Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;
 
const int PHOTO_THRESHOLD_1 = 200;
const int PHOTO_THRESHOLD_2 = 200;
const int PHOTO_THRESHOLD_3 = 400;
const int PHOTO_THRESHOLD_4 = 400;
 
int pos1 = 0;
int pos2 = 0; 
int pos3 = 0;
int pos4 = 0;
 
int photo1 = 0;   // select the input pin for the potentiometer
int photo2 = 1;
int photo3 = 2;
int photo4 = 3;
 
int ledPin1 = 13;   // select the pin for the LED
int val1=0;
int val2=0;
int val3=0;
int val4=0;
 
void setup()
{
  myservo1.attach(7);
  myservo2.attach(6);
  myservo3.attach(5);
  myservo4.attach(4);
  Serial.begin(9600); 
}
 
void loop()
{
  val1 = analogRead(photo1);    // read the value from the sensor, between 0 - 1024
  val2 = analogRead(photo2);    // read the value from the sensor, between 0 - 1024
  val3 = analogRead(photo3);    // read the value from the sensor, between 0 - 1024
  val4 = analogRead(photo4);    // read the value from the sensor, between 0 - 1024
 
  Serial.print("val1: ");
  Serial.print(val1);
  Serial.print("; val2: ");
  Serial.print(val2);
  Serial.print("; val3: ");
  Serial.print(val3);
  Serial.print("; val4 ");
  Serial.print(val4);  
  Serial.println(" ");
 
  if (val1 >= PHOTO_THRESHOLD_1){
    pos1 = 40;
  }
  else {
    pos1 = 10;
  }
  if (val2 >= PHOTO_THRESHOLD_2){
    pos2 = 40;
  }
  else {
    pos2 = 10;
  }
  if (val3 >= PHOTO_THRESHOLD_3){
    pos3 = 40;
  }
  else {
    pos3 = 10;
  }
  if (val4 >= PHOTO_THRESHOLD_4){
    pos4 = 40;
  }
  else {
    pos4 = 10;
  }
  myservo1.write(pos1);
  myservo2.write(pos2);
  myservo3.write(pos3);
  myservo4.write(pos4);
  delay(2);
}
 
IMG_0562.JPG
0
Your rating: None
Drupal theme by Kiwi Themes.