Skip to content

Instantly share code, notes, and snippets.

@andrewbolster
Created November 15, 2013 11:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewbolster/7482781 to your computer and use it in GitHub Desktop.
Save andrewbolster/7482781 to your computer and use it in GitHub Desktop.
Jing Deng's Arduino code for RCUK Magherafelt project
/*
Melody
Plays a melody
circuit:
* 8-ohm speaker on digital pin 8
created 21 Jan 2010
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
http://arduino.cc/en/Tutorial/Tone
*/
// #include "pitches.h"
const int led_R = 13;
const int led_Y = 12;
const int led_G = 11;
const int led_B = 10;
const int tilt = 9;
const int button1 = 8;
const int button2 = 7;
const int button3 = 6;
const int button4 = 5;
const int speaker = 4;
const int lightSensor = A2;
const int tempSensor = A1;
const int knob = A0;
int fadeValue = 0;
int lightValue = 0;
int tempValue = 0;
int knobValue = 0;
int tiltState = 0;
int button1State = 0;
int button2State = 0;
int button3State = 0;
int button4State = 0;
// notes in the melody:
int melody[] = {
262, 196, 196, 220, 196, 0, 247, 262, 3951, 3520, 2349};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
4, 8, 8, 4,4,4,4,4 };
void setup() {
Serial.begin(9600);
pinMode(led_R, OUTPUT);
pinMode(led_Y, OUTPUT);
pinMode(led_G, OUTPUT);
pinMode(led_B, OUTPUT);
pinMode(speaker, OUTPUT);
pinMode(tilt, INPUT);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
// iterate over the notes of the melody:
// for (int thisNote = 0; thisNote < 8; thisNote++) {
//
// // to calculate the note duration, take one second
// // divided by the note type.
// //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
// int noteDuration = 1000/noteDurations[thisNote];
// tone(4, melody[thisNote],noteDuration);
//
// // to distinguish the notes, set a minimum time between them.
// // the note's duration + 30% seems to work well:
// int pauseBetweenNotes = noteDuration * 1.30;
// delay(pauseBetweenNotes);
// // stop the tone playing:
// noTone(4);
// }
}
void loop() {
// initialization and read values from all inputs
int noteDuration = 1000/4;
int knobmap = 0;
int pauseBetweenNotes = noteDuration * 1.30;
lightValue = analogRead(lightSensor);
tempValue = analogRead(tempSensor);
knobValue = analogRead(knob);
tiltState = digitalRead(tilt);
button1State = digitalRead(button1);
button2State = digitalRead(button2);
button3State = digitalRead(button3);
button4State = digitalRead(button4);
// if the breadboard was held at a right angle, it plays sound
if (tiltState != HIGH)
{
tone(speaker, melody[10],noteDuration);
delay(pauseBetweenNotes);
tone(speaker, melody[9],noteDuration);
delay(pauseBetweenNotes);
noTone(speaker);
}
// turn the knob to hear different sound
if (knobValue > 20)
{
int thisPitch = map(knobValue, 0, 1023, 50, 2000);
tone(speaker, (thisPitch * 2.5), 5000);
delay(175);
tone(speaker, (thisPitch * 2), 5000);
delay(175);
tone(speaker, (thisPitch * 1.5), 5000);
delay(175);
tone(speaker, (thisPitch), 5000);
delay(175);
thisPitch = thisPitch * .5;
tone(speaker, (thisPitch), 5000);
delay(175);
tone(speaker, (thisPitch * 1.5), 5000);
delay(175);
tone(speaker, (thisPitch * 2), 5000);
delay(175);
tone(speaker, (thisPitch * 2.5), 5000);
delay(175);
}
// check different button to play different sound
if (button1State == HIGH)
{
tone(speaker, 200, noteDuration);
delay(pauseBetweenNotes);
}
else if ((button2State == HIGH))
{
tone(speaker, 1000,noteDuration);
delay(pauseBetweenNotes);
}
else if ((button3State == HIGH))
{
tone(speaker, 3000,noteDuration);
delay(pauseBetweenNotes);
}
else if ((button4State == HIGH))
{
tone(speaker, 5000, noteDuration);
delay(pauseBetweenNotes);
}
else
{
noTone(speaker);
}
// check temperature and light up coloured LED
fadeValue = 255 - map(lightValue, 0, 1023, 0, 255);
if (tempValue <= 300)
{
digitalWrite(led_R,LOW);
digitalWrite(led_Y,LOW);
analogWrite(led_G,0);
analogWrite(led_B,fadeValue);
}
else if (300 < tempValue && tempValue <= 400)
{
digitalWrite(led_R,0);
digitalWrite(led_Y,0);
analogWrite(led_G,fadeValue);
analogWrite(led_B,0);
}
else if (400 < tempValue && tempValue <= 500)
{
digitalWrite(led_R,0);
digitalWrite(led_Y,1);
analogWrite(led_G,0);
analogWrite(led_B,0);
}
else
{
digitalWrite(led_R,1);
analogWrite(led_Y,0);
analogWrite(led_G,0);
analogWrite(led_B,0);
tone(speaker, 2500, 5000);
delay(175);
tone(speaker, 3000, 5000);
delay(175);
}
// write analog sensor value to computer
Serial.print("light sensor = " );
Serial.println(lightValue);
Serial.print("\t temperature sensor = ");
Serial.println(tempValue);
Serial.print("\t knob = ");
Serial.println(knobValue);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment