Skip to content

Instantly share code, notes, and snippets.

@benrules2
Last active October 13, 2022 06:43
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benrules2/2f5304627c109e4ceb31b8ec12d69ee0 to your computer and use it in GitHub Desktop.
Save benrules2/2f5304627c109e4ceb31b8ec12d69ee0 to your computer and use it in GitHub Desktop.
Billy Bass
#include <AFMotor.h>
#define NUM_MOTORS 3
AF_DCMotor head(1, MOTOR12_1KHZ); // create motor #2, 64KHz pwm
AF_DCMotor mouth(2, MOTOR12_1KHZ);
AF_DCMotor tail(3, MOTOR34_1KHZ);
void runMotorOnOff(AF_DCMotor motor, int aniDelay = 500){
motor.run(FORWARD);
delay(aniDelay);
motor.run(BACKWARD);
delay(5);
motor.run(RELEASE);
delay(aniDelay);
}
void runTwoMotorsOnOff(AF_DCMotor motor1, AF_DCMotor motor2, int aniDelay = 500){
motor1.run(FORWARD);
motor2.run(FORWARD);
delay(aniDelay);
motor1.run(BACKWARD);
motor2.run(BACKWARD);
delay(5);
motor1.run(RELEASE);
motor2.run(RELEASE);
delay(aniDelay);
}
bool randomDecisionToTurnOn(int oddsOn) {
int score = random(1,100);
return score < oddsOn;
}
void releaseMotor(AF_DCMotor motor) {
motor.run(BACKWARD);
delay(15);
motor.run(RELEASE);
}
void tapTail(int duration, int times=5){
int iterDuration = duration / times;
for (int i = 0; i < times; i++) {
runMotorOnOff(tail, iterDuration);
}
}
void dance(int duration, int times = 1){
int elapsedTime = 0;
int iterDuration = duration / times;
for (int i = 0; i < times; i++) {
if(randomDecisionToTurnOn(60)){
runTwoMotorsOnOff(head, tail, iterDuration);
} else {
tapTail(iterDuration/2, times*2);
}
}
}
void talk(int duration, int wordCount) {
int iterDuration = duration / wordCount;
head.run(FORWARD);
for (int i = 0; i < wordCount; i++) {
runMotorOnOff(mouth, iterDuration);
}
}
AF_DCMotor motors[NUM_MOTORS] = {head, mouth, tail};
void setupFishMotors() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Motor test!");
for (int i = 0; i < NUM_MOTORS; i++) {
motors[i].setSpeed(255);
}
}
void billySleep(){
head.run(RELEASE);
tail.run(RELEASE);
mouth.run(RELEASE);
}
void moveBilly(int movementCount, int duration){
if (movementCount > 3 && randomDecisionToTurnOn(50)) {
billySleep();
dance(duration);
} else {
talk(duration, random(2,6));
}
}
#include "AnimateBilly.h"
int sensorPin = A0; // select the input pin for the audio signal
int ledPin = 13;
int sampleSensor(int pin, int samples = 20, int duration = 250) {
float sensorValue = 0;
int iterDelay = duration / samples;
for (int i = 0; i < samples; i++) {
sensorValue += analogRead(pin);
delay(iterDelay);
}
digitalWrite(ledPin, LOW);
return (int)(sensorValue / samples);
}
int sampleAverages(int pin, int samples = 3, int duration = 100) {
int iterDelay = duration / samples;
int avgValue = 0;
for (int i = 0; i < samples; i++) {
avgValue += sampleSensor(pin, samples, iterDelay);
delay(iterDelay);
}
return (int)(avgValue / samples);
}
void setup() {
setupFishMotors();
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // open the serial port at 9600 bps:
}
int stillPlaying = 0;
int aniDuration = 500;
int previousValue = 0;
int quietThreshold = 2;
void loop() {
// read the value from the sensor:
int sensorValue = sampleSensor(sensorPin);
float smoothedSensorValue = (sensorValue + previousValue) / 2 ;
if(smoothedSensorValue > quietThreshold) {
digitalWrite(ledPin, HIGH);
stillPlaying = min(stillPlaying++, 4);
moveBilly(stillPlaying, aniDuration);
} else if (stillPlaying == 0){
billySleep();
digitalWrite(ledPin, LOW);
stillPlaying = 0;
} else {
stillPlaying--;
}
previousValue = sensorValue;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment