Musical Instrument

Concept

In this assignment, we were to create musical instruments but in groups. I could not find someone to work with so i decided to work on my own. One other reason was that i wanted to also have time to explore and learn more. My musical instrument is a basic one which uses two external libraries that are adapted for the ultrasonic sensor and the piezo buzzer. The libraries are the NewPing.h and the ToneAC.h library. The NewPing.h library is adapted to work with all types of ultrasonic sensors and increases the accuracy of the distance calculated. The ToneAC library has a higher frequency range, twice the volume and higher quality than the tone library. By combining these two libraries my instrument improved a lot more and had better quality sounds.

#include <NewPing.h> // newping library for the ultrasonic sensor
#include <toneAC.h> // tine library for the piezo buzzer

//pins of the ultrasonic sensor
const int trig=12; 
const int echo=11;

NewPing sonar( trig, echo, 35 ); // creating a new class for the ultrasonic sensor

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600); // for debugging and printing the values stored
}



void loop() {
  // put your main code here, to run repeatedly:
  //calculate the distance to the obstacle
int f= sonar.ping_cm();
Serial.print("freq");
Serial.println(f);


//map the distance calculated to the notes c1 to e7
int frequency =map(f, 0, 35, 33, 4000);

//play the sound on the buzzer
toneAC(frequency, 10);

delay(200);
}

 

Week 10 – Light Theremin (Stefan and Arslan)

Concept:

For this assignment, we wanted to create a version “Theremin” that will be linked with the light sensor and will produce sound based on the light intensity received by the sensor on Arduino board. Additionally, we’ll add the switch to this Light Theremin to change the frequency of the sound and to play different sounds using the light sensor. The frequency only changes while the button is pressed, we did this to change tune while playing the theremin which makes it easier for the user to play different tone from both frequencies. In short, the analog in takes values from the light sensor and these values are converted within a certain limit to be played through the buzzer. When the button is pressed, the limit of these values changes and thus giving us the tone of higher frequency.

SCHEMATIC:
Video:

Code:

// create variable to hold sensor value
int sensorValue;
// UNO's analog read sensors use "analog to digital converter" (ADC),
// which means it divides the reading coming from the sensor (0 to 5 volts) into 1024 equal steps.
// Thus, the reading from our photoresistor could range from 0 to 1023,
// however it is unlikely to use the full range so we will calibrate it.
// For now, create variables to hold the high and low value for our photoresistor range,
// make them the opposite of the max/min values and we will calibrate them down.
int sensorLow = 1023;
int sensorHigh = 0;
// set the LED pin, 13 is the built in LED
const int ledPin = 13;
int switchFreq = 4;
int switchVal;


void setup() {
 
 
  pinMode(ledPin, OUTPUT);
  pinMode(switchFreq, INPUT);
 
  digitalWrite(ledPin, HIGH);
 
  delay(500);
 
  digitalWrite(ledPin, LOW);
  delay(500);
  digitalWrite(ledPin, HIGH);
  delay(500);  
  digitalWrite(ledPin, LOW);
  delay(500);
  digitalWrite(ledPin, HIGH);

  // after it blinks for 2 seconds, its time to calibrate the photoresistor readings
  // create a loop with while() that lasts for 5000 ms to calibrate the light sensor according the setting,
  while (millis() < 5000) {
    // use analogRead() to get a reading from the photoresistor,
    sensorValue = analogRead(A0);
    // create two conditions using if() to calibrate the max and min sensor values,
    // raise the sensorHigh if the current reading is higher:
    if (sensorValue > sensorHigh) {
      sensorHigh = sensorValue;
    }
    // lower the sensorLow if the current reading is lower:
    if (sensorValue < sensorLow) {
      sensorLow = sensorValue;
    }
  }
 
  // turn the LED off, calibration is done!
  digitalWrite(ledPin, LOW);
}



void loop() {
  //read the input from A0 and store it in a variable
  sensorValue = analogRead(A0);
  switchVal = digitalRead(switchFreq);

  // map(currentValue, oldRange1, oldRange2, newRange1, newRange2)
  // I reverse their order (high, low) so that shading the photoresister will cause a higher tone.
  // the new range is the range of Hz to use to produce sound, which for UNO can be from 31 to 65535 Hz.
  // I set it from 0 to 300, which is a more normal sound range and if a button is pressed I change the values between 500 to 1000
  // since we start at 0, full light will make no tone, full shade will make the highest tone.
  if (switchVal == HIGH){
  int pitch = map(sensorValue, sensorHigh, sensorLow, 500, 1000);
  tone(8, pitch, 10);
  }
  else{
    int pitch = map(sensorValue, sensorHigh, sensorLow, 0, 300);
    tone(8, pitch, 10);
  }
  // use the tone function to create sound in the piezo,
  // tone(pin#, value, milliseconds)
 

  // add a delay to make the current tone sustained for a few ms to adjust the quality of the tone
  delay(15);
}

Future improvements:

For future, we want to use more than 2 limits of frequencies and we want to add instruments that could be switched using the button. After completing this project, we realised that playing sound using the light sensor is easier than any other sensor since it only requires basic movements of hands to block the light or allow the light to the sensor. Thus, in future, we’d like to add more instruments like electric guitar frequencies and allow users to switch between these instruments using the switch buttons.

 

Assignment 7 – BuzzBoard

BuzzBoard By Hassan and Majid

Concept

For our project, we aimed to create a unique musical instrument using distance tracking sensors and buttons. After brainstorming various ideas, we were inspired to design a piano-like instrument that could be played by two hands, one is distance tracked and the other is pressing the buttons. There are 7 notes and 3 different octaves that can be played. The notes are determined by the distance and the octave is based on which button is pressed.

implementation

We decided to use ultrasonic sensor for detecting the hand distance and buttons for controlling different notes of the piano. The Arduino was also connected to a piezo sensor for producing the piano sounds. Once the circuit assembly was complete, we tested the circuit to ensure the sensors and buttons were working and registered properly. Then we programmed the buttons to play different octaves of the notes.

Deciding which note to be played is at the core of our code. We did so through making a list of if conditions. First we checked what was the last button pressed, to decide the octave we are playing the notes in. After that, based on the distance from the ultrasonic sensor, in increments of 3cm, we chose a note to be played. For example, here is how the notes are played if the last button pressed was 2:

  if(lastButton == 2){
  if (distance < 3) {
    tone(8,NOTE_A4,noteDuration);
  } else if (distance >= 3 && distance < 6) {
    tone(8,NOTE_B4,noteDuration);
  } else if (distance >= 6 && distance < 9) {
    tone(8,NOTE_C4,noteDuration);
  } else if (distance >= 9 && distance < 12) {
    tone(8,NOTE_D4,noteDuration);
  }
  else if (distance >= 12 && distance < 15) {
    tone(8,NOTE_E4,noteDuration);
  }
      else if (distance >= 15 && distance < 18) {
    tone(8,NOTE_F4,noteDuration);
  }
      else if (distance >= 18 && distance < 21) {
    tone(8,NOTE_G4,noteDuration);
  }
}

Challenges

One of the main challenges we encountered during the project was calibrating the distance sensors to play different notes. We had to experiment with different threshold values and distances for the different notes and octaves.

The Demo

Week 9- SWITCHES AND LED LIGHTS

Concept

For this assignment, I had two LEDs which would be controlled by two switches. When the switches are pressed either individually or together, the LEDs are turned on in different ways.  First, pressing the swiitches one by one would turn on their corresponding LEDs and when they are pressed together, the LEDs light up, blinking in a pattern.

Diagram

CODE

void setup() {
  pinMode(8, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(A2, INPUT);
  pinMode(10, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(A3, INPUT);
}

void loop() {

  int switchPosition = digitalRead(A2);
  int switch2Position = digitalRead(A3);

  if (switchPosition == HIGH) { 
    digitalWrite(8, HIGH);   // turn the LED on (HIGH is the voltage level)
    digitalWrite(13, LOW);
  } else  {
    digitalWrite(8, LOW);    // turn the LED off by making the voltage LOW
    digitalWrite(13, HIGH);
  }
 if (switch2Position == HIGH) {
    digitalWrite(10, HIGH);   // turn the LED on (HIGH is the voltage level)
    digitalWrite(13, LOW);
  } else  {
    digitalWrite(10, LOW);    // turn the LED off by making the voltage LOW
    digitalWrite(13, HIGH);
  }



  if (switchPosition == HIGH && switch2Position == HIGH) {
   digitalWrite(8, HIGH); // turn the LED on (HIGH is the voltage level)
   delay(200); // wait 0.2 seconds before turning off the LED 
   digitalWrite(8, LOW); // turn the LED off by making the voltage LOW
   delay(200); // wait 0.2 seconds before turning on the LED

   
  digitalWrite(10, LOW); // turn the LED off by making the voltage LOW
  delay(200); // wait 0.2 seconds before turning on the LED
  digitalWrite(10, HIGH); // turn the LED on (HIGH is the voltage level)
  delay(200); // wait 0.2 seconds before turning off the LED 
  
  }
 

}

VIDEO

Week 9: A – Blue, B – Yellow, C – Green, D – Red

Idea:

In high school, I remember having O-level exams with 40 Multiple Choice Questions and each MCQ had 4 options to choose from. In class, everyone was supposed to work on their paper for a set amount of time after which the teacher would one by one say the correct answer out loud. The problem started when in a class of 30 energetic students the teacher’s voice was reduced to a mere muffle. The other issue was the confusion between the option ‘B’ and ‘D’ which sounds so similar that a clarification of ‘B for Ball’ and ‘D for Dog’ was always followed. This gave me the idea to make a 4 LED structure that can be used to communicate the correct answer for each MCQ one by one. The teacher can click the corresponding number of times and display the correct option and then follow it with an explanation of the respective MCQ.

Code:

int buttonPin = 7;
int LED1 = 13;
int LED2 = 12;
int LED3 = 11;
int LED4 = 10;
int initial = 0;
int lastState = 0;
int buttonState = LOW;


void setup()
 {
  // put your setup code here, to run once:
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(7, INPUT);
}

void loop() 
{
  // put your main code here, to run repeatedly:
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH)
  {
    delay(500);
    buttonState = digitalRead(buttonPin);
     if (buttonState == LOW) //when pressed move to next led
     {
      initial = lastState + 1;
     }
  }
  else 
  {
    delay(100);
  }
  
    if(initial == 1) //first time blue is lit up
    {
      digitalWrite (13, HIGH);
      digitalWrite (12, LOW);
      digitalWrite (11, LOW);
      digitalWrite (10, LOW);
      lastState = 1;
    }
    else if(initial == 2) //second time yellow is lit up
    {
      digitalWrite (13, LOW);
      digitalWrite (12, HIGH);
      digitalWrite (11, LOW);
      digitalWrite (10, LOW);
      lastState = 2;
    }
   else if (initial == 3) //third time green is lit up
   {
      digitalWrite (13, LOW);
      digitalWrite (12, LOW);
      digitalWrite (11, HIGH);
      digitalWrite (10, LOW);
      lastState = 3;
   }
    else if (initial == 4) //fourth time red is lit up
    {
        digitalWrite (13, LOW);
        digitalWrite (12, LOW);
        digitalWrite (11, LOW);
        digitalWrite (10, HIGH);
        lastState = 4;
    }
    else //all LEDs off
    {
        digitalWrite (13, LOW);
        digitalWrite (12, LOW);
        digitalWrite (11, LOW);
        digitalWrite (10, LOW);
        lastState = 0;
    }

}

Video:

Analog Input/Output

Idea

While I really like sunlight or natural light filtering in through my window, if I am deeply focused on a task I often forget to turn the lights on when the sun has set and this has often lead to headaches and dryness in my eyes due to eye strain when working on my laptop in the dark. So I wanted to create an indicator using a light sensor and LED in which the LED starts to blink if the light in the room is very dim. The glaring red alarm LED can only be temporarily switched to a blue light when a button is pressed down because I often get lazy and don’t get up to turn on the lights. So the red light would continue to blink as long as lights are not turned on and it becomes brighter in the room.

Circuit

I created the following circuit for the light indicator. I connected the LDR with a pull down resistor of 10K Ω and in the same circuit added the red LED with its respective resistor of 330Ω. Then I connected the red LED and LDR with the blue LED through a button and following is the Arduino Uno code for the circuit:

const int BUTTON = 7; // the number of the pushbutton pin on the arduino board
int lastState = LOW; // the last state from the button
int currentState;    // the current reading from the button
const int LIGHT_SENSOR_PIN = A0; 
const int LED_PIN          = 3;  
const int LED_PIN_2          = 11;  
const int ANALOG_THRESHOLD = 500;
int Analog;

void setup() {
  Serial.begin(9600);
  pinMode(LED_PIN, OUTPUT); 

  pinMode(BUTTON, INPUT_PULLUP);
}

void loop() {
  Analog = analogRead(LIGHT_SENSOR_PIN); // read the input on LDR

  currentState = digitalRead(BUTTON); //read input on button 
  if(Analog < ANALOG_THRESHOLD){
    if (currentState==HIGH){
      digitalWrite(LED_PIN, HIGH);   // turn LED on 
      delay(500);                       // wait 
      digitalWrite(LED_PIN, LOW);    // turn LED off 
      delay(500);
    }
    else{
      digitalWrite(LED_PIN_2, HIGH);   // turn LED 
      delay(500);                       // wait 
      digitalWrite(LED_PIN_2, LOW);    // turn LED off 
      delay(500);
    }    
  
  }
  else{
    digitalWrite(LED_PIN, LOW);  // turn LED off
  }


  
}

 

Improvements

For future improvements I would want to add some sort of sound alarm to it as well so that I do not ignore the indicator at all because of the noise. I would also like to add a LED that starts blinking again after a set time period of the lights are not turned on in for example 5 minutes or something similar to this.

Light Crystals – Digital/Analog I/O

Concept

I really wanted to play around with ultrasonic sensors and RGB Led, so for this project, I built two glowing crystals that change brightness based on how close an object is and can change colors when a button is pressed.

Final Product

Process/Challenges

I started off by first playing around with ultrasonic sensors with a tutorial I found here. Once I had that working, I slowly added more parts starting with a blue LED, then the RGB LED, and finally the switch. I wanted to present the LED in a cute and pretty way (instead of it just being there) but had a hard time finding something I could use to cover up the LEDs (I was going for a tennis ball but was not able to get one in time). When I added the LEDs, I temporarily covered them up with caps of my lip liners and since I liked the way they looked like crystals from the side I decided to keep them.

The circuit
Circuit diagram

I have already built a very similar circuit in class so this project was challenging in terms of creativity rather than technicality. I spent a lot of time trying to find a way to present my circuit without over-complicating things, and I was able to achieve that through trial and error.

 

Week 9 – Light Switch and Dimmer

For this weeks homework I decided to continue my work from class and make use of the switch for the digital sensor and the light sensor for the analog one.

I decided to do quite a simple light attached to the switch, when the switch is on the light is on, when you are no longer pressing the switch, the light goes off.

Here is the simple code:

// read the input on digital pin 2:
int buttonState = digitalRead(pushButton);

// if switch is on, turn LED on
if(buttonState==1) {
  digitalWrite(8, HIGH);
}
// if switch is off, turn LED off
else {
  digitalWrite(8, LOW);
}

For the analog sensor, I wanted to do a light dimmer where the dimness corresponds to the dimness that the sensor catches. This required a bit of math and a few if statements, because it is hard to achieve very low values with a light sensor, and unless the values are very low, the LED is quite bright.

Here is the code for that below:

// read the input on analog pin A2:
float sensorValue = analogRead(A2);

// exponential value for analogLEDValue
int analogLEDValue = 25.5*pow(10,(sensorValue/950));

// some more tweaking to get a more drastic change in output
if(analogLEDValue<200) {
  analogLEDValue=analogLEDValue/2-50;
}
else if(analogLEDValue<150) {
  analogLEDValue=analogLEDValue/4-50;
}
// control so it does not go <0
if(analogLEDValue<0){
  analogLEDValue=0;
}

// output
analogWrite(6, analogLEDValue);

Here is the demo:

Assignment 6 – Watering System

The Concept

The circuit is meant to represent a form of smart plant watering system. The light sensor is meant to detect the level of hypothetical sunlight, and the switch is meant to be pressed when the hypothetical soil is dried out. When the switch is held down, there is a third LED that shows that the plants are being watered, and the brightness of this LED slowly fades out, representing that the plants have been sufficiently watered. The initial brightness of this LED is based on the reading from the light sensor, to portray that you will have to water less if the sunlight isn’t as strong. Also, if the switch isn’t held down until this LED completely fades out, it will stay on to show that more watering still needs to be down.

The circuit

The circuit consists of a light sensor hooked to the Yellow LED, and the brightness of this LED is mapped to the reading from the light sensor

if(lightValue<=400){brightness=0;}
else{brightness = map(lightValue, 400, 1023, 0, 255);}

I realized even if I fully covered the light sensor with my hand, the reading would still be around 350-400. Thus, instead of directly mapping the input from the sensor to the output for the LED brightness, I set a threshold at 400 and below, which means that at 400 I want the light to be off, as this is a negligible amount of light.

The circuit then consists of a switch hooked to the Green LED to represent whether or not the switch is held down. This LED is digital and simply turns on and off.

There is also a third Blue LED, and this is an analog LED that is impacted by both the light sensor and the switch. The LED’s brightness is determined by the variable waterLevel, and this waterLevel is slowly decremented while the switch is pressed down.

if(switchValue==1)
{
  if(waterLevel>0){
    waterLevel --; 
  }
  analogWrite(waterLEDPin, waterLevel);

The initial value of waterLevel is set by the reading of the light sensor. Therefore, both sensors contribute to the behavior of this Blue LED.

The Demo

Unusual Switch (Clipping)

Concept:
For this assignment, I wanted to create a simple switch using something in my room. I decided to do it with the laundry clip. I had to attach the wires to both ends of the clips so that the circuit is complete as soon as the clip is closed. I tied a thread on both ends of the clip and placed wires in it. Now, if the clip is closed the light is on, otherwise, if it’s open, the light will turn off.

Here’s the video:

IMG_7699 (1)

Future Improvements:

I was able to achieve what I wanted from this assignment. However, in the future, I would like to use a conductor between the clips so that I can increase the distance between the Arduino board and the switch. Moreover, I would like to use the switch for some games or interaction between Arduino and the computer.