Exercises – Jade

Exercise 1

Arduino:

void setup() {
  Serial.begin(9600);
  Serial.println("0,0");
  pinMode(A0, INPUT);
}
 
void loop() {
  while (Serial.available()) {
    if (Serial.read() == '\n') {
     int sensor = analogRead(A0);
     Serial.println(sensor);
   }
  }
}

 

Processing:

import processing.serial.*;
Serial myPort;
int xPos=0;

void setup(){
  size(960,720);
  printArray(Serial.list());
  String portname=Serial.list()[1];
  println(portname);
  myPort = new Serial(this,portname,9600);
  myPort.clear();
  myPort.bufferUntil('\n');
}

void draw(){
  background(255);
  ellipse(xPos,height/2,30,30);
}

void serialEvent(Serial myPort){
  String s=myPort.readStringUntil('\n');
  s=trim(s);
  if (s!=null){
    println(s);
    int values[]=int(split(s,','));
    if (values.length==1){
      xPos=(int)map(values[0],0,1023,width, 0);
    }
  }
  myPort.write("\n");
}

 

 

Exercise 2

Arduino:

int bright = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("0,0");
  pinMode(2, OUTPUT);
  pinMode(5, OUTPUT);
}

void loop() {
  while (Serial.available()) {
      bright = Serial.parseInt();
      if (Serial.read() == '\n') {
        analogWrite(5, bright);
        Serial.println(0);
    }
  }
}

Processing:

import processing.serial.*;
Serial myPort;

void setup(){
  size(960,720);
  printArray(Serial.list());
  String portname=Serial.list()[1];
  println(portname);
  myPort = new Serial(this,portname,9600);
  myPort.clear();
  myPort.bufferUntil('\n');
}

void draw(){
  background(255);
  ellipse(mouseX,mouseY,30,30); // mark
}

void serialEvent(Serial myPort){
  String s=myPort.readStringUntil('\n');
  myPort.write((int)(map(mouseY, height,0, 0, 255))+"\n");
}

 

Exercise 3

Arduino:

int bounce = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("0,0");
  pinMode(5, OUTPUT);
  pinMode(A0, INPUT);
}

void loop() {
  while (Serial.available()) {
    bounce = Serial.parseInt();
    if (Serial.read() == '\n') {
      digitalWrite(5, bounce);
      int sensor = analogRead(A0);
      Serial.println(sensor);
    }
  }
}

Processing:

import processing.serial.*;
Serial myPort;

PVector velocity;
PVector gravity;
PVector position;
PVector acceleration;
PVector wind;
float drag = 0.99;
float mass = 50;
float hDampening;

boolean flag = false;

void setup() {
  size(640,360);
  printArray(Serial.list());
  String portname=Serial.list()[1];
  println(portname);
  myPort = new Serial(this,portname,9600);
  myPort.clear();
  myPort.bufferUntil('\n');
  
  
  noFill();
  position = new PVector(width/2, 0);
  velocity = new PVector(0,0);
  acceleration = new PVector(0,0);
  gravity = new PVector(0, 0.5*mass);
  wind = new PVector(0,0);
  hDampening=map(mass,15,80,.98,.96);
}

void draw() {
  background(255);
  velocity.x*=hDampening;
  applyForce(wind);
  applyForce(gravity);
  velocity.add(acceleration);
  velocity.mult(drag);
  position.add(velocity);
  acceleration.mult(0);
  ellipse(position.x,position.y,mass,mass);
  if (position.y >= height-mass/2) {
      velocity.y *= -0.9;  // A little dampening when hitting the bottom
      position.y = height-mass/2;
    } 
  if (round(velocity.y)<0) {
    flag = true;
  } else {
    flag = false;
  }
}
  
void applyForce(PVector force){
  // Newton's 2nd law: F = M * A
  // or A = F / M
  PVector f = PVector.div(force, mass);
  acceleration.add(f);
}

void keyPressed(){
  if (key==' '){
    mass=random(15,80);
    position.y=-mass;
    velocity.mult(0);
  }
}

void serialEvent(Serial myPort){
  String s=myPort.readStringUntil('\n');
  s=trim(s);
  if (s!=null) {
    println(s);
    int values[]=int(split(s, ','));
    if (values.length==1) {
      wind.x=(float)map(values[0], 0, 1023, 5, -5);
    }
  }
  myPort.write(int(flag)+"\n");

}

Serial Communication Exercises

Exercise 1:

Processing:
import processing.serial.*;
Serial myPort;
int xPos=0;

void setup(){
  size(960,720);
  printArray(Serial.list());
  String portname=Serial.list()[4];
  println(portname);
  myPort = new Serial(this,portname,9600);
  myPort.clear();
  myPort.bufferUntil('\n');
}

void draw(){
  background(255);
  ellipse(xPos,height/2,30,30);
}

void serialEvent(Serial myPort){
  String s=myPort.readStringUntil('\n');
  s=trim(s);
  if (s!=null){
    println(s);
    int values=int(s);
    if (values >= 0){
      xPos=(int)map(values,0,1023,0, width);
    }
  }
  myPort.write("\n");
}
Arduino:
void setup() {
  Serial.begin(9600);
  Serial.println("0,0");
}

void loop() {
  while (Serial.available()) {
    if (Serial.read() == '\n') {
      int sensor = analogRead(A0);
      delay(1);
      Serial.println(sensor);
    }
  }
}

Exercise 2:

Processing:
import processing.serial.*;
Serial myPort;
//int xPos=0;
//int yPos=0;
boolean onOff=false;
boolean onOff2=false;

void setup(){
  size(960,720);
  printArray(Serial.list());
  String portname=Serial.list()[4];
  println(portname);
  myPort = new Serial(this,portname,9600);
  myPort.clear();
  myPort.bufferUntil('\n');
}

void draw(){
  background(255);
  //ellipse(xPos,yPos,30,30);
  if (mousePressed){
    if(mouseX<=width/2)
      onOff2=true;
    else
      onOff=true;
  }else{
    onOff=onOff2=false;
  }
}

void serialEvent(Serial myPort){
  String s=myPort.readStringUntil('\n');
  s=trim(s);
  if (s!=null){
    println(s);
    int values[]=int(split(s,','));
    if (values.length==1){
      //xPos=(int)map(values[0],0,1023,0, width);
      //yPos=(int)map(values[1],0,1023,0, height);
    }
  }
  myPort.write(int(onOff)+","+int(onOff2)+"\n");
}
Arduino:
int left = 0;
int right = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("0,0");
  pinMode(2, OUTPUT);
  pinMode(5, OUTPUT);
}

void loop() {
  while (Serial.available()) {
    right = Serial.parseInt();
    left = Serial.parseInt();
    if (Serial.read() == '\n') {
      digitalWrite(2, right);
      digitalWrite(5, left);
      Serial.println();
    }
  }
}

Exercise 3:

Processing:
import processing.serial.*;
Serial myPort;
boolean onOff=false;
boolean onOff2=false;

PVector velocity;
PVector gravity;
PVector position;
PVector acceleration;
PVector wind;
float drag = 0.99;
float mass = 50;
float hDampening;


void setup(){
  delay(1000);
  size(960,720);
  printArray(Serial.list());
  String portname=Serial.list()[4];
  println(portname);
  myPort = new Serial(this,portname,9600);
  myPort.clear();
  myPort.bufferUntil('\n');
  
  position = new PVector(width/2, 0);
  velocity = new PVector(0,0);
  acceleration = new PVector(0,0);
  gravity = new PVector(0, 0.5*mass);
  wind = new PVector(0,0);
  hDampening=map(mass,15,80,.98,.96);
  delay(1000);
}

void draw(){
  background(255);
  applyForce(wind);
  applyForce(gravity);
  velocity.add(acceleration);
  velocity.mult(drag);
  position.add(velocity);
  acceleration.mult(0);
  ellipse(position.x,position.y,mass,mass);
  if (position.y > height-mass/2) {
      velocity.y *= -0.9;  // A little dampening when hitting the bottom
      position.y = height-mass/2;
      onOff = true;
      println("BOUNCE!!!!");
    }else{
      onOff = false;
 }
}

void serialEvent(Serial myPort){
  String s=myPort.readStringUntil('\n');
  s=trim(s);
  if (s!=null){
    println(s);
  int values[]=int(split(s,','));
  if (values.length==2){
      int mappedValues=(int)map(values[0],0,1023,0, width);
  if (mappedValues <= width/2){
    wind.x=-1;
  }
  if (mappedValues > width/2){
    wind.x=1;
  }
  }
  myPort.write(int(onOff)+","+int(onOff2)+"\n");
  println("OnOFF"+onOff);
}
}


void applyForce(PVector force){
  PVector f = PVector.div(force, mass);
  acceleration.add(f);
}

void keyPressed(){
  if (key==' '){
    mass=random(15,80);
    position.y=-mass;
    velocity.mult(0);
  }
}
Arduino:
int left = 0;
int right = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("0,0");
  pinMode(2, OUTPUT);
  pinMode(5, OUTPUT);
}

void loop() {
  while (Serial.available()) {
    right = Serial.parseInt();
    left = Serial.parseInt();
    if (Serial.read() == '\n') {
      digitalWrite(2, right);
      digitalWrite(5, left);
      delay(2);
      int sensor = analogRead(A0);
      delay(1);
      int sensor2 = analogRead(A1);
      delay(1);
      Serial.print(sensor);
      Serial.print(',');
      Serial.println(sensor2);
    }
  }
}

Video:

 

Serial Communication Exercises

Exercise 1:

 //arduino exercise #1
void setup() {
  Serial.begin(9600);
  Serial.println("0,0");
}

void loop() {
  while (Serial.available()) {
    if (Serial.read() == '\n') {
      int sensor = analogRead(A0);
      delay(1);
      Serial.print(sensor);
      Serial.println(',');
    }
  }
}
//processing exercise #1

import processing.serial.*;
Serial myPort;
int xPos=0;
int yPos=height/2;

void setup() {
  size(960, 720);
  printArray(Serial.list());
  String portname=Serial.list()[3];
  println(portname);
  myPort = new Serial(this, portname, 9600);
  myPort.clear();
  myPort.bufferUntil('\n');
}

void draw() {
  background(255);
  ellipse(xPos, yPos, 30, 30);
}

void serialEvent(Serial myPort) {
  String s=myPort.readStringUntil('\n');
  s=trim(s);
  if (s!=null) {
    println(s);
    int values[]=int(split(s, ','));
    if (values.length==2) {
      xPos=(int)map(values[0], 0, 1023, 0, width);
    }
  }
  myPort.write(xPos+','+"\n");
}

//arduino exercise #2

int left = 0;
int right = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("0,0");
  pinMode(3, OUTPUT);
}

void loop() {
  while (Serial.available()) {
    right = Serial.parseInt();
    left = Serial.parseInt();
    if (Serial.read() == '\n') {
      analogWrite(3, right);
      Serial.print(0);
      Serial.print(',');
      Serial.println(0);
    }
  }
}

Exercise 2:

//arduino exercise #2

int left = 0;
int right = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("0,0");
  pinMode(3, OUTPUT);
}

void loop() {
  while (Serial.available()) {
    right = Serial.parseInt();
    left = Serial.parseInt();
    if (Serial.read() == '\n') {
      analogWrite(3, right);
      Serial.print(0);
      Serial.print(',');
      Serial.println(0);
    }
  }
}
//processing exercise #2 

import processing.serial.*;
Serial myPort;
int xPos=0;

void setup(){
  size(960,720);
  printArray(Serial.list());
  String portname=Serial.list()[3];
  println(portname);
  myPort = new Serial(this,portname,9600);
  myPort.clear();
  myPort.bufferUntil('\n');
}

void draw(){
  background(255);
}

void serialEvent(Serial myPort){
  String s=myPort.readStringUntil('\n');
  s=trim(s);
  if (s!=null){
    println(s);
    int values[]=int(split(s,','));
    if (values.length==2){
      xPos=(int)map(mouseX,0,width, 0, 255);
    }
  }
  myPort.write(xPos+","+0+"\n");
}
//arduino exercise #3
 
void setup() {
  Serial.begin(9600);
  Serial.println("0,0");
  pinMode(3, OUTPUT);
}
 
void loop() {
  while (Serial.available()) {
    int onOff = Serial.parseInt();
    if (Serial.read() == '\n') {
      digitalWrite(3, onOff);
      int sensor = analogRead(A0);
      delay(1);
      Serial.println(sensor);
    }
  }
}

Exercise 3:

//arduino exercise #3
 
void setup() {
  Serial.begin(9600);
  Serial.println("0,0");
  pinMode(3, OUTPUT);
}
 
void loop() {
  while (Serial.available()) {
    int onOff = Serial.parseInt();
    if (Serial.read() == '\n') {
      digitalWrite(3, onOff);
      int sensor = analogRead(A0);
      delay(1);
      Serial.println(sensor);
    }
  }
}
//processing exercise #3

import processing.serial.*;
Serial myPort;

PVector velocity;
PVector gravity;
PVector position;
PVector acceleration;
PVector wind;
float drag = 0.99;
float mass = 50;
float hDampening;
boolean onOff=false;
float analog;

void setup() {
  size(640, 360);
  String portname=Serial.list()[3];
  println(portname);
  myPort = new Serial(this, portname, 9600);
  myPort.clear();
  myPort.bufferUntil('\n');

  noFill();
  position = new PVector(width/2, 0);
  velocity = new PVector(0, 0);
  acceleration = new PVector(0, 0);
  gravity = new PVector(0, 0.5*mass);
  wind = new PVector(0, 0);
  hDampening=map(mass, 15, 80, .98, .96);
}
void draw() {
  background(255);
  if (!keyPressed) {
    wind.x= analog;
    velocity.x*=hDampening;
  }
  applyForce(wind);
  applyForce(gravity);
  velocity.add(acceleration);
  velocity.mult(drag);
  position.add(velocity);
  acceleration.mult(0);
  ellipse(position.x, position.y, mass, mass);
  if (position.y > height-mass/2) {
    velocity.y *= -0.9;  // A little dampening when hitting the bottom
    position.y = height-mass/2;
  }
  
  if (position.y >= height - mass){
  onOff = true;
  }
  else{
  onOff = false;
  }
}

void serialEvent(Serial myPort) {
  String s=myPort.readStringUntil('\n');
  s=trim(s);
  int values = parseInt(s);
  if (s!=null) {
    analog =map(values, 0, 1023, -3, 3);
  }
  myPort.write(int(onOff)+"\n");
}

void applyForce(PVector force) {
  // Newton's 2nd law: F = M * A
  // or A = F / M
  PVector f = PVector.div(force, mass);
  acceleration.add(f);
}

void keyPressed() {
  if (key==' ') {
    mass=random(15, 80);
    position.y=-mass;
    velocity.mult(0);
  }
}


Exercise 3 video:

https://www.youtube.com/shorts/TAmqSXp7FqE

Serial Communication: Three Exercises

//arduino exercase #1
void setup() {
  Serial.begin(9600);
  Serial.println("0,0");
}

void loop() {
  while (Serial.available()) {
    if (Serial.read() == '\n') {
      int sensor = analogRead(A0);
      delay(1);
      Serial.print(sensor);
      Serial.println(',');
    }
  }
}
//processing exercise #1

import processing.serial.*;
Serial myPort;
int xPos=0;
int yPos=height/2;

void setup() {
  size(960, 720);
  printArray(Serial.list());
  String portname=Serial.list()[3];
  println(portname);
  myPort = new Serial(this, portname, 9600);
  myPort.clear();
  myPort.bufferUntil('\n');
}

void draw() {
  background(255);
  ellipse(xPos, yPos, 30, 30);
}

void serialEvent(Serial myPort) {
  String s=myPort.readStringUntil('\n');
  s=trim(s);
  if (s!=null) {
    println(s);
    int values[]=int(split(s, ','));
    if (values.length==2) {
      xPos=(int)map(values[0], 0, 1023, 0, width);
    }
  }
  myPort.write(xPos+','+"\n");
}
//arduino exercise #2

int left = 0;
int right = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("0,0");
  pinMode(3, OUTPUT);
}

void loop() {
  while (Serial.available()) {
    right = Serial.parseInt();
    left = Serial.parseInt();
    if (Serial.read() == '\n') {
      analogWrite(3, right);
      Serial.print(0);
      Serial.print(',');
      Serial.println(0);
    }
  }
}
//processing exercise #2 

import processing.serial.*;
Serial myPort;
int xPos=0;

void setup(){
  size(960,720);
  printArray(Serial.list());
  String portname=Serial.list()[3];
  println(portname);
  myPort = new Serial(this,portname,9600);
  myPort.clear();
  myPort.bufferUntil('\n');
}

void draw(){
  background(255);
}

void serialEvent(Serial myPort){
  String s=myPort.readStringUntil('\n');
  s=trim(s);
  if (s!=null){
    println(s);
    int values[]=int(split(s,','));
    if (values.length==2){
      xPos=(int)map(mouseX,0,width, 0, 255);
    }
  }
  myPort.write(xPos+","+0+"\n");
}
//arduino exercise #3
 
void setup() {
  Serial.begin(9600);
  Serial.println("0,0");
  pinMode(3, OUTPUT);
}
 
void loop() {
  while (Serial.available()) {
    int onOff = Serial.parseInt();
    if (Serial.read() == '\n') {
      digitalWrite(3, onOff);
      int sensor = analogRead(A0);
      delay(1);
      Serial.println(sensor);
    }
  }
}
//processing exercise #3

import processing.serial.*;
Serial myPort;

PVector velocity;
PVector gravity;
PVector position;
PVector acceleration;
PVector wind;
float drag = 0.99;
float mass = 50;
float hDampening;
boolean onOff=false;
float analog;

void setup() {
  size(640, 360);
  String portname=Serial.list()[3];
  println(portname);
  myPort = new Serial(this, portname, 9600);
  myPort.clear();
  myPort.bufferUntil('\n');

  noFill();
  position = new PVector(width/2, 0);
  velocity = new PVector(0, 0);
  acceleration = new PVector(0, 0);
  gravity = new PVector(0, 0.5*mass);
  wind = new PVector(0, 0);
  hDampening=map(mass, 15, 80, .98, .96);
}
void draw() {
  background(255);
  if (!keyPressed) {
    wind.x= analog;
    velocity.x*=hDampening;
  }
  applyForce(wind);
  applyForce(gravity);
  velocity.add(acceleration);
  velocity.mult(drag);
  position.add(velocity);
  acceleration.mult(0);
  ellipse(position.x, position.y, mass, mass);
  if (position.y > height-mass/2) {
    velocity.y *= -0.9;  // A little dampening when hitting the bottom
    position.y = height-mass/2;
  }
  
  if (position.y >= height - mass){
  onOff = true;
  }
  else{
  onOff = false;
  }
}

void serialEvent(Serial myPort) {
  String s=myPort.readStringUntil('\n');
  s=trim(s);
  int values = parseInt(s);
  if (s!=null) {
    analog =map(values, 0, 1023, -3, 3);
  }
  myPort.write(int(onOff)+"\n");
}

void applyForce(PVector force) {
  // Newton's 2nd law: F = M * A
  // or A = F / M
  PVector f = PVector.div(force, mass);
  acceleration.add(f);
}

void keyPressed() {
  if (key==' ') {
    mass=random(15, 80);
    position.y=-mass;
    velocity.mult(0);
  }
}

Video

Musical Instrument: Shanaia Paruthi & Fatima Nadeem

Description 

In pairs, make a musical instrument

  • You must use at least one digital sensor (switch)
  • You must use at least one analog sensor (photoresistor, potentiometer, or distance measuring sensor)

Process: 

What if you could play music without even touching the instrument!

So our device uses a photoresistor to detect the amount of light around the system and uses that light to give different frequencies out of the buzzer to create a musical tone with some LED effect.

We started off with building a circuit for the photoresistor to detect the amount of light around the system. Then we divided that value into 5 different intervals for the LED. in each interval, a single led lights up indicating  the note. We did the same  with the buzzer.  In the end, we added a switch to keep the device from switching on/off. Here’s a picture of our system:

So the current in the system first goes to the photoresistor to detect the light, which is mapped for the buzzer frequency (note), the led lights, and the duration of the notes. Which ultimately gets displayed as below:

VIDEO:

CODE:

#include "pitches.h"

//creating an array for the noted used
int notes[10] = {NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_E5};

//declaring the ports 
const int button = 13;
int const red1 = 2;
int const yellow = 7;
int const blue = 4;
int const green = 8;
int const red2 = 12;
const int tonepin = 5;
const int photoresistor = A0;

//creating an array for the lights with respect to leds
int lights[5] = {2, 4, 7, 8, 12};

//declaring the original state
bool lastButtonState = LOW;
bool onoff = LOW;


void setup() {

  //starting the sensors 
  Serial.begin(9600);
 
  //declaring the function of the port
  pinMode(red1, OUTPUT);
  pinMode(blue, OUTPUT);
  pinMode(red2, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(yellow, OUTPUT);
  pinMode(tonepin, OUTPUT);
  pinMode(button, INPUT);

}

void loop() {

  bool buttonState = digitalRead(button);//reaing the state
  Serial.println(buttonState);

  //condition for button
  if (buttonState != lastButtonState) {
    if (buttonState == LOW) {
      onoff = (onoff == HIGH) ? LOW: HIGH;
    }
  }
  lastButtonState = buttonState;
  if(onoff==HIGH){
    for (int light = 0; light < 5; light++) { //loop for lights 
      digitalWrite(lights[light], LOW);
    }
    int value = analogRead(photoresistor); //reading photoresistor
 
    //mapping the values
    int mappedValue = map(value, 0, 900, 100, 150);
    int mappedValue2 = map(value, 0, 900, 0, 9);
    int mappedValue3 = map(value, 0, 900, 0, 4);

    //condition for the buzzer
    if (millis() % mappedValue == 0) {
      tone(tonepin, notes[mappedValue2], mappedValue);
    }
    digitalWrite(lights[mappedValue3], HIGH);
  }
  
}



////////////////////////////////////////////////////


/*************************************************

 * Public Constants

 *************************************************/

#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978

 

Gopika and Leo’s Arduino Bagpipes

For this week’s assignment, we started off with wanting to do something with the distance sensors. There’s something really cool about moving your hands in thin air and creating music. 

 We wanted to use the distance sensor to record the distance and convert that into musical notes and have lasers put in just for the aesthetics and as a guide for the player to know where to move. We took inspiration from the laser keyboard/bass featured in this video.

We soon realized that lasers were too complicated to integrate into the instrument as we would need some kind of smoke in the air to trace the path of the beam and that was a huge, complicated process to replicate on campus. So, we decided to work with just air.

For the instrument, we decided to have a one-octave scale and a button that would take digital input, which would shift the scale by an octave. We replicated the same thing with another Arduino so that we could play in harmony with different octaves. There’s also a button that acts like an on/off switch in the beginning and once the instrument is switched on, it acts as a momentary pause. 

We had a few issues with balancing the tone() function from both the digital input of the push button and the input from the distance sensor, but it was some wiring issues, once that was sorted out, all was done.

We added a few lines of codes that would convert distances beyond a certain point to 0 so that we don’t hear any annoying noise when there is no object in front of the instrument.

Interestingly, what we achieved, in the end, sounded a lot like bagpipes.

Here are a few demos. The first one is just using one octave and the second one is by shifting octaves midway. It’s ‘The Lion Sleeps Tonight’:

Here’s the code:

#define echoPinC 12 
#define trigPinC 11 
const int BUTTON_PIN = 7; // Arduino pin connected to button's pin
const int BUTTON_PIN2 = 8;
# include "pitches.h"

String notes6[7] = {"C4", "D4", "E4", "F4", "G4", "A4", "B4"};

int notes[7] = {NOTE_C5, NOTE_D5, NOTE_E5, NOTE_F5, NOTE_G5, NOTE_A5, NOTE_B5};
int notesHigh[7] = {NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4};

long durationC;
int distanceC;

int playNote;

bool start = false;

void setup() {
  pinMode(trigPinC, OUTPUT);
  pinMode(echoPinC, INPUT);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(BUTTON_PIN2, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {
  
   int buttonState = digitalRead(BUTTON_PIN); // read new state
   int offState = digitalRead(BUTTON_PIN2); 

  //switch on 
   if (offState == LOW) {
    start=true;
   }

  if(start)
  {
  
    // put your main code here, to run repeatedly:
  digitalWrite(trigPinC, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPinC, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPinC, LOW);
    durationC = pulseIn(echoPinC, HIGH);
    distanceC = durationC * 0.034 / 2; 
  
    Serial.print("DistanceC: ");
    Serial.print(distanceC);
    Serial.print(" cm");
    Serial.print("mapped: ");
  
    int constrainedInput=constrain(distanceC, 1, 70);
    
    int mappedNoteC = map(constrainedInput, 1, 70, 0, 6);
  
  Serial.println(notes6[mappedNoteC]);
  
    if (buttonState == LOW) {
     Serial.print("button unpressed: ");
  
    
    // noTone(4);
    playNote = notesHigh[mappedNoteC];
    
    if(distanceC > 70){
     noTone(4);
    } 
    
    }
    else
    if (buttonState == HIGH) {
     Serial.print("button pressed: ");
        playNote = notes[mappedNoteC];
    }

  //no note when button is triggered
    if (offState == LOW) {
       Serial.print("button unpressed: ");
       noTone(4);
    }
    else

    //when the button is not triggered play a note
      if (offState == HIGH) { 
        tone(4, playNote);
      }

      //distances beyonf a point has no tone
      if(distanceC > 70){
        noTone(4);
      }

}

}

 

[Badr & Ziyad] Week 10: Music Instrument

For this week’s assignment, Badr and I tried to build a simple musical instrument using a servo to emulate a simple drum and a buzzer to play a melody.

Drum: 

We defined three different drum modes (beats) that can be looped over using a pushbutton. Every time the button is clicked, the drum moves to the next mode. Originally, we planned to use two servos (drums), but it turned out noisier than we thought, so we decided to use only one. To move the servos, we have used servo.write().

Millis():

To avoid using delay() which will affect the interaction, we have used Millis to set breaks. For that, we had to create an array to store the duration of the breaks, variables to store the elapsed time, and if conditions. 

unsigned long curr = millis(); // store the elapsed time
bool played[] = {1, 0, 0, 0}; // boolean variables to control the modes
int period[]= {200, 150, 150, 200}; // Tempo of the servo (breaks)
int curr_prd=0; // keep track of the current break

Beats (Modes): 

To alternate between the modes, we have used if/else conditions, boolean variables, digitalRead() to read the state of the push button and a prevButton variable to store the previous state.

// switch between the modes
  // default mode
  if (!prevbutton && digitalRead(3)==HIGH && level2==0 && level3==0){
    level2=1;
    period[0] = 200; period[1] = 150; period[2] = 150; period[3] = 200;
  }
  // mode 3
  else if (!prevbutton && digitalRead(3)==HIGH && level2==0 && level3==1){
    level3=0;
    period[0] = 300; period[1] = 300; period[2] = 300; period[3] = 300;
  }
  // mode 2
  else if (!prevbutton && digitalRead(3)==HIGH && level2==1 && level3==0){
    level3=1; level2=0;
    period[0] = 60; period[1] = 60; period[2] = 60; period[3] = 60;
  }

Moving the Servo:

// set the tempo for level 3
  if (curr-prev>=period[curr_prd] && level2==0 && level3==1){
    prev=curr;
    // change the array parameters
    if (played[0]==1){played[0]=0; played[1]=1;}
    else if (played[1]==1){played[1]=0; played[2]=1;}
    else if (played[2]==1){played[2]=0; played[3]=1;}
    else if (played[3]==1){played[3]=0; played[0]=1;}
    curr_prd=(curr_prd+1)%4;
  }
  // set the tempo for level 2
  if (curr-prev>=period[curr_prd] && level2==1 && level3==0){
    prev=curr;
    // change the array parameters
    if (played[0]==1){played[0]=0; played[1]=1;}
    else if (played[1]==1){played[1]=0; played[2]=1;}
    else if (played[2]==1){played[2]=0; played[3]=1;}
    else if (played[3]==1){played[3]=0; played[0]=1;}
    curr_prd=(curr_prd+1)%4;
  }
  // set the tempo for the default level
  if (curr-prev>=period[curr_prd] && level2==0 && level3==0){
    prev=curr;
    // change the array parameters
    if (played[0]==1){played[0]=0; played[1]=1;}
    else if (played[1]==1){played[1]=0; played[2]=1;}
    else if (played[2]==1){played[2]=0; played[3]=1;}
    else if (played[3]==1){played[3]=0; played[0]=1;}
    curr_prd=(curr_prd+1)%4;
  }
  // Move the servo
  if (played[0]){servo_r.write(90);}
  if (played[1]){servo_r.write(120);}
  if (played[2]){servo_r.write(100);}
  if (played[3]){servo_r.write(120);}

Buzzer: 

We chose to play the tetris theme on the buzzer, and used the ultrasonic sensor to change the speed at which the melody is being played. The larger the values read by the sensor are, the slower the music will play. 

View post on imgur.com

Melody:

To play the melody, we stored the convenient notes in an array (we got them online), then proceeded to calculate how long the note is using sizeof() and the tempo which is 160.

int tempo = 160; // Tempo of the song // Melody notes 
int melody[] = { NOTE_E5, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_C5, NOTE_B4, NOTE_A4, NOTE_A4, NOTE_C5, NOTE_E5, NOTE_D5, NOTE_C5, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_E5, NOTE_C5, NOTE_A4, NOTE_A4, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_F5, NOTE_A5, NOTE_G5, NOTE_F5, NOTE_E5, NOTE_C5, NOTE_E5, NOTE_D5, NOTE_C5, NOTE_B4, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_E5, NOTE_C5, NOTE_A4, NOTE_A4, REST, NOTE_E5, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_C5, NOTE_B4, NOTE_A4, NOTE_A4, NOTE_C5, NOTE_E5, NOTE_D5, NOTE_C5, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_E5, NOTE_C5, NOTE_A4, NOTE_A4, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_F5, NOTE_A5, NOTE_G5, NOTE_F5, NOTE_E5, NOTE_C5, NOTE_E5, NOTE_D5, NOTE_C5, NOTE_B4, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_E5, NOTE_C5, NOTE_A4, NOTE_A4, REST, NOTE_E5, NOTE_C5, NOTE_D5, NOTE_B4, NOTE_C5, NOTE_A4, NOTE_GS4, NOTE_B4, REST, NOTE_E5, NOTE_C5, NOTE_D5, NOTE_B4, NOTE_C5, NOTE_E5, NOTE_A5, NOTE_GS5 };

// Calculate the duration of each note
int notes= sizeof(melody) / sizeof(melody[0]) / 2; 
int note = (60000 * 4) / tempo;
int divider = 0, duration = 0;

// Calculate the duration of each note
int notes= sizeof(melody) / sizeof(melody[0]) / 2; 
int note = (60000 * 4) / tempo;
int divider = 0, duration = 0;

Ultrasonic Sensor:

Depending on the distance measured by the sensor, the speed of the song is changing. For that, we used map() to modify it within a certain range. We also had to convert microseconds to cm.

// get the distance using the ultrasonic sensor
  long duration, cm;
  digitalWrite(9, LOW);
  delayMicroseconds(2);
  digitalWrite(9, HIGH);
  delayMicroseconds(10);
  digitalWrite(9, LOW);
  duration = pulseIn(8, HIGH);
  // convert microseconds to cm
  cm = duration/29/2;
  if (cm>20) {
   cm = 20;
  }
  // map the distance form 80 to 150 to fit 
  cm = map(cm,0,20,80,150);

Challenges: 

The biggest challenge we faced was getting the drum to work without using delay(). Using delay() would have affected the interaction with the button and sensor, so we used millis() instead. When we tried to implement the millis() solution, the drum’s playing started to become irregular and sometimes stopped working after some time. To fix this, we used arrays to keep track of the angles and timings of each angle change of the servo. The code started working properly after we introduced this fix and restructured the logic we used in the loop.

We also faced some issues with taping of our servo and stick coming loose occasionally while working on the project.

Code: 

#include <Servo.h>
#include "notes.h"

Servo servo_r;
unsigned long prev=0; // variable to store the time
bool played[] = {1, 0, 0, 0}; // boolean variables to control the modes
int period[]= {200, 150, 150, 200}; // Tempo of the servo (breaks)
int curr_prd=0; // keep track of the current break
bool level2=0; // Mode 2
bool level3=0; // Mode 3
bool prevbutton=0; // store the previous state of the pushbutton
int prevnote = 0; // store the prev note
int currnote = 0; // store the current note
int tempo = 160; // Tempo of the song
// Melody notes
int melody[] = {
  NOTE_E5, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_C5, NOTE_B4,
  NOTE_A4, NOTE_A4, NOTE_C5, NOTE_E5, NOTE_D5, NOTE_C5,
  NOTE_B4, NOTE_C5, NOTE_D5, NOTE_E5, NOTE_C5, NOTE_A4,
  NOTE_A4, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_F5,
  NOTE_A5, NOTE_G5, NOTE_F5, NOTE_E5, NOTE_C5, NOTE_E5,
  NOTE_D5, NOTE_C5, NOTE_B4, NOTE_B4, NOTE_C5, NOTE_D5,
  NOTE_E5, NOTE_C5, NOTE_A4, NOTE_A4, REST, NOTE_E5, 
  NOTE_B4, NOTE_C5, NOTE_D5, NOTE_C5, NOTE_B4, NOTE_A4,
  NOTE_A4, NOTE_C5, NOTE_E5, NOTE_D5, NOTE_C5, NOTE_B4,
  NOTE_C5, NOTE_D5, NOTE_E5, NOTE_C5, NOTE_A4, NOTE_A4,
  NOTE_A4, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_F5, NOTE_A5,
  NOTE_G5, NOTE_F5, NOTE_E5, NOTE_C5, NOTE_E5, NOTE_D5,
  NOTE_C5, NOTE_B4, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_E5,
  NOTE_C5, NOTE_A4, NOTE_A4, REST, NOTE_E5, NOTE_C5,
  NOTE_D5, NOTE_B4, NOTE_C5, NOTE_A4, NOTE_GS4, NOTE_B4,
  REST, NOTE_E5, NOTE_C5, NOTE_D5, NOTE_B4, NOTE_C5, NOTE_E5,
  NOTE_A5, NOTE_GS5
};
// Calculate the duration of each note
int notes= sizeof(melody) / sizeof(melody[0]) / 2; 
int note = (60000 * 4) / tempo;
int divider = 0, duration = 0;


void setup() {
  Serial.begin(9600);
  servo_r.attach(6); // Set the servo
  pinMode(3, INPUT); // Set the button pinmode
  pinMode(9, OUTPUT); // set the echo pin for the sensor
  pinMode(8, INPUT); // set the pin for the sensor
}

void loop() {
  // switch between the modes
  // default mode
  if (!prevbutton && digitalRead(3)==HIGH && level2==0 && level3==0){
    level2=1;
    period[0] = 200; period[1] = 150; period[2] = 150; period[3] = 200;
  }
  // mode 3
  else if (!prevbutton && digitalRead(3)==HIGH && level2==0 && level3==1){
    level3=0;
    period[0] = 300; period[1] = 300; period[2] = 300; period[3] = 300;
  }
  // mode 2
  else if (!prevbutton && digitalRead(3)==HIGH && level2==1 && level3==0){
    level3=1; level2=0;
    period[0] = 60; period[1] = 60; period[2] = 60; period[3] = 60;
  }

  unsigned long curr = millis(); // store the elapsed time

  // set the tempo for level 3
  if (curr-prev>=period[curr_prd] && level2==0 && level3==1){
    prev=curr;
    // change the array parameters
    if (played[0]==1){played[0]=0; played[1]=1;}
    else if (played[1]==1){played[1]=0; played[2]=1;}
    else if (played[2]==1){played[2]=0; played[3]=1;}
    else if (played[3]==1){played[3]=0; played[0]=1;}
    curr_prd=(curr_prd+1)%4;
  }
  // set the tempo for level 2
  if (curr-prev>=period[curr_prd] && level2==1 && level3==0){
    prev=curr;
    // change the array parameters
    if (played[0]==1){played[0]=0; played[1]=1;}
    else if (played[1]==1){played[1]=0; played[2]=1;}
    else if (played[2]==1){played[2]=0; played[3]=1;}
    else if (played[3]==1){played[3]=0; played[0]=1;}
    curr_prd=(curr_prd+1)%4;
  }
  // set the tempo for the default level
  if (curr-prev>=period[curr_prd] && level2==0 && level3==0){
    prev=curr;
    // change the array parameters
    if (played[0]==1){played[0]=0; played[1]=1;}
    else if (played[1]==1){played[1]=0; played[2]=1;}
    else if (played[2]==1){played[2]=0; played[3]=1;}
    else if (played[3]==1){played[3]=0; played[0]=1;}
    curr_prd=(curr_prd+1)%4;
  }
  // Move the servo
  if (played[0]){servo_r.write(90);}
  if (played[1]){servo_r.write(120);}
  if (played[2]){servo_r.write(100);}
  if (played[3]){servo_r.write(120);}

  // read the state of the button
  prevbutton = digitalRead(3);

  // get the distance using the ultrasonic senor
  long duration, cm;
  digitalWrite(9, LOW);
  delayMicroseconds(2);
  digitalWrite(9, HIGH);
  delayMicroseconds(10);
  digitalWrite(9, LOW);
  duration = pulseIn(8, HIGH);
  // convert microseconds to cm
  cm = duration/29/2;
  if (cm>20) {
   cm = 20;
  }
  // map the distance form 80 to 150 to fit 
  cm = map(cm,0,20,80,150);

  if (curr-prevnote >= cm) {
    prevnote = curr;
    // play the note
    tone(11, melody[currnote], duration*0.9);
    // loop over the array
    currnote = (currnote + 1) % 100;
  }
}

Leo and Gopika Make Bagpipes

Inspiration:

Gopika was inspired by this  video of a Coldplay performance, in which it appears like the band is playing music on lasers in the air. We decided we would try to make an “air guitar” in a similar style. We would use six ultrasonic distance sensors that acted like “strings” and buttons that acted like the frets on a guitar. However because ultrasonic distance sensors need a clock, and each Arduino board only has three, we were limited to how many distance sensors we could use. We ended up with two distance sensors, each controlled by two buttons.

Process:

We first built a circuit with the ultrasonic distance sensor to see what kind of ranges were measurable. Next, we added a buzzer to the circuit, and wrote code to play a note whenever a given distance was triggered. For example, between 0 and 60 cm, every 10 cm corresponds to one note.

We added control buttons to the circuit next, one of which turns the system on (starts detecting distance) and the other changes the octave. Each code had two arrays of notes, each a full octave, giving the buzzer access to two full octaves of notes. If the button is pressed, the higher octave is played, if the button is not pressed, the lower octave is played.

Challenges:

We had a lot of trouble wiring the buttons into the circuit. At first, they didn’t work at all, and pressing the button wasn’t even being detected. Next, the press would be detected, but it would lag the system so that no tone was played for a few seconds. Finally Gopika realized that we had wired the circuit incorrectly and the buttons finally worked.

Final Work:

See if you recognize the song:

And with the octave change:

Main Code:

#define echoPinC 12 
#define trigPinC 11 
const int BUTTON_PIN = 7; // Arduino pin connected to button's pin
const int BUTTON_PIN2 = 8;
# include "pitches.h"

String notes6[7] = {"C4", "D4", "E4", "F4", "G4", "A4", "B4"};

int notes[7] = {NOTE_C6, NOTE_D6, NOTE_E6, NOTE_F6, NOTE_G6, NOTE_A6, NOTE_B6};
int notesHigh[7] = {NOTE_C7, NOTE_D7, NOTE_E7, NOTE_F7, NOTE_G7, NOTE_A7, NOTE_B7};

long durationC;
int distanceC;

int playNote;

bool start = false;

void setup() {
  // put your setup code here, to run once:

  pinMode(trigPinC, OUTPUT);
  pinMode(echoPinC, INPUT);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(BUTTON_PIN2, INPUT_PULLUP);

  Serial.begin(9600);
}

void loop() {
   int buttonState = digitalRead(BUTTON_PIN); // read new state
int offState = digitalRead(BUTTON_PIN2); 

 if (offState == LOW) {
  start=true;
 }

if(start)
{

  // put your main code here, to run repeatedly:
digitalWrite(trigPinC, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPinC, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPinC, LOW);
  durationC = pulseIn(echoPinC, HIGH);
  distanceC = durationC * 0.034 / 2; 

  Serial.print("DistanceC: ");
  Serial.print(distanceC);
  Serial.print(" cm");
  Serial.print("mapped: ");

  int constrainedInput=constrain(distanceC, 1, 70);
  
  int mappedNoteC = map(constrainedInput, 1, 70, 0, 6);

Serial.println(notes6[mappedNoteC]);

  if (buttonState == LOW) {
   Serial.print("button unpressed: "); 
  // noTone(4);
  playNote = notesHigh[mappedNoteC];
  
  if(distanceC > 70){
   noTone(4);
  } 
  
  }
  else
  if (buttonState == HIGH) {
   Serial.print("button pressed: ");
      playNote = notes[mappedNoteC];
  }
if (offState == LOW) {
   Serial.print("button unpressed: ");
  noTone(4);
}
 else
  if (offState == HIGH) {
tone(4, playNote);
  }
  if(distanceC > 70){
    noTone(4);
  }
}
}
#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978

 

Rhythm & Eric : Melodic Piano

Description

 Rhythm and Eric are to make a musical instrument using at least one digital sensor(switch), and at least one analog sensor (photoresistor, potentiometer, or distance measuring sensor)

Idea

The piano is usually used to play two parts simultaneously. One musician plays all the chords and melody.  Chords and melodies both work together to create rhythm and harmony. A chord is when three or more notes are played together at the same time whilst melody is when single notes are played individually in a pattern or sequence.

Then comes the Eureka moment! We can have the melody which is 8 switches holding the single notes (do re me fa so la ti do)  and a chord which is a photoresistor that plays a melody using three or more notes.

Process

So the setup was done as described above, we had 8 switches connected in a row, a buzzer for sound, and a photoresistor. Each note above was connected to one switch. Data from the photoresistor was mapped to play a chord.

Final Work

 

Challenges

Our initial plan was to use 8 photoresistors and a switch but couldn’t figure how to connect the 8 photoresistors to Arduino. We experimented with the distance sensor as well but were getting a weird noise from the buzzer, we rechecked the connection and code but still couldn’t get it to work. We also had in mind to include drums but couldn’t get the buzzer to produce drum notes.

Code

-Main

#include "pitches.h"
int knobPin = A0;

int melody[] = {NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_E5};
int noteDurations[] = {4, 8, 8, 4, 4, 4, 8, 4, 8, 4};

int duration = 100

// notes and their respective buttons
const int Dobbutton = 2;
const int Rebutton = 3;
const int Mibutton = 4;
const int Fabutton = 5;
const int Solbutton = 6;
const int Labutton = 7;
const int Tibutton = 8;
const int Dotbutton = 9;

// initial status of buttons
int DobButtonStatus = 0;
int ReButtonStatus = 0;
int MiButtonStatus = 0;
int FaButtonStatus = 0;
int SolButtonStatus = 0;
int LaButtonStatus = 0;
int TiButtonStatus = 0;
int DotButtonStatus = 0;


void setup()

{

  Serial.begin(9600);
  pinMode(knobPin, INPUT);


  pinMode(Dobbutton, INPUT);
  pinMode(Rebutton, INPUT);
  pinMode(Mibutton , INPUT);
  pinMode(Fabutton, INPUT);
  pinMode(Solbutton, INPUT);
  pinMode(Labutton, INPUT);
  pinMode(Tibutton , INPUT);
  pinMode(Dotbutton, INPUT);
}

void loop() {
  int ldrStatus = analogRead(knobPin);
  int mappedValue = map(ldrStatus, 0, 1023, 0, 255);
//  Serial.println(mappedValue);


  // check for photoresistor value and play chord
  
  if (mappedValue > 200 ) {
    for (int i = 0; i < 10; i++) {
      int noteDuration = 1000 / noteDurations[i];
      tone(10, melody[i], noteDuration);
      int pauseBetweenNotes = noteDuration * 1.30;
      delay(pauseBetweenNotes);
      noTone(10);

    }

  }

  
  //read state of buttons
  DobButtonStatus = digitalRead(Dobbutton);
  ReButtonStatus = digitalRead(Rebutton);
  buttonMistatus = digitalRead(Mibutton);
  FaButtonStatus = digitalRead(Fabutton);
  SolButtonStatus = digitalRead(Solbutton);
  LaButtonStatus = digitalRead(Labutton);
  TiButtonStatus = digitalRead(Tibutton);
  DotButtonStatus = digitalRead(Dotbutton);
  
  
  if (DobButtonStatus == HIGH) {
    tone(10, dot, duration); 
  }
  if (ReButtonStatus == HIGH) {
    tone(10, re, duration);
  }
  if (buttonMistatus == HIGH) {
    tone(10, mi, duration);
  }
  if (FaButtonStatus == HIGH) {
    tone(10, fa, duration);
  }
  if (SolButtonStatus == HIGH) {
    tone(10, sol, duration);
  }
  if (LaButtonStatus == HIGH) {
    tone(10, la, duration);
  }
  if (TiButtonStatus == HIGH) {
    tone(10, si, duration);
  }
  if (DotButtonStatus == HIGH) {
    tone(10, dot, duration);
  }


}




– Pitches.h

#define dot  264 //first do
#define re   297
#define mi   330
#define fa   352
#define sol  396
#define la   440
#define si   495
#define dot  528// last do



#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  80
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435

 

Electronic Drums: Fatema Nassar &Ingy

General Idea:

After a lot of thinking and research we decided we wanted to try something and we honestly didn’t know if it would work or not. Our initial Idea was to created some sort of drum set. We felt like it would be a cool challenge to take on! The idea is to have 6 pads attached to a board, and each pad creates a different sound or tone when you press on it.

First attempt:

In our first attempt we started trying to create the “drums” using piezo buzzers. We assembled together a foam board with 6 pads, each with an piezo buzzer attached to it. We think we got a bit to courageous and we put together the whole instrument and it did not end up fully working. We believe the conceptualization and idea could work (it did work to a certain extent).

We think the reason why it didn’t work is because we wanted each pad to create a sound when we press on it. However, we believe the vibrations created by pressing on one of the pads to create the sound was also picked up by the other pads which made the buzzer go crazy. The serial monitor is what helped us get to that conclusion. We tried to fix it however, we did not have enough time to do so. Hence, we decided to switch it up a bit.

The piezo buzzers we had to remove 🙁

Attempt 2 (This one actually worked :))

We met again, and decided to use force sensitive resistors instead. We reassembled the board and substituted the piezo buzzer with force sensitive resistors, we also added a layer of sponge in the middle between the board and the separate tabs. Each pad has a different tune when pressed on.

We then decided to use a button as our digital read, as a switch for Led lights that we added. The Leds only light up when you press on the pads. So the button switches the LEDS on and off but they only light up when we press on the tabs, if we press on the tabs when the button is off the LEDS won’t light up we we press on the tabs.

This video shows how the button is used:

Final touches:

Just to add some visual appeal to the board we decided to print out pictures off different drums to complete a drum set and stick them on each pad. Even thought the tunes don’t really sound like drums lol 🙂

Finally this our result!

#include "pitches.h"

const int inputPins[] = { A0, A1, 2, A3, A4, A5};
int inputs[6];
int threshold[] = {250, 900, 940, 960, 900, 750};
int leds[] = {2, 3, 4, 5, 6, 7};
bool flag = HIGH;
bool prev, curr;
const int buzzerPin = 13, button = 12;
long timer[6];

int notes[10] = {NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_E5};

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  for (int i = 0; i < sizeof(inputPins); i++) {
    pinMode(inputPins[i], INPUT);
  }
  pinMode(button, INPUT);
  for (int i = 0; i < sizeof(leds); i++) {
    pinMode(leds[i], OUTPUT);
  }
  pinMode(buzzerPin, OUTPUT);

}

void loop() {
  if (curr == 1 && curr != prev) {
    flag = !flag;
    Serial.println("flag");
  }
  curr = digitalRead(button);
  for (int i = 1; i < 6; i++) {
    inputs[i] = analogRead(inputPins[i]);

    Serial.print(i + 1);
    Serial.print(" : ");

    Serial.print(inputs[i]);
    Serial.print(" ");

    if (inputs[i] > threshold[i]) {
      tone(buzzerPin, notes[i * 2], 500);
      if (flag) {
        digitalWrite(leds[i], HIGH);
      }
      else {
        digitalWrite(leds[i], LOW);
      }

    }
    else {
      digitalWrite(leds[i], LOW);
    }


  }
  Serial.println("");

  for (int j = 0; j < 6; j++) {

  }
  prev = digitalRead(button);
}