Looking into this now, I am beginning to realize that my efforts may have been better spent modifying one of those little "Dancing Santas" that they sell every year around Christmas - but at any rate:
I've purchase an Arduino Uno, a Paralax Motion Sensor, a MicroSD card, a WaveShare MicroSD Card Reader, and scavenged a vibration motor from an old game controller I had laying around.
As they parts may hint, I'm looking to create a project that detects motion, begins playing a sound, and about a second and a half into that sound, turns on the vibration motor for a few seconds.
The project will end up 'animating' (playing a sound and shaking, really) a toy turkey (a conversation piece for my hunting in-laws).
I feel confident in most of my canabilized code (combined publically available motion sensor tutorials, sound file tutorials, and am attempting to modify the LED blink code to wind my vibration motor up and then back down), but I'm not entirely sure how to make a 'loop within a loop'.
Attached below is my code: I'm hoping someone can help me incorporate the motor code (even further below) or simplify this without requiring me to purchase anymore hardware.
Drafted Code:
#include <pcmRF.h>
#include <TMRpcm.h>
#include <SD.h> // need to include the SD library
//#define SD_ChipSelectPin 53 //example uses hardware SS pin 53 on Mega2560
#define SD_ChipSelectPin 10 //using digital pin 4 on arduino nano 328
#include <TMRpcm.h> // also need to include this library...
const int sensorPin = 2; // PIR Sensor is attached to digital pin 2
const int ledPin = 13; // Built-in LED
const int ledBlinkTime = 500; // Blink one for half a second while calibrating
int motor = 6; // the pin that the motor is attached to
int strength = 0; // how strong the motor is
int fadeAmount = 5; // how many points to fade the motor by
TMRpcm tmrpcm; // create an object for use in this sketch
char mychar;
// Wait for the seonsor to calibrate (20 - 60 seconds according to datasheet)
// 60 Seconds in milliseconds
const unsigned int calibrationTime = 60000;
void setup(){
tmrpcm.speakerPin = 9; //11 on Mega, 9 on Uno, Nano, etc
// declare pin 6 to be an output:
pinMode(motor, OUTPUT);
Serial.begin(9600);
if (!SD.begin(SD_ChipSelectPin)) { // see if the card is present and can be initialized:
Serial.println("SD fail");
return; // don't do anything more if not
}
tmrpcm.play("gobble.wav"); //the sound file "music" will play each time the arduino powers up, or is reset
Serial.begin(115200);
pinMode(sensorPin, INPUT);
pinMode(ledPin, OUTPUT);
// We need to wait one minute for the sensor to calibrate
// Get out of view of the sensor for this duration!
// Blink the LED while calibrating
for (unsigned int i=0; i<calibrationTime; i+=ledBlinkTime*2) {
digitalWrite(ledPin, HIGH);
delay(ledBlinkTime);
digitalWrite(ledPin, LOW);
delay(ledBlinkTime);
}
}
void loop(){
// Constantly check the state of pin 2
// If it is HIGH the sensor is detecting motion
if (digitalRead(sensorPin) == HIGH) {
// Turn the LED on
digitalWrite(ledPin, HIGH);
// Play Spitting and Drumming Sound
tmrpcm.play("spit.wav");
// Wait a second and a half before shaking
delay(1500);
// Start Shaking
// I somehow need to incorpoate the motor code into this section, but the motor code appears to rely on it's own repeating loop for functionality.
} else {
// Turn the LED off
digitalWrite(ledPin, LOW);
}
}
Motor Code:
// the loop routine runs over and over again forever:
void loop() {
// set the strength of pin 6:
analogWrite(motor, strength);
// change the strength for next time through the loop:
strength = strength + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (strength == 0 || strength == 255) {
fadeAmount = -fadeAmount ;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
Thank you very much for your time - I know I'm new here and tragically under skilled - but I'm hoping to improve on that.