127 lines
3.3 KiB
C++
127 lines
3.3 KiB
C++
/*
|
|
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"
|
|
#include <Wire.h>
|
|
|
|
int distance = 0; // Stores the calculated distance
|
|
byte high, low = 0; // High and low byte of distance
|
|
int shift = 0; // Value in shift bit register
|
|
|
|
#define ADDRESS 0x80 >> 1 // Arduino uses 7 bit addressing so we shift address right one bit
|
|
#define DISTANCE_REG 0x5E
|
|
#define SHIFT 0x35
|
|
|
|
#define buzzer 11
|
|
#define power_led 10
|
|
|
|
// Variables will change :
|
|
int ledState = LOW; // ledState used to set the LED
|
|
|
|
// Generally, you should use "unsigned long" for variables that hold time
|
|
// The value will quickly become too large for an int to store
|
|
unsigned long previousMillis = 0; // will store last time LED was updated
|
|
|
|
// constants won't change :
|
|
const long interval = 250; // interval at which to blink (milliseconds)
|
|
|
|
// notes in the melody:
|
|
int melody[] = {
|
|
NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
|
|
};
|
|
|
|
// note durations: 4 = quarter note, 8 = eighth note, etc.:
|
|
int noteDurations[] = {
|
|
4, 8, 8, 4, 4, 4, 4, 4
|
|
};
|
|
|
|
void setup() {
|
|
pinMode(power_led, OUTPUT);
|
|
digitalWrite(power_led, ledState);
|
|
// 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(buzzer, 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(buzzer);
|
|
}
|
|
// Start comms
|
|
Wire.begin();
|
|
Serial.begin(115200);
|
|
|
|
delay(50); // Delay so everything can power up
|
|
analogReference(INTERNAL);
|
|
// Read the sift bit register from the module, used in calculating range
|
|
Wire.beginTransmission(ADDRESS);
|
|
Wire.write(SHIFT);
|
|
Wire.endTransmission();
|
|
|
|
Wire.requestFrom(ADDRESS, 1);
|
|
while(Wire.available() == 0);
|
|
shift = Wire.read();
|
|
}
|
|
|
|
void loop() {
|
|
unsigned long currentMillis = millis();
|
|
if(currentMillis - previousMillis >= interval) {
|
|
// save the last time you blinked the LED
|
|
previousMillis = currentMillis;
|
|
|
|
// if the LED is off turn it on and vice-versa:
|
|
if (ledState == LOW)
|
|
ledState = HIGH;
|
|
else
|
|
ledState = LOW;
|
|
|
|
// set the LED with the ledState of the variable:
|
|
digitalWrite(power_led, ledState);
|
|
}
|
|
// Request and read the 2 address bytes from the GP2Y0E02B
|
|
Wire.beginTransmission(ADDRESS);
|
|
Wire.write(DISTANCE_REG);
|
|
Wire.endTransmission();
|
|
|
|
Wire.requestFrom(ADDRESS, 2);
|
|
|
|
while(Wire.available() < 2);
|
|
|
|
high = Wire.read();
|
|
low = Wire.read();
|
|
|
|
distance = (high * 16 + low)/16/(int)pow(2,shift); // Calculate the range in CM
|
|
|
|
Serial.print("Distance is ");
|
|
Serial.print(distance);
|
|
Serial.println("CM");
|
|
|
|
Serial.print("Battery is ");
|
|
Serial.print(analogRead(A5)*(2*2560/1023.));
|
|
Serial.println("mV");
|
|
|
|
|
|
delay(50);
|
|
}
|