Skip to content

Instantly share code, notes, and snippets.

@dadatuputi
Created May 25, 2020 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dadatuputi/211a09876cf436667302bb213f328c5d to your computer and use it in GitHub Desktop.
Save dadatuputi/211a09876cf436667302bb213f328c5d to your computer and use it in GitHub Desktop.
Halloween Arduino Slug: Arduino code for a motion-activated screaming pumpkin slug
#include <RBD_Timer.h>
#include <RBD_Light.h>
#include "SD.h"
#include "TMRpcm.h"
#include "SPI.h"
#define SD_ChipSelectPin 4
TMRpcm tmrpcm;
int redpin=3; //Pin 10
int greenpin=5; //Pin 11
int bluepin=6; //Pin 12
int colorr = 0;
int colorg = 0;
int colorb = 0;
long flickerTime = 0;
int played = 0;
RBD::Light eye(10);
int pirPin = 7;
char *sounds[6] = {"slimer.wav", "growl.wav", "growl2.wav", "scream.wav", "scream2.wav", "scream3.wav"};
void setup(){
// SOUND
tmrpcm.speakerPin = 9;
Serial.begin(9600);
if (!SD.begin(SD_ChipSelectPin)) {
Serial.println("SD fail");
}
// MOTION SENSOR
pinMode(pirPin, INPUT);
// COLOR LED
randomSeed(analogRead(0));
tmrpcm.setVolume(6);
eye.fade(1000,6000,1000,2000);
}
void loop(){
// DETECT MOTION
if (digitalRead(pirPin)){
// PLAY SOUND IF SOUND ISN'T PLAYING AND HASN'T ALREADY PLAYED
if (!played && !tmrpcm.isPlaying()){
tmrpcm.play(sounds[random(6)%6]);
played = 1;
}
// TURN LIGHT RED
analogWrite(redpin, 255);
analogWrite(greenpin, 0);
analogWrite(bluepin, 0);
} else {
played = 0;
// CANDLE FLICKER
if (millis() >= flickerTime){
flickerTime = millis() + random(600);
colorr = random(128);
colorg = random(255-colorr);
colorb = random(20);
analogWrite(redpin, 128+colorr);
analogWrite(greenpin, colorg);
analogWrite(bluepin, colorb);
}
}
eye.update();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment