I am looking for someone on here who is good with Arudino code. I actually have all the code ready, but need it altered to reflect the motors used on the motor shield to how it is now.
Basically what I’m doing is converting a billy bass 3 motor design to an Alexa bass, I have this great code, but it’s written for a small 2 motor h bridge. I need to adjusted to reflect for the Arduino Motor shield.
I’m a straight forward guy, great with tech but horrible with code, am hoping someone on here would be cool to adjust code. I’ll be happy to compensate. Should t take too long as code is already complete.
[Flag this post and ask a moderator to move it to the Jobs and Paid Consultancy category. More members that code for money will see it there. I was too slow.
Or post the code and, perhaps, someone will help you to convert the code for free. You never know till you try. Read the forum guidelines to see how to properly post code. Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags. include a schematic of the current hardware setup and links to relevant hardware data sheets or manuals. You will need to do that for anyone that you pay, anyway.
so here is the code,
was originally made for a basic 2 motor h bridge ,a MX1508.
My billy bass is an OG model , 3 motors, motor 1 mouth , motor 2 body, motor 3 tail.
using an Arduino motor shield instead of the mx1508 because MX1508 is a 2 motor tiny board.
code below:
MX1508 bodyMotor(6, 9); // Sets up an MX1508 controlled motor on PWM pins 6 and 9
MX1508 mouthMotor(5, 3); // Sets up an MX1508 controlled motor on PWM pins 5 and 3
int soundPin = A0; // Sound input
int silence = 12; // Threshold for "silence". Anything below this level is ignored.
int bodySpeed = 0; // body motor speed initialized to 0
int soundVolume = 0; // variable to hold the analog audio value
int fishState = 0; // variable to indicate the state Billy is in
bool talking = false; //indicates whether the fish should be talking or not
//these variables are for storing the current time, scheduling times for actions to end, and when the action took place
long currentTime;
long mouthActionTime;
long bodyActionTime;
long lastActionTime;
void setup() {
//make sure both motor speeds are set to zero
bodyMotor.setSpeed(0);
mouthMotor.setSpeed(0);
//input mode for sound pin
pinMode(soundPin, INPUT);
Serial.begin(9600);
}
void loop() {
currentTime = millis(); //updates the time each time the loop is run
updateSoundInput(); //updates the volume level detected
SMBillyBass(); //this is the switch/case statement to control the state of the fish
}
void SMBillyBass() {
switch (fishState) {
case 0: //START & WAITING
if (soundVolume > silence) { //if we detect audio input above the threshold
if (currentTime > mouthActionTime) { //and if we haven't yet scheduled a mouth movement
talking = true; // set talking to true and schedule the mouth movement action
mouthActionTime = currentTime + 100;
fishState = 1; // jump to a talking state
}
} else if (currentTime > mouthActionTime + 100) { //if we're beyond the scheduled talking time, halt the motors
bodyMotor.halt();
mouthMotor.halt();
}
if (currentTime - lastActionTime > 1500) { //if Billy hasn't done anything in a while, we need to show he's bored
lastActionTime = currentTime + floor(random(30, 60)) * 1000L; //you can adjust the numbers here to change how often he flaps
fishState = 2; //jump to a flapping state!
}
break;
case 1: //TALKING
if (currentTime < mouthActionTime) { //if we have a scheduled mouthActionTime in the future....
if (talking) { // and if we think we should be talking
openMouth(); // then open the mouth and articulate the body
lastActionTime = currentTime;
articulateBody(true);
}
}
else { // otherwise, close the mouth, don't articulate the body, and set talking to false
closeMouth();
articulateBody(false);
talking = false;
fishState = 0; //jump back to waiting state
}
break;
case 2: //GOTTA FLAP!
//Serial.println("I'm bored. Gotta flap.");
flap();
fishState = 0;
break;
}
}
int updateSoundInput() {
soundVolume = analogRead(soundPin);
}
void openMouth() {
mouthMotor.halt(); //stop the mouth motor
mouthMotor.setSpeed(220); //set the mouth motor speed
mouthMotor.forward(); //open the mouth
}
void closeMouth() {
mouthMotor.halt(); //stop the mouth motor
mouthMotor.setSpeed(180); //set the mouth motor speed
mouthMotor.backward(); // close the mouth
}
void articulateBody(bool talking) { //function for articulating the body
if (talking) { //if Billy is talking
if (currentTime > bodyActionTime) { // and if we don't have a scheduled body movement
int r = floor(random(0, 8)); // create a random number between 0 and 7)
if (r < 1) {
bodySpeed = 0; // don't move the body
bodyActionTime = currentTime + floor(random(500, 1000)); //schedule body action for .5 to 1 seconds from current time
bodyMotor.forward(); //move the body motor to raise the head
} else if (r < 3) {
bodySpeed = 150; //move the body slowly
bodyActionTime = currentTime + floor(random(500, 1000)); //schedule body action for .5 to 1 seconds from current time
bodyMotor.forward(); //move the body motor to raise the head
} else if (r == 4) {
bodySpeed = 200; // move the body medium speed
bodyActionTime = currentTime + floor(random(500, 1000)); //schedule body action for .5 to 1 seconds from current time
bodyMotor.forward(); //move the body motor to raise the head
} else if ( r == 5 ) {
bodySpeed = 0; //set body motor speed to 0
bodyMotor.halt(); //stop the body motor (to keep from violent sudden direction changes)
bodyMotor.setSpeed(255); //set the body motor to full speed
bodyMotor.backward(); //move the body motor to raise the tail
bodyActionTime = currentTime + floor(random(900, 1200)); //schedule body action for .9 to 1.2 seconds from current time
}
else {
bodySpeed = 255; // move the body full speed
bodyMotor.forward(); //move the body motor to raise the head
bodyActionTime = currentTime + floor(random(1500, 3000)); //schedule action time for 1.5 to 3.0 seconds from current time
}
}
bodyMotor.setSpeed(bodySpeed); //set the body motor speed
} else {
if (currentTime > bodyActionTime) { //if we're beyond the scheduled body action time
bodyMotor.halt(); //stop the body motor
bodyActionTime = currentTime + floor(random(20, 50)); //set the next scheduled body action to current time plus .02 to .05 seconds
}
}
}
void flap() {
bodyMotor.setSpeed(180); //set the body motor to full speed
bodyMotor.backward(); //move the body motor to raise the tail
delay(500); //wait a bit, for dramatic effect
bodyMotor.halt(); //halt the motor
}
because my billy bass is a 3 motor design, the MX1508 is only for 2 motors
so i chose a well reputed product Arudino and motor shield which supports 3 motors.
battery powered for the motor shield yes, standard seperate power for the Arduino and Amazon alexa
Yeah man the code is pretty complete actually I have 2 sets, one is actually done for 3 motor, it’s good code I think an error tho when compiled inside the arudino app. this was setup for a arudino motor shield same one i have . maybe i can get the errors figured out then i can test it this weekend and see if anything needs to be messed with. here is the code
/*
Make a set of motors move to sound.
Uses an Arduino Uno paired with the Adafruit Motor Shield v2.
Written with the larger Big Mouth Billy Bass (or any animatronic) in mind.
Modified by Joe Villegas on July 2018:
-Defined all motors
-Set variables (Ex: speed and sensitivity) for easier tweaking
-Added support for the third motor in some Basses (Disable/Enable third motor use)
-Included serial printouts for debugging and audio reading monitoring
August 2018:
-Added frantic mode (Maximum speed of motor movement, requires lots of power)
Original information:
Created by Donald Bell, Maker Project Lab (2016).
Based on Sound to Servo by Cenk Özdemir (2012)
and DCMotorTest by Adafruit
*/
// Libraries
#include <Wire.h>
#include <QueueArray.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
//* SETUP (!!! This is the usually the best and probably only place you need to adjust!)
boolean legacyFish = true; //For older models that incorporate three motors
//* Motor speed variables
int motorDelay = 0; //Delay between sound readings and motor movements
//* Range: 1 - 255. Lower makes most movement.
int mouthMotorSpeed = 100;
int headMotorSpeed = 100;
int tailMotorSpeed = 25;
// Funsies (ENABLE AT RISK (make sure you have power)
boolean modeFrantic = false;
int logDelay = 10;
//* SETUP finished
//Base system variables
int afmsFreq = 1600; //Default is 1600 (1.6kHz)
int audioSensitivity = 1023;
//Audio threshold variables
boolean headMovementEnabled = true; //TODO: head motor is unable to move the head itself (reglue?)
int staticThreshold = 1; //To leave out any unwanted movement due to static
int mouthAudioThreshold = 10;
int tailAudioThreshold = 25;
//* ---- This section below don't touch
//Handled all by system (Best to not touch)
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);
// Select which 'port' M1, M2, M3 or M4.
Adafruit_DCMotor *mouthMotor = AFMS.getMotor(1);
Adafruit_DCMotor *headMotor = AFMS.getMotor(2);
Adafruit_DCMotor *tailMotor = AFMS.getMotor(3);
// Some other Variables we need
int SoundInPin = A1;
int LedPin = 12; //in case you want an LED to activate while mouth moves
boolean audioDetected = false;
int audioLastDetected = 0;
QueueArray<int> audioReadingQueue;
boolean bodyMoved;
int speedFrantic = 254;
int ping = 0;
//* ---- This section above don't touch ---
//The setup routine runs once when you press reset:
void setup()
{
//Pre-setup tweaks
if (modeFrantic == true)
{
motorDelay = 0;
mouthMotorSpeed = speedFrantic;
headMotorSpeed = speedFrantic;
tailMotorSpeed = speedFrantic;
Serial.println("hello");
}
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("sup b");
AFMS.begin(afmsFreq);
if (afmsFreq == 1600)
{
Serial.println("Adafruit Motor Shield booted");
}
else
{
Serial.println("AFMS enabled with frequency of " + String(afmsFreq));
}
// Set the speed to start, from 0 (off) to 255 (max speed)
//mouth motor
mouthMotor->setSpeed(mouthMotorSpeed);
Serial.println("Mouth motor speed set to " + String(mouthMotorSpeed) + ".");
mouthMotor->run(FORWARD); // turn on motor
Serial.println("Mouth motor connected, attempted to move");
mouthMotor->run(RELEASE);
pinMode(SoundInPin, INPUT);
pinMode(LedPin, OUTPUT);
//head motor
headMotor->setSpeed(headMotorSpeed);
Serial.println("Head motor speed set to " + String(headMotorSpeed) + ".");
headMotor->run(FORWARD); // turn on motor
Serial.println("Head motor connected, attempted to move");
headMotor->run(RELEASE);
pinMode(SoundInPin, INPUT);
//tail motor
if (legacyFish == true)
{
tailMotor->setSpeed(tailMotorSpeed);
Serial.println("Tail motor speed set to " + String(tailMotorSpeed) + ".");
tailMotor->run(FORWARD); // turn on motor
Serial.println("Tail motor connected, attempted to move");
tailMotor->run(RELEASE);
}
Serial.println("Billy Bass is GO!");
}
void loop()
{
uint8_t i;
//Reading values from analog pin
int sensorValue = analogRead(SoundInPin);
sensorValue = map(sensorValue, 0, 256, 0, audioSensitivity); //Sets the range of audio readings
if (ping >= logDelay)
{
Serial.println("Audio - Input value: " + String(sensorValue));
ping = 0;
}
else
{
ping++;
}
//int LEDValue = map(sensorValue,0,512,0,255); // we Map another value of this for LED that can be a integer betwen 0..255
//int MoveDelayValue = map(sensorValue,0,255,0,sensorValue); // note normally the 512 is 1023 because of analog reading should go so far, but I changed that to get better readings.
//Passive movement (Moves the head when it detects any audio, and moves back when it becomes silent for a while)
if (headMovementEnabled)
{
//Gathering history
audioReadingQueue.enqueue(sensorValue);
//Serial.println("QUEUE SIZE: " + String(audioReadingQueue.count())); //DEBUG
if (audioReadingQueue.count() == 50)
{
if (audioReadingQueue.peek() > staticThreshold)
{
audioLastDetected = 0;
audioDetected = true;
}
else
{
audioLastDetected++;
audioDetected = false;
}
audioReadingQueue.pop();
//Serial.println("Audio - Last detected " + String(audioLastDetected) + " steps ago"); //DEBUG
}
//Moving head according to specific scenario
if (audioLastDetected < 50 && bodyMoved == false)
{
headMotor->run(FORWARD);
for (i = headMotorSpeed; i < 255; i++)
{
headMotor->setSpeed(i);
}
headMotor->setSpeed(0);
bodyMoved = true;
Serial.println("I'm ALIVEEEEE");
}
else if (audioLastDetected < 50)
{
//hold position
}
else if (bodyMoved == true)
{
headMotor->run(BACKWARD);
for (i = headMotorSpeed; i < 255; i++)
{
headMotor->setSpeed(i);
}
headMotor->setSpeed(0);
bodyMoved = false;
Serial.println("Imma be sleepin");
}
else
{
//hold position
}
}
//Active movement
if (sensorValue > mouthAudioThreshold)
{
delay(motorDelay);
// now move the motor
mouthMotor->run(FORWARD);
for (i = mouthMotorSpeed; i < 255; i++)
{
mouthMotor->setSpeed(i);
}
analogWrite(LedPin, sensorValue); //Brightens LED according to immediate audio strength
mouthMotor->run(RELEASE);
}
if (sensorValue > tailAudioThreshold)
{
delay(motorDelay);
// now move the motor
tailMotor->run(BACKWARD);
for (i = tailMotorSpeed; i < 255; i++)
{
tailMotor->setSpeed(i);
}
tailMotor->run(RELEASE);
}
// Done.
// turn off the led again.
analogWrite(LedPin, 0);
// and this repeats all the time.
}
mine is adafruit v3 and not a v2 as code states, but i believe comparing the 2 the directories should be the same. thank you for even looking at the code.
the errors im getting start at top of #include <queueArray.h> , again my first run so trying to understand. I can wire this thing together no problem, just getting code to work properly will be the challenge thankfully somebody did this project before and looks like they spent some time on the code
I just tried your code. Is this what your sketch returned?
sketch_oct28a:22:10: fatal error: QueueArray.h: No such file or directory
#include <QueueArray.h>
^~~~~~~~~~~~~~
compilation terminated.
exit status 1
QueueArray.h: No such file or directory
If so, just download the library that @mugambi mentioned and place it under "Arduino/libraries" in your files. Then restart the IDE and you should be able to upload your code.