int ledPin = D2;
int speakerPin = D4;
int melody[] = {1908,2551,2551,2273,2551,0,2024,1908};
int noteDurations[] = {4,8,8,4,4,4,4,4 };
int fsrPin = A0;
int fsrReading = 0;
int last_published = -1;
void setup() {
pinMode(ledPin, OUTPUT);
Particle.variable("force", &fsrReading, INT);
}
void loop() {
fsrReading = analogRead(fsrPin);
if(fsrReading > 1000 ){
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
playNotes();
}else{
digitalWrite(ledPin, LOW );
}
}
void playNotes()
{
// 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(speakerPin, 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(speakerPin);
}
}
void log_to_spreadsheet( ){
// check if 1 minute has elapsed
if( last_published + 60000 < millis() ){
Particle.publish( "log_to_spreadsheet", String(fsrReading ) );
last_published = millis();
}
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .