Programming - lynchej/Piano-Arm GitHub Wiki

#Introduction to Code

For this project, the Arduino IDE will be used. To produce the Piano Arm, we will use multiple different functions. Before writing any code, it is important to understand the function that will be used.

Be sure to read the notes below to understand what each function does.

Remember that delay is set in milliseconds, so delay(1000) will set a delay of one second. The delays used in this code were intentionally long so as to make each movement distinct.

#Code The first thing to do is load the servo library. This can be done by typing:

#include <servo.h>

Using the servo library, you will want to set calibrations for the motors, which can be done by typing:

Servo middle, left, right, claw;
int leftCallib = 45;
int rightCallib = 45;
int middleCallib = 90;
int clawCallib = 0;

The setup() function for this project will be used to initialize the four motors:

void setup() {
  Serial.begin(9600);
  middle.attach(11);
  left.attach(10);
  right.attach(9);
  claw.attach(6);
}

The first written function is the turn() function which will turn the Piano Arm towards the first note:

void turn(int x, int y){
  int i = y;
  while(i <= x){
    middle.write(i);
    delay(50);
    i = i + 5;
  }
}

The second written function is the up() function which will place the Piano Arm into the playing position:

void up(){
  int i = 45;
  int j = 45;
  while(i > 0 && j < 90){
    left.write(j);
    right.write(i);
    delay(50);
    i = i - 5;
    j = j + 5;
  }
}

The third written function is the turnBack() function which turns the Piano Arm towards its next note:

void turnBack(int x, int y){
  int i = x;
  while(i > y){
    middle.write(i);
    delay(50);
    i = i - 5;
  }
}

The fourth function is the playNote() function which will drop the arm of the Piano Arm to strike the piano key, thus playing the note:

void playNote(){
  left.write(0);
  right.write(90);
  delay(1000);
}

The fifth function is the playTetra() function. This function will combine all of the above functions, to allow the Piano Arm to play the entire C Tetrachord:

void playTetra(){
  int x = 90; //initiatlizes the current position for use in turn
  int y = 65; //initializes the first desired position for turnBack
  int z = 90; //initializes the first current position for turnBack
  
  up(); //sets arms to playing position
  delay(1000); 
  turn(x, 0); //resets the base to the playing position
  delay(1000);
  playNote(); //runs the playNote function to play 'C'
  delay(1000);
  
  up(); 
  delay(1000);
  turnBack(z, y); //turns the arm towards the next note that will be played
  delay(1000);
  playNote(); //runs the playNote function to play 'D'
  z = y; //stores the current location for turnBack
  y = 55; //sets the next location for the crane
  delay(1000);
  
  up();
  delay(1000);
  turnBack(z, y);
  delay(1000);
  playNote(); //runs the playNote function to play 'E'
  z = y;
  y = 40;
  delay(1000);
  
  up();
  delay(1000);
  turnBack(z, y);
  delay(1000);
  playNote(); //runs the playNote function to play 'F'
  
}

Finally, we will place playTetra() into the loop() function to constantly be running the C Tetrachord:

void loop() {
  playTetra();
  delay(2000);
  
}
⚠️ **GitHub.com Fallback** ⚠️