Back to Parent

The code used for running the artifact's program
//_______________________________________________________________________________________
//Libraries & Definitions----------------------------------------------------------------
#include <Adafruit_NeoPixel.h>
#include <Servo.h>
#define PIXEL_TYPE NEO_GRB + NEO_KHZ800

//_______________________________________________________________________________________
//Variables------------------------------------------------------------------------------

//Pin allocation
int ledPin1 = 2;
int ledPin2 = 3;
int speaker = 8;
int servoPin = 11;
int button = 12;
int distanceSensorPin = A0;

//Other
int pixelCount = 12;
unsigned long prev_time = millis();
int distanceThreshold = 350;
Adafruit_NeoPixel led1 = Adafruit_NeoPixel(pixelCount, ledPin1, PIXEL_TYPE);
Adafruit_NeoPixel led2 = Adafruit_NeoPixel(pixelCount, ledPin2, PIXEL_TYPE);
Servo servo;
bool servoIncreasing = true; //Keeps track of which way servo should be turning
int servoAngle = 0;
unsigned long motorTimer = millis(); //Keeps track of last time the motor changed its angle
int motorTimerThreshold = 1500; //Sets amount of time that needs to pass before motor turns
int motorAngleChange = 20; //Changes how much the motor changes its angle each turn


//_______________________________________________________________________________________
//Helper Functions-----------------------------------------------------------------------
int getAngle(Servo motor, int motorAngle, bool motorIncreasing) {
  if (motorIncreasing == true) {
    return motorAngle + motorAngleChange;
  }
  else {
    return motorAngle - motorAngleChange;
  }
}


//_______________________________________________________________________________________
//Initialization-------------------------------------------------------------------------

void setup() {
  Serial.begin(9600);
  pinMode(button, INPUT);
  led1.begin();
  led1.show();
  led2.begin();
  led2.show();
  servo.attach(servoPin);
  servo.write(0);
}


//________________________________________________________________________________________
//Execution-------------------------------------------------------------------------------

void loop() {
  int buttonState = digitalRead(button); //Records whether or not button is being pressed
  int distance = analogRead(distanceSensorPin); //Records distance detected by sensor

  //If nothing is nearby, make LEDs blue and allow doorbell to be rung
  if (distance <= distanceThreshold) {
    delay(50); //to ensure there's really nobody around
    if (distance <= distanceThreshold) {
      //Make LEDs Blue
      uint32_t c = led1.Color(0, 0, 255);
      led1.fill(c, 0);
      led2.fill(c, 0);

      //Ring doorbell
      if (buttonState == HIGH) {
        delay(50); //to ensure button is actually being pressed
        if (buttonState == HIGH) {
          tone(speaker, 53);
        }
        else noTone(speaker);
      }
      else (noTone(speaker));
    }
    else (noTone(speaker));
  }
  else {
    //Make LEDs red
    uint32_t c = led1.Color(255, 0, 0);
    led1.fill(c, 0);
    led2.fill(c, 0);
    noTone(speaker);
  }
  led1.show();
  led2.show();
  
  //Make servo motor gradually pan from left to right
  if (millis() - motorTimer >= motorTimerThreshold) {
    if (servoAngle > 170) {
      servoIncreasing = false;
    }
    if (servoAngle < 10) {
      servoIncreasing = true;
    }
    servoAngle = getAngle(servo, servoAngle, servoIncreasing);
    servo.write(servoAngle);
    motorTimer = millis();
  }

  //Serial Monitor
  unsigned long curr_time = millis();
  if( (curr_time - prev_time) > 50) {
    Serial.println(distance);
    prev_time = curr_time;
  }
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0