14 March 2021

Project NIMES: Alarm Box


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 detect
 
float X, Y, Z, totalAccel;
 

Servo myServo;
int pos = 0;            //position of servo
int inc = 1;            //incruments to move servo
int timer = 0;          //timer instead of delay

int 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 acceleration
  X = 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 rolling
  if (totalAccel > ROLL_THRESHOLD) {
      for (int thisNote = 0; thisNote < 8; thisNote++) {  // iterate over the notes of the melody

            int nDuration = 5000 / nDurations[thisNote];  //sets duration of each note based on type
        
            tone(A0, melody[thisNote], nDuration);
        
            int pauseBetween = nDuration * 1.30; //sets minimum time between notes to distinguish them
        
            delay(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 value
      myServo.write(pos); //write it to servo
  }
}

  //Servo Function Code
void servoBeat(){
    if(millis() > timer + 15){
      if(pos == 0) inc = 15;     //At 0 degrees, begins to add inc value
      if(pos == 150) inc = -15;  //At 180 degrees, begins to subtract inc value
      myServo.write(pos);       //Makes the servo move to pos angle
      pos += inc;               //Adds inc to pos
      timer = 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.