Pcomp Week4

Project1

Idea

I’ve played Chinese Zheng for 10years when I was young. (a traditional Chinese instrument.) There are only Five notes in Chinese called 宫(C)、商(D)角(E)徵(G)羽(A). I wanna use a speaker to simulate the notes.

Looks like this.

Scheme

I used the photoregister as the sensor, when the light is high(not touch the sensor), the speaker is quiet. When the light is low(touch the sensor)

Prototype

Code

#include "pitches.h"
 
int threshold0 = 300;      
int threshold1 = 300;
int threshold2 = 250;
int threshold3 = 300;
int threshold4 = 300;
int threshold5 = 300;

const int speakerPin = 8;      // pin number for the speaker
const int noteDuration = 30;   // play notes for 20 ms
 
// notes to play, corresponding to the 5 sensors:
int notes[] = {
NOTE_C5, NOTE_D5, NOTE_E5, NOTE_G5, NOTE_A5, NOTE_A4  };

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  int analogValue0 = analogRead(A0);
  Serial.println(analogValue0);
  if (analogValue0 < threshold0){
    tone(speakerPin, notes[0], noteDuration);
    delay(30);
    }

   int analogValue1 = analogRead(A1);
//  Serial.println(analogValue1);
  if (analogValue1 < threshold1){
    tone(speakerPin, notes[1], noteDuration);
    delay(30);
    }

   int analogValue2 = analogRead(A2);
//  Serial.println(analogValue2);
  if (analogValue2 < threshold2){
    tone(speakerPin, notes[2], noteDuration);
    delay(30);
    }
    
    int analogValue3 = analogRead(A3);
//   Serial.println(analogValue3);
   if (analogValue3 < threshold3){
    tone(speakerPin, notes[3], noteDuration);
    delay(30);
    }

     int analogValue4 = analogRead(A4);
//   Serial.println(analogValue4);
   if (analogValue4 < threshold4){
    tone(speakerPin, notes[4], noteDuration);
    delay(30);
    }

     int analogValue5 = analogRead(A5);
//   Serial.println(analogValue5);
   if (analogValue5 < threshold5){
    tone(speakerPin, notes[5], noteDuration);
    delay(30);
    }
}

Housing

Problems

At first, the speaker worked seperately, while when I linked them together, the speaker made a lot of noise. Then I figured out I did not add “delay” , after changing the code the speaker worked.

I also found each photocell is so different. This sensor is not stable (or too sensitive to detect the change of light). And the coding parts are different from using other sensors. I can not just use a similar code shown on the webpage. But it was hard to figure out how to adjust the code if you are a beginner.