Skip to content

Instantly share code, notes, and snippets.

@ringodin
Created September 25, 2016 09:36
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 ringodin/88f659631fac522d10fae4025b58e723 to your computer and use it in GitHub Desktop.
Save ringodin/88f659631fac522d10fae4025b58e723 to your computer and use it in GitHub Desktop.
int ledPin = 13;
//led for visualization (use 13 for built-in led)
int speakerPin = 8;
//speaker connected to one of the PWM ports
#define c 261
#define d 294
#define e 329
#define f 349
#define g 391
#define gS 415
#define a 440
#define aS 455
#define b 466
#define cH 523
#define cSH 554
#define dH 587
#define dSH 622
#define eH 659
#define fH 698
#define fSH 740
#define gH 784
#define gSH 830
#define aH 880
#include <Servo.h>
Servo myservo;
int yellow = 7;
int red = 6;
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// the setup function runs once when you press reset or power the board
void setup() {
pinMode(ledPin, OUTPUT);
// sets the ledPin to be an output
pinMode(speakerPin, OUTPUT);
//sets the speakerPin to be an output
myservo.attach(9);
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("LCD booting up");
}
// the loop function runs over and over again forever
void loop() {
lcd.setCursor(0, 1);
lcd.print("yay");
delay(500);
lcd.setCursor(0, 1);
lcd.print("For Robots");
delay(500);
lcd.setCursor(0, 1);
lcd.print(" ");
delay(500);
lcd.setCursor(0, 0);
lcd.print("Use the Force ");
delay(1000);
// lcd.setCursor(0, 0);
// lcd.print(" ");
// delay(1000);
// lcd.begin(16, 2);
// Print a message to the LCD.
//lcd.print("rocks");
//
// delay(1000);
march();
digitalWrite(yellow, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); // wait for a second
digitalWrite(yellow, LOW); // turn the LED off by making the voltage LOW
digitalWrite(red, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); // wait for a second
digitalWrite(red, LOW); // turn the LED off by making the voltage LOW
myservo.write(120); // tell servo to go to position in variable 'pos'
delay(200); // waits 15ms for the servo to reach the position
myservo.write(70); // tell servo to go to position in variable 'pos'
delay(500);
}
void beep (unsigned char speakerPin, int frequencyInHertz, long timeInMilliseconds)
{
digitalWrite(ledPin, HIGH);
//use led to visualize the notes being played
int x;
long delayAmount = (long)(1000000/frequencyInHertz);
long loopTime = (long)((timeInMilliseconds*1000)/(delayAmount*2));
for (x=0;x<loopTime;x++)
{
digitalWrite(speakerPin,HIGH);
delayMicroseconds(delayAmount);
digitalWrite(speakerPin,LOW);
delayMicroseconds(delayAmount);
}
digitalWrite(ledPin, LOW);
//set led back to low
delay(20);
//a little delay to make all notes sound separate
}
void march()
{
//for the sheet music see:
//http://www.musicnotes.com/sheetmusic/mtd.asp?ppn=MN0016254
//this is just a translation of said sheet music to frequencies / time in ms
//used 500 ms for a quart note
beep(speakerPin, a, 500);
beep(speakerPin, a, 500);
beep(speakerPin, a, 500);
beep(speakerPin, f, 350);
beep(speakerPin, cH, 150);
beep(speakerPin, a, 500);
beep(speakerPin, f, 350);
beep(speakerPin, cH, 150);
beep(speakerPin, a, 1000);
//first bit
beep(speakerPin, eH, 500);
beep(speakerPin, eH, 500);
beep(speakerPin, eH, 500);
beep(speakerPin, fH, 350);
beep(speakerPin, cH, 150);
beep(speakerPin, gS, 500);
beep(speakerPin, f, 350);
beep(speakerPin, cH, 150);
beep(speakerPin, a, 1000);
//second bit...
/*
beep(speakerPin, aH, 500);
beep(speakerPin, a, 350);
beep(speakerPin, a, 150);
beep(speakerPin, aH, 500);
beep(speakerPin, gSH, 250);
beep(speakerPin, gH, 250);
beep(speakerPin, fSH, 125);
beep(speakerPin, fH, 125);
beep(speakerPin, fSH, 250);
delay(250);
beep(speakerPin, aS, 250);
beep(speakerPin, dSH, 500);
beep(speakerPin, dH, 250);
beep(speakerPin, cSH, 250);
//start of the interesting bit
beep(speakerPin, cH, 125);
beep(speakerPin, b, 125);
beep(speakerPin, cH, 250);
delay(250);
beep(speakerPin, f, 125);
beep(speakerPin, gS, 500);
beep(speakerPin, f, 375);
beep(speakerPin, a, 125);
beep(speakerPin, cH, 500);
beep(speakerPin, a, 375);
beep(speakerPin, cH, 125);
beep(speakerPin, eH, 1000);
//more interesting stuff (this doesn't quite get it right somehow)
beep(speakerPin, aH, 500);
beep(speakerPin, a, 350);
beep(speakerPin, a, 150);
beep(speakerPin, aH, 500);
beep(speakerPin, gSH, 250);
beep(speakerPin, gH, 250);
beep(speakerPin, fSH, 125);
beep(speakerPin, fH, 125);
beep(speakerPin, fSH, 250);
delay(250);
beep(speakerPin, aS, 250);
beep(speakerPin, dSH, 500);
beep(speakerPin, dH, 250);
beep(speakerPin, cSH, 250);
//repeat... repeat
beep(speakerPin, cH, 125);
beep(speakerPin, b, 125);
beep(speakerPin, cH, 250);
delay(250);
beep(speakerPin, f, 250);
beep(speakerPin, gS, 500);
beep(speakerPin, f, 375);
beep(speakerPin, cH, 125);
beep(speakerPin, a, 500);
beep(speakerPin, f, 375);
beep(speakerPin, c, 125);
beep(speakerPin, a, 1000);
//and we're done \ó/ */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment