Arduino 8 key piano

Hey friendos,

Today I’m gonna show you a tiny little thing I built during Jterm and only now got to post. This is a tutorial on how to make your very own very basic piano using Arduino. The diagram is a bit crowded but it’s really not that complicated to make. 

Image

 

You will need 8 resistors, 8 buttons, 20 wires, one piezo speaker. And your Arduino and bread board of course. 

The code for this is as follows:

int button_C = 2;
int button_D = 3;
int button_E = 4;
int button_F = 5;
int button_G = 6;
int button_A = 7;
int button_B = 8;
int button_Cup = 9;

int speaker = 13;

int buttonstate_C = 0;
int buttonstate_D = 0;
int buttonstate_E = 0;
int buttonstate_F = 0;
int buttonstate_G = 0;
int buttonstate_A = 0;
int buttonstate_B = 0;
int buttonstate_Cup = 0;

//NOTES         'c'  , 'd',  'e',  'f',  'g', 'a',  'b',  'C'
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 }; //freq
int Cur_tone = 0;

void setup()
{
  pinMode(button_C, INPUT);
  pinMode(button_D, INPUT);
  pinMode(button_E, INPUT);
  pinMode(button_F, INPUT);
  pinMode(button_G, INPUT);
  pinMode(button_A, INPUT);
  pinMode(button_B, INPUT);
  pinMode(button_Cup, INPUT);

  pinMode(speaker, OUTPUT);
}

void loop()
{
	buttonstate_C = digitalRead(button_C);
	buttonstate_D = digitalRead(button_D);
	buttonstate_E = digitalRead(button_E);
	buttonstate_F = digitalRead(button_F);
	buttonstate_G = digitalRead(button_G);
	buttonstate_A = digitalRead(button_A);
	buttonstate_B = digitalRead(button_B);
	buttonstate_Cup = digitalRead(button_Cup);

	if((buttonstate_C == HIGH) || (buttonstate_E == HIGH) || 
		(buttonstate_G == HIGH) || (buttonstate_D == HIGH) || 
		(buttonstate_F == HIGH) || (buttonstate_A == HIGH) || 
		(buttonstate_B == HIGH) || (buttonstate_Cup == HIGH) )
	{ 
		if (buttonstate_C == HIGH)
		{
			Cur_tone = tones[0];
		} 
		if (buttonstate_E == HIGH)
		{
			Cur_tone = tones[1];
		}
		if (buttonstate_G == HIGH)
		{
			Cur_tone = tones[2];
		}
		if (buttonstate_D == HIGH)
		{
			Cur_tone = tones[3];
		}
		if (buttonstate_F == HIGH)
		{
			Cur_tone = tones[4];
		}
		if (buttonstate_A == HIGH)
		{
			Cur_tone = tones[5];
		}
		if (buttonstate_B == HIGH)
		{
			Cur_tone = tones[6];
		}
		if (buttonstate_Cup == HIGH)
		{
			Cur_tone = tones[7];
		}

		digitalWrite(speaker, HIGH);
		delayMicroseconds(Cur_tone);
		digitalWrite(speaker, LOW);
		delayMicroseconds(Cur_tone);
	}
	else //in case no button is pressed , close the piezo
	{
		digitalWrite(speaker, LOW);
	}

}

(adapted from godspeech.wordpress.com)

E.LIT/NET.ART 2.0

If you missed the fantastic exhibit of video games and computer-based art that the Media Lab was fortunate enough to host last semester, or if you didn’t miss it but just enjoyed it so much you want more— you’re in luck. This Thursday, February 6th, the E.LIT/NET.ART 2.0 exhibit will be showing at Hampshire College.

Screen Shot 2014-02-03 at 10.14.12 AM

(Not sure where the Harold F. Johnson Library is? It’s number 19 on the map here.)

Sound and pitch change with Arduino

Warning: this is a really really annoying little project. If you want to irritate everyone around you, then this project is for you.

My initial idea was to connect more than one pressure sensor to it and make it like a miniature piano. But I could only find one pressure sensor so I made do with it. 

Diagram:

Image

 

The code I used for this is really simple. I used the tone() function to code for what would normally be 5 lines of regular code.

int speakerPin = 12;
int pressurePin = 0;
 void setup()
{
}
 void loop()
{
  int reading = analogRead(pressurePin);
  int pitch = 200 + reading / 4;
  tone(speakerPin, pitch);
}
 
 
When you apply pressure to the sensor, the pitch of the note will change. It’s really annoying though so use with caution. 

Arduino: Robotic Wrist using 2 servos and a speaker

I’m currently working on a robotic arm that I hope will have fine motor control abilities.
So far, I’ve got the wrist movements down: I can move vertically and rotate. This is all controlled by an old SONY TV remote.
The vertical movements are controlled by one servo (in code: servoV) and the rotating movements are controlled by another (in code: servo). I’ve got buttons on the remote that do both macro and micro movements.
The speaker lets me know when I’ve exceeded arbitrarily selected max and min degree values (well not so arbitrary: I selected them because I noticed that the servos struggle past certain degrees).

Watch the Demo here! (less than 2 mins)

I’ve provided download links to all the materials you’ll need, but if for some reason they don’t work, download the files from my fritzing project profile:
Robotic Wrist with Speaker (at work #2)

To recreate my robotic wrist you’ll want to:

1 – copy and adapt my code in Arduino
2 – build the physical robot body with servos
3 – connect the servos, speaker, breadboard, and arduino following my circuit diagram

Parts used:
2 servos
1 IR receiver
1 Piezo Speaker
1 breadboard
1 Arduino
1 IR remote
1 9V battery
extra black and red wires
a USB cord that plugs into the Arduino
Some foam core, tape, and a large one of these clips 
Image

1: Copy and adapt my code in Arduino:
Code: Arduino (.ino) file    or    Code: Java (.java) document
Labelled (with results values) JPG of SONY Remote Control  ** you don’t really need this 
** if you use the Arduino code, make sure you place it in a folder that has the exact same name as the .ino file. If you don’t know what I mean, when you try to open the .ino file, Arduino will ask you if it’s okay to put it in a folder. Let it do that **

2: Build the Physical robot body
Watch the video here (2:30 mins)

3: Connect the pieces
Circuit diagram: fritzing (.fzz) document    or    Circuit diagram: PNG

Image

** note: If you’re using larger servos (like I am), make sure your power supply is sufficient. You’ll know it’s insufficient if the intro to Beethoven’s 5th Symphony repeatedly plays. This is because when the power supply is insufficient, the Arduino will keep rebooting, so it will repeatedly run through the setup.

Light up different LEDs under different light intensity

I built a little fun device with Arduino that uses a light sensor to detect how light the ambience is and lights up one of the LEDs accordingly. If it is bright in the surroundings the green LED will light, if it is dim the yellow LED will light and the red LED lights if it is completely dark.

Here is the circuit diagram for setting up the device

Image

Each LED has a wire connected to the UNO board to slot 9, 10, 11 individually, and the resistor and light sensor(photocell) all ultimately connect to the negative ground. Make sure they are all interconnected.

Below is the code with detailed comments:

// LDR reads the ambient light, and lights up one of the three LEDs,
//depending on how light the ambience is

#define LEDg 11
#define LEDy 10
#define LEDr 9
#define SENSOR 0

int val = 0;
int val_sensor = 0;

void setup() {

pinMode(LEDg, OUTPUT);
pinMode(LEDy, OUTPUT);
pinMode(LEDr, OUTPUT);
Serial.begin(9600);

//when plugged in, all the LED will light at first
digitalWrite(LEDg, HIGH);
digitalWrite(LEDy, HIGH);
digitalWrite(LEDr, HIGH);
delay(1000);
}

void loop() {
val = analogRead(0);

//print out the readings for light intensity from the sensor
Serial.println(val);

//default all the LED values to LOW so at first they won’t light

digitalWrite(LEDg, LOW);
digitalWrite(LEDy, LOW);
digitalWrite(LEDr, LOW);

 //bright surroundings light the green LED

if (val >= 500)
{
digitalWrite(LEDg, HIGH);
}

//medium light intensity, light the yellow LED
else if (val > 200)
{

digitalWrite(LEDy, HIGH);

}

//when the light intensity is VERY LOW, light the red LED
else
{

digitalWrite(LEDr, HIGH);
}

//set up the delay to 100 milliseconds so the LED respond to change in the light intensity faster
delay(100);
}

Measuring temperature with Arduino

Hi guys!

Here’s a fun little thing you can do with your Arduino. If you don’t have a thermometer but would still like to know the temperature in your room, you can build one using the temperature sensor found in the basic kits for Arduino. 

Image

 

I had to turn the sensor around in this diagram so you could actually see where the wires go. For pin 1 (which when the sensor faces you with the flat side is the left most one, but in this it’s the right most one), you will attach your power line (in this case 5V). Pin 2 goes to an analog source (A1 in my case) and number 3 is your ground pin.

Arduino code:

int tempPin =1; //this is the analog pin you’re connected to

void setup()
{Serial.begin(9600); //serial port connection to the computer

}

void loop()
{

int tempRead = analogRead(tempPin); //voltage reading from sensor
Serial.println(tempRead); //puts out the reading in the serial monitor
int tempC = map(tempRead, 0, 1023, -50, 450); //your values will be from 0 to 1024 which you then convert to your degrees using the map function

//10mv correspond to 1 degreeC
Serial.print(“new temp: “);
Serial.println(tempC);

delay(200);
}

Color certain part of the photo

Want to make your photo fancier? Here is a little useful trick

This is the original image:

Image

If we want to color only the flower and make the rest black and white, it will look like this:

Image

 

 

Open the image and select ‘Pen Tool’ from the tool barImage, the select ‘Path’ from the function window at the topImage, and draw along the figure of flower like this

Image

 

press ‘CTRL+ENTER’ to select the area you just drew, and then press ‘CTRL+SHIFT+I’ to select the counterpart

Image

 

Duplicate the background layer to get background layer copy, then select ‘<Image –> Adjustments –> Hue/Saturation>, a window will pop up and enter the data like following

Image

 

Now your image will look like this:

Image

You are done for now, but if you want to further change the color of the flower, do the following:

press ‘CTRL+SHIFT+I’ to counter-select the flower part, and get the window for ‘Hue/Saturation’ again( A fast way to do this is to press ‘CTRL+U’)

If I want the flower in bright color, I will enter the data as following

Image

 

Notice the color of flower changed? Press ‘CTRL+D’ to cancel selection, and you are done!

Image