Trying to get a motor and buzzer to run together. Motor wont run. How do i fix It?

For the discussion of Arduino related topics.

Moderator: phalanx

Post Reply
lukem776
Posts: 1
Joined: Fri Nov 10, 2017 8:04 am

Trying to get a motor and buzzer to run together. Motor wont run. How do i fix It?

Post by lukem776 » Fri Nov 10, 2017 8:10 am

I've been trying to run a motor and buzzer together. the buzzer works but the motor doesn't run. My code is below. Where did I go wrong?


const int motorPin = 10;

const int buzzerPin = 9;

const int songLength = 18;

char notes[] = "cdfda ag cdfdg gf "; // a space represents a rest

int beats[] = {1,1,1,1,1,1,4,4,2,1,1,1,1,1,1,4,4,2};

int tempo = 113;

void setup()
{

pinMode(motorPin, OUTPUT);

Serial.begin(9600);

pinMode(buzzerPin, OUTPUT);
}



void loop()
{
int i, duration;

for (i = 0; i < songLength; i++) // step through the song arrays
{
duration = beats * tempo; // length of note/rest in ms

if (notes == ' ') // is this a rest?
{
delay(duration); // then pause for a moment
}
else // otherwise, play the note
{
tone(buzzerPin, frequency(notes), duration);
delay(duration); // wait for tone to finish
}
delay(tempo/10); // brief pause between notes
}

// We only want to play the song once, so we'll pause forever:
while(true){}
// If you'd like your song to play over and over,
// remove the above statement
}


int frequency(char note)
{
// This function takes a note character (a-g), and returns the
// corresponding frequency in Hz for the tone() function.

int i;
const int numNotes = 8; // number of notes we're storing

// The following arrays hold the note characters and their
// corresponding frequencies. The last "C" note is uppercase
// to separate it from the first lowercase "c". If you want to
// add more notes, you'll need to use unique characters.

// For the "char" (character) type, we put single characters
// in single quotes.

char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};

// Now we'll search through the letters in the array, and if
// we find it, we'll return the frequency for that note.

for (i = 0; i < numNotes; i++) // Step through the notes
{
if (names == note) // Is this the one?
{
return(frequencies); // Yes! Return the frequency
}
}
return(0); // We looked through everything and didn't find it,
// but we still need to return a value, so return 0.
serialSpeed();
}


// This function turns the motor on and off like the blinking LED.
// Try different values to affect the timing.

void motorOnThenOff()
{
int onTime = 3000; // milliseconds to turn the motor on
int offTime = 3000; // milliseconds to turn the motor off

digitalWrite(motorPin, HIGH); // turn the motor on (full speed)
delay(onTime); // delay for onTime milliseconds
digitalWrite(motorPin, LOW); // turn the motor off
delay(offTime); // delay for offTime milliseconds
}


// This function alternates between two speeds.
// Try different values to affect the timing and speed.

void motorOnThenOffWithSpeed()
{
int Speed1 = 200; // between 0 (stopped) and 255 (full speed)
int Time1 = 3000; // milliseconds for speed 1

int Speed2 = 50; // between 0 (stopped) and 255 (full speed)
int Time2 = 3000; // milliseconds to turn the motor off

analogWrite(motorPin, Speed1); // turns the motor On
delay(Time1); // delay for onTime milliseconds
analogWrite(motorPin, Speed2); // turns the motor Off
delay(Time2); // delay for offTime milliseconds
}


// This function slowly accelerates the motor to full speed,
// then back down to zero.

void motorAcceleration()
{
int speed;
int delayTime = 20; // milliseconds between each speed step

// accelerate the motor

for(speed = 0; speed <= 255; speed++)
{
analogWrite(motorPin,speed); // set the new speed
delay(delayTime); // delay between speed steps
}

// decelerate the motor

for(speed = 255; speed >= 0; speed--)
{
analogWrite(motorPin,speed); // set the new speed
delay(delayTime); // delay between speed steps
}
}



void serialSpeed()
{
int speed;

Serial.println("Type a speed (0-255) into the box above,");
Serial.println("then click [send] or press [return]");
Serial.println(); // Print a blank line

while(true)
{

while (Serial.available() > 0)
{

speed = Serial.parseInt();

speed = constrain(speed, 0, 255);

Serial.print("Setting speed to ");
Serial.println(speed);


analogWrite(motorPin, speed);
}
}
}

jremington
Support Volunteer
Posts: 2271
Joined: Fri Jun 15, 2007 9:41 pm
Location: Eugene, Or

Re: Trying to get a motor and buzzer to run together. Motor wont run. How do i fix It?

Post by jremington » Fri Nov 10, 2017 2:59 pm

Please edit your post to add code tags, and post a circuit diagram.

Motors must be powered from a separate power supply. Connect all the grounds.

Post Reply