Sync servo with leds, and buzzer.

Hi all, I’m a total noob, just playing around for a few days now. I’m trying to sync a servo to shift back and forth from 90deg to 90deg with the music notes in this code. This code works fine with the lights, but is it possible to add the servo to this somehow? Thanks!

/*
  "Jingle Bells" Melody

Plays "Jingle Bells"

circuit:
* 8-ohm speaker on digital pin 7
  
*/
#include "pitches.h"

#define led0 B0001
#define led1 B0010
#define led2 B0100
#define led3 B1000

int ledPins[] = { 
  3,6,9,11 };       // an array of pin numbers to which LEDs are attached
int pinCount = 4;           // the number of pins (i.e. the length of the array)

// notes in the melody:
int melody[] = {
  NOTE_D4, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_D4, NOTE_D4, NOTE_D4,
  NOTE_D4, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_E4,
  NOTE_E4, NOTE_C5, NOTE_B4, NOTE_A4, NOTE_FS4,
  NOTE_D5, NOTE_D5, NOTE_C5, NOTE_A4, NOTE_B4, NOTE_D4,
  NOTE_D4, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_D4, NOTE_D4, NOTE_D4,
  NOTE_D4, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_E4, NOTE_E4,
  NOTE_E4, NOTE_C5, NOTE_B4, NOTE_A4, NOTE_D5, NOTE_D5, NOTE_D5, NOTE_D5,
  NOTE_E5, NOTE_D5, NOTE_C5, NOTE_A4, NOTE_G4,
  NOTE_B4, NOTE_B4, NOTE_B4, NOTE_B4, NOTE_B4, NOTE_B4,
  NOTE_B4, NOTE_D5, NOTE_G4, NOTE_A4, NOTE_B4,
  NOTE_C5, NOTE_C5, NOTE_C5, NOTE_C5, NOTE_C5, NOTE_B4, NOTE_B4, NOTE_B4, NOTE_B4,
  NOTE_B4, NOTE_A4, NOTE_A4, NOTE_B4, NOTE_A4, NOTE_D5,
  NOTE_B4, NOTE_B4, NOTE_B4, NOTE_B4, NOTE_B4, NOTE_B4,
  NOTE_B4, NOTE_D5, NOTE_G4, NOTE_A4, NOTE_B4,
  NOTE_C5, NOTE_C5, NOTE_C5, NOTE_C5, NOTE_C5, NOTE_B4, NOTE_B4, NOTE_B4, NOTE_B4,
  NOTE_D5, NOTE_D5, NOTE_C5, NOTE_A4, NOTE_G4 };

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {

  8, 8, 8, 8, 3, 16, 16,
  8, 8, 8, 8, 2,
  8, 8, 8, 8, 2, 
  8, 8, 8, 8, 3, 8,
  8, 8, 8, 8, 3, 16, 16,
  8, 8, 8, 8, 3, 8,
  8, 8, 8, 8, 8, 8, 8, 8,
  8, 8, 8, 8, 2,
  8, 8, 4, 8, 8, 4,
  8, 8, 6, 16, 2,
  8, 8, 6, 16, 8, 8, 8, 16, 16,
  8, 8, 8, 8, 4, 4,
  8, 8, 4, 8, 8, 4, 8, 8, 6, 16, 2,
  8, 8, 6, 16, 8, 8, 8, 16, 16,
  8, 8, 8, 8, 2 };

byte LEDs[]={
   B0010,   B0100,   B1000,   B0001,   B1111,       B0001,    B0001,
   B0001,   B1000,   B0100,   B0010,   B1111,
   B0010,   B1000,   B0100,   B0001,   B1111,
   B0001,   B0100,   B1000,   B0010,   B1111,       B0010,
   B0010,   B0100,   B1000,   B0001,   B1111,       B0001,    B0001,
   B0001,   B1000,   B0100,   B0010,   B1111,       B0010,
   B0010,   B0100,   B1000,   B0001,   B1001,     B0110,    B1100,       B0011,
   B1010,    B0101,    B1010,     B0101,    B1111,
   B0110,    B0110,    B0110,     B1001,    B1001,      B1001,
   B0010,   B0100,   B1000,   B0001,   B1111,
   B0110,    B0110,    B0110,     B0110,    B0110,      B1001,    B1001,       B1001,      B1001,
   B0010,   B0100,   B1000,   B0001,   B0010,    B1111,
   B0110,    B0110,    B0110,     B1001,    B1001,     B1001,
   B0010,   B0100,   B1000,   B0001,   B1111,
   B0110,    B0110,    B0110,     B0110,    B0110,     B1001,     B1001,       B1001,      B1001,
   B1111,       B1111,       B1100,    B0011,    B1111};

void setup() {
  for (int i=0;i<pinCount;i++)
  {
    pinMode(ledPins[i],OUTPUT);
    digitalWrite(ledPins[i], LOW);
  }
  // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 100; thisNote++) {

    // to calculate the note duration, take one second 
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1800/noteDurations[thisNote];
    // I found that by lengthening the noteDuration,
    // I was able to "slow" the song down.
    tone(7, melody[thisNote],noteDuration);
  
    LEDon(thisNote);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    delay(noteDuration);
    LEDoff(thisNote);

    int pauseBetweenNotes = noteDuration * 0.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(7);
  }
}

void loop() {
  // no need to repeat the melody.
}

int currentLEDpin=3;
void LEDon(int thisNote)
{
  if ((LEDs[thisNote]&led0)!=0) digitalWrite(ledPins[0],HIGH);
  if ((LEDs[thisNote]&led1)!=0) digitalWrite(ledPins[1],HIGH);
  if ((LEDs[thisNote]&led2)!=0) digitalWrite(ledPins[2],HIGH);
  if ((LEDs[thisNote]&led3)!=0) digitalWrite(ledPins[3],HIGH);
}

void LEDoff(int thisNote)
{
  if ((LEDs[thisNote]&led0)!=0) digitalWrite(ledPins[0],LOW);
  if ((LEDs[thisNote]&led1)!=0) digitalWrite(ledPins[1],LOW);
  if ((LEDs[thisNote]&led2)!=0) digitalWrite(ledPins[2],LOW);
  if ((LEDs[thisNote]&led3)!=0) digitalWrite(ledPins[3],LOW);
}

"from 90deg to 90deg" does not involve moving at all. That should be very easy. But perhaps you mean something else?

Have a look at the Knob or Sweep example sketches to see what you need to set up for a servo. Basically you need a #include and an attach() command. Then probably two write()s, .write(first position), delay a bit .write(second position).

Steve

Haha 90 to 180 is what I meant. I wanted it to change positions in time with the notes is all. I need to keep reading up on the basics to get a clue of how exactly to write this in. Thanks Steve, I'll keep hacking at it.

It sounds like the servo movements could be co-ordinated with LEDs switching. You could probably just put a write(180) in the LEDon() function and the write(90) in LEDoff().

Steve

Thanks Steve, I think I'm in over my head on this at this point.