Miniature Laser Shooting Gallery

PHOTO EMBED

Tue Jan 04 2022 06:41:48 GMT+0000 (UTC)

Saved by @thisisgarnet #c #java

#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
#include <Adafruit_NeoPixel.h>
#include <Servo.h>

// sensors
#define LDR0pin A0 
#define LDR1pin A1 
#define LDR2pin A2 
#define LDR3pin A3 
#define LDR4pin A4 

// servos/motors
#define boo1pin 2
#define boo2pin 3
#define ceilingpin 4

// gun
#define buttpin 5
#define indipin 6
#define laserpin 7

// LEDs
#define melodypin 8
#define mompin 9
#define DATA_PIN 10 
#define NUM_LEDS 20 

// DFPlayer
SoftwareSerial mySoftwareSerial(12, 13); // TX, RX
DFRobotDFPlayerMini myDFPlayer;

// led strips
Adafruit_NeoPixel lightning_strip = Adafruit_NeoPixel(NUM_LEDS, DATA_PIN, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel melody = Adafruit_NeoPixel(2, melodypin, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel mom = Adafruit_NeoPixel(2, mompin, NEO_GRB + NEO_KHZ800);

// servos
// twelve servo objects can be created on most boards
Servo servo0;  // create servo1 object to control a servo1
Servo servo1;
Servo servo2;

// ADJUST FOR LIGHT THRESHOLDS
short laserThresh = 240;
short altThresh = 255;

// lightning
unsigned long flickerTime = 0, fadeInTime = 0;
byte i = 6, lowBright = 10, highBright = 255;
boolean lightState = true;

// boo servos
unsigned long booTime = 0;
boolean booState = false;

// mom, melody, ceiling trigger times
unsigned long momTime = 0, melodyTime = 0, ceilingTime = 0;

 // used for debouncing button
unsigned long lastDebounceTime = 0, highTime = 1, lasthighTime = 0;
byte buttonState, lastButtonState = HIGH;
boolean adjust = true;

// sensors
byte LDR0 = 0, LDR1 = 0, LDR2 = 0, LDR3 = 0, LDR4 = 0, LDR5 = 0;

void setup() {
  lightning_strip.begin();
  lightning_strip.setBrightness(lowBright);
  setLightning(0,255,0);

  melody.begin();
  melody.setBrightness(100);
  setMelody(0,0,0);

  mom.begin();
  mom.setBrightness(100);
  setMom(0,0,0);
  
  mySoftwareSerial.begin(9600);
  Serial.begin(9600);
  
  Serial.println();
  Serial.println(F("DFRobot DFPlayer Mini Demo"));
  Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
  
  if (!myDFPlayer.begin(mySoftwareSerial)) {  //Use softwareSerial to communicate with mp3.
    Serial.println(F("Unable to begin:"));
    Serial.println(F("1.Please recheck the connection!"));
    Serial.println(F("2.Please insert the SD card!"));
    while(true);
  }
  Serial.println(F("DFPlayer Mini online."));

  // CHANGE TO 20
  myDFPlayer.volume(30);  //Set volume value (0~30).

  // gun
  pinMode(laserpin, OUTPUT);
  pinMode(indipin, OUTPUT);
  pinMode(buttpin, INPUT_PULLUP);

  // sensors
  pinMode(LDR0pin, INPUT);
  pinMode(LDR1pin, INPUT);
  pinMode(LDR2pin, INPUT);
  pinMode(LDR3pin, INPUT);
  pinMode(LDR4pin, INPUT);

  // servos
  
  servo0.attach(boo1pin);
  servo1.attach(boo2pin);  // attaches the servo1 on pin 9 to the servo1 object
  servo2.attach(ceilingpin);

  // initialize servo positions:
  servo0.write(40); // boo1 swivels out RIGHT
  servo1.write(40); // boo2 swivels out LEFT
  servo2.write(90); // ceiling starts horizontal

  // opening song
  myDFPlayer.playMp3Folder(0);
}

void loop() {
  LDR0 = analogRead(LDR0pin);
  LDR1 = analogRead(LDR1pin);
  LDR2 = analogRead(LDR2pin);
  LDR3 = analogRead(LDR3pin);
  LDR4 = analogRead(LDR4pin);

  //trigger(trig_time, reload_time);
  //default (250, 1000)
  trigger(250, 1000);

  // boo(toggle_interval
  boo(2000);

  //lightning(flickerRate, blackout_time);
  lightning(150, 3000);
  lightning_strip.show();
  Serial.println(LDR4);

  //targetHit(playTime)
  melodyHit(4000);
  momHit(5500);
  ceiling(3000);
}

void ceiling (short playTime) {
  if (LDR4 > laserThresh) {
     ceilingTime = millis();
     myDFPlayer.playMp3Folder(4);
  }
  
  if (millis() - ceilingTime < playTime) servo2.write(0);
  else servo2.write(90);
}

void melodyHit (short playTime) {
  // if target hit, reset routine
  if (LDR2 > laserThresh) {
    melodyTime = millis();
    myDFPlayer.playMp3Folder(2);
  }

  if ((millis() - melodyTime < playTime)&&(melodyTime > 0)) setMelody(128, 0, 128);
  else setMelody(0, 0, 0);
}

void momHit (short playTime) {
  // if target hit, reset routine
  if (LDR3 > laserThresh) {
    momTime = millis();
    myDFPlayer.playMp3Folder(3);
  }

  if ((millis() - momTime < playTime)&&(momTime > 0)) setMom(128, 0, 128);
  else setMom(0, 0, 0);
}

void boo (short toggle_interval) {
  if (millis() - booTime > toggle_interval) {
    booState = !booState;
    booTime = millis();
  }
  
  if (booState)  {
    servo0.write(40);
    servo1.write(40);
  }
  
  else  {
    servo0.write(160);
    servo1.write(160);
  }
}

void lightning (short flickerRate, short blackout) {
  if (i > 6) i = 6;
  
  // if target hit, reset routine
  if ((LDR0 > laserThresh)||(LDR1 > laserThresh)) {
    i = 0;
    lightning_strip.setBrightness(highBright);
    lightState = true;
    myDFPlayer.playMp3Folder(1);
  }

  // flicker on/off 5 times at flickerRate
  if ((millis() - flickerTime > flickerRate)&& (i < 5)) 
  {
    lightState = !lightState;
    if (lightState) setLightning(255,255,255);
    else setLightning(0,0,0);
    i++;
    flickerTime = millis();
  }

  // lights stay off for blackout time
  else if ((millis() - flickerTime > blackout)&& (i == 5)) 
  {
    lightning_strip.setBrightness(lowBright);
    for (short j = 0 ; j < 255 ; ) {
      if (fadeInTime - millis() > 25) {
        setLightning(0, j, 0);
        j++;
      }
      fadeInTime = millis();
    }
    
    lightState = !lightState;
    flickerTime = millis();
    i++;
    
  }
}

void trigger(short trig_time, short reload) {
  adjust = digitalRead(buttpin);

  if (adjust != lastButtonState) {
    lastDebounceTime = millis();
  }
  
  if ((millis() - lastDebounceTime) > 50) {
    
    if (adjust != buttonState) {
      buttonState = adjust;
  
      if ((buttonState == LOW) && (millis() > 500)) {
        highTime = millis();
      }
    }
  }
  lastButtonState = adjust;

  if ((millis() - highTime <= trig_time)&&(millis() > 500)) {
    if (highTime-lasthighTime >= reload) {
      digitalWrite(laserpin, HIGH);
      lasthighTime = highTime;
    }
    digitalWrite(indipin, LOW);
  }
  else {
    digitalWrite(laserpin, LOW);
    digitalWrite(indipin, HIGH);
  }
}

// Set all LEDs to a given color and apply it (visible)
void setLightning (byte red, byte green, byte blue) {
  for(byte i = 0; i < NUM_LEDS; i++ ) {
    lightning_strip.setPixelColor(i, lightning_strip.Color(red, green, blue));
  }
  lightning_strip.show();
}

// Set all LEDs to a given color and apply it (visible)
void setMom (byte red, byte green, byte blue) {
  for(byte i = 0; i < 2; i++ ) {
    mom.setPixelColor(i, mom.Color(red, green, blue));
  }
  mom.show();
}

// Set all LEDs to a given color and apply it (visible)
void setMelody (byte red, byte green, byte blue) {
  for(byte i = 0; i < 2; i++ ) {
    melody.setPixelColor(i, melody.Color(red, green, blue));
  }
  melody.show();
}
content_copyCOPY