I was at a bit of a loss for what to do for this project but, I have always been on a stealth theme kick recently, and was able to make a stealthy game with the last Circuit Playground project, so I started my concept brainstorming there. I focused on more of an alarm design, thinking of using the circuit playgrounds light and sound sensors to play a tone if you were “caught”. I had to move on from this though, as being in a box it would be difficult to do this with any precision without other sensors. I settled on a simple alarm that plays a sound if the box is moved using the circuit playground’s accelerometer. I use a servo attached A7 and controlled by the D5 built in switch to determine if the box is operational, a white LED attached to A1 and controlled by the potentiometer attached to A5 to light up the inside when working on it. For the function of the box itself though, I use the Circuit Playground’s built in Accelerometer(D27) to determine if it has been bumped or moved by an “intruder”. If it is, it plays a sad melody of descending notes through the built in speaker at A0, to indicate they have been “caught”. I think this is a fun and useful idea for a game or something, what are your thoughts?
Code:#include <Servo.h>#include <Adafruit_CircuitPlayground.h>#include <Adafruit_Circuit_Playground.h>#define led 6#include "pitches.h"#define ROLL_THRESHOLD 15 // Total acceleration threshold for roll detectfloat X, Y, Z, totalAccel;Servo myServo;int pos = 0; //position of servoint inc = 1; //incruments to move servoint timer = 0; //timer instead of delayint sensorValue = 0; //value read from the pot// notes in the melody:int melody[] = {NOTE_AS4, NOTE_A4, NOTE_GS4, NOTE_G4};// note durations: 4 = quarter note, 8 = eighth note, etc.:int nDurations[] = {8, 8, 8, 2};void setup() {// put your setup code here, to run once:Serial.begin(9600);delay(1000);CircuitPlayground.begin();CircuitPlayground.setAccelRange(LIS3DH_RANGE_8_G);myServo.attach(1); //Pin servo is attached to (A7)}void loop() {// put your main code here, to run repeatedly:// Compute total accelerationX = 0;Y = 0;Z = 0;for (int i=0; i<10; i++) {X += CircuitPlayground.motionX();Y += CircuitPlayground.motionY();Z += CircuitPlayground.motionZ();delay(1);}X /= 10;Y /= 10;Z /= 10;totalAccel = sqrt(X*X + Y*Y + Z*Z);// Play sound if rollingif (totalAccel > ROLL_THRESHOLD) {for (int thisNote = 0; thisNote < 8; thisNote++) { // iterate over the notes of the melodyint nDuration = 5000 / nDurations[thisNote]; //sets duration of each note based on typetone(A0, melody[thisNote], nDuration);int pauseBetween = nDuration * 1.30; //sets minimum time between notes to distinguish themdelay(pauseBetween);noTone(A0); //stops tone}}sensorValue = analogRead (A5);Serial.println (sensorValue);sensorValue = map(sensorValue, 0, 1023, 0, 255);analogWrite(led, sensorValue);if (CircuitPlayground.rightButton()) {servoBeat(); //call function servoBeat}else {pos = 0; //set position (pos) to original valuemyServo.write(pos); //write it to servo}}//Servo Function Codevoid servoBeat(){if(millis() > timer + 15){if(pos == 0) inc = 15; //At 0 degrees, begins to add inc valueif(pos == 150) inc = -15; //At 180 degrees, begins to subtract inc valuemyServo.write(pos); //Makes the servo move to pos anglepos += inc; //Adds inc to postimer = millis(); //adds current millis count to timer to prevent reading of if statement before servo completes action}}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.