Go Down

Topic: 5 button kidstoy with random sounds (Read 572 times) previous topic - next topic

Koraleck

Hi l have allready made an toy with 5 buttons that lights up an neopixel matrix with different colours. Now l wanna add some sounds into the mix.

I got an Adafruit musicmaker shield. can l combine it and make the toy make sounds as each button is being pushed?

This is the code for the toy verson 1.0

Code: [Select]
#include <Adafruit_NeoPixel.h>

void startShow1(int i);
void startShow2(int i);
void startShow3(int i);
void startShow4(int i);
void startShow5(int i);

// Declare and initialise variable for Neopixel brightness
byte neoBright = 5;

#define BUTTON_PIN1   2

#define BUTTON_PIN2   3

#define BUTTON_PIN3   4

#define BUTTON_PIN4   5

#define BUTTON_PIN5   6

#define PIXEL_PIN    10

#define PIXEL_COUNT 40



Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

bool oldState = HIGH;
int showType1 = 0;
int showType2 = 0;
int showType3 = 0;
int showType4 = 0;
int showType5 = 0;

void setup() {
  pinMode(BUTTON_PIN1, INPUT_PULLUP);
  pinMode(BUTTON_PIN2, INPUT_PULLUP);
  pinMode(BUTTON_PIN3, INPUT_PULLUP);
  pinMode(BUTTON_PIN4, INPUT_PULLUP);
  pinMode(BUTTON_PIN5, INPUT_PULLUP);
  strip.begin();
  strip.setBrightness(neoBright);
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {
  // Get current button state.
  bool newState1 = digitalRead(BUTTON_PIN1);
  bool newState2 = digitalRead(BUTTON_PIN2);
  bool newState3 = digitalRead(BUTTON_PIN3);
  bool newState4 = digitalRead(BUTTON_PIN4);
  bool newState5 = digitalRead(BUTTON_PIN5);

  if (newState1 == LOW && oldState == HIGH) {
    delay(20);
    newState1 = digitalRead(BUTTON_PIN1);
    if (newState1 == LOW) {
      showType1++;
      if (showType1 > 9)
        showType1 = 0;
      startShow1(showType1);
    }
  }
  // Set the last button state to the old state.
  oldState = newState1;


  if (newState2 == LOW && oldState == HIGH) {
    delay(20);
    newState2 = digitalRead(BUTTON_PIN2);
    if (newState2 == LOW) {
      showType2++;
      if (showType2 > 9)
        showType2 = 0;
      startShow2(showType2);
    }
  }
  // Set the last button state to the old state.
  oldState = newState2;


  if (newState3 == LOW && oldState == HIGH) {
    delay(20);
    newState3 = digitalRead(BUTTON_PIN3);
    if (newState3 == LOW) {
      showType3++;
      if (showType3 > 9)
        showType3 = 0;
      startShow3(showType3);
    }
  }
  // Set the last button state to the old state.
  oldState = newState3;

  if (newState4 == LOW && oldState == HIGH) {
    delay(20);
    newState4 = digitalRead(BUTTON_PIN4);
    if (newState4 == LOW) {
      showType4++;
      if (showType4 > 9)
        showType4 = 0;
      startShow4(showType4);
    }
  }
  // Set the last button state to the old state.
  oldState = newState4;

  if (newState5 == LOW && oldState == HIGH) {
    delay(20);
    newState5 = digitalRead(BUTTON_PIN5);
    if (newState5 == LOW) {
      showType5++;
      if (showType5 > 9)
        showType5 = 0;
      startShow5(showType5);
    }
  }
  // Set the last button state to the old state.
  oldState = newState5;
}


void startShow1(int i) {
  switch (i) {
    case 0: colorWipe(strip.Color(0, 0, 0), 50);    // Black/off
      break;
      case 1: colorWipe(strip.Color(127, 127, 127), 50);  // White
      break;
    case 2: theaterChase(strip.Color(127, 127, 127), 50); // White
      break;
  }
}

void startShow2(int i) {
  switch (i) {
    case 0: colorWipe(strip.Color(0, 0, 0), 50);    // Black/off
      break;
    case 1: colorWipe(strip.Color(0, 118, 189), 50);  // Blue
      break;
    case 2: theaterChase(strip.Color(0, 118, 189), 50); // Blue
      break;
  }
}

void startShow3(int i) {
  switch (i) {
    case 0: colorWipe(strip.Color(0, 0, 0), 50);    // Black/off
      break;
    case 1: colorWipe(strip.Color(0, 255, 0), 50);  // Green
      break;
    case 2: theaterChase(strip.Color(0, 255, 0), 50); // Green
      break;
  }
}


void startShow4(int i) {
  switch (i) {
    case 0: colorWipe(strip.Color(0, 0, 0), 50);    // Black/off
      break;
    case 1: colorWipe(strip.Color(255, 204, 0), 50);  // Yellow
      break;
    case 2: theaterChase(strip.Color(255, 204, 0), 50); // Yellow
      break;
  }
}


  void startShow5(int i){
  switch (i) {
    case 0: colorWipe(strip.Color(0, 0, 0), 50);    // Black/off
      break;
    case 1: colorWipe(strip.Color(175, 9, 23), 50);  // Red
      break;
    case 2: theaterChase(strip.Color(175, 9, 23), 50); // Red
      break;
  }
}


// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for (uint16_t i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(80);
  }
}

void rainbow(uint8_t wait) {
  uint16_t i, j;

  for (j = 0; j < 256; j++) {
    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i + j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
  uint16_t i, j;

  for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
  for (int j = 0; j < 12; j++) { //do 10 cycles of chasing
    for (int q = 0; q < 2; q++) {
      for (int i = 0; i < strip.numPixels(); i = i + 2) {
        strip.setPixelColor(i + q, c);  //turn every third pixel on
      }
      strip.show();

      delay(120);

      for (int i = 0; i < strip.numPixels(); i = i + 2) {
        strip.setPixelColor(i + q, 0);      //turn every third pixel off
      }
    }
  }
}

//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
  for (int j = 0; j < 256; j++) {   // cycle all 256 colors in the wheel
    for (int q = 0; q < 3; q++) {
      for (int i = 0; i < strip.numPixels(); i = i + 3) {
        strip.setPixelColor(i + q, Wheel( (i + j) % 255)); //turn every third pixel on
      }
      strip.show();

      delay(wait);

      for (int i = 0; i < strip.numPixels(); i = i + 3) {
        strip.setPixelColor(i + q, 0);      //turn every third pixel off
      }
    }
  }
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if (WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if (WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}


is it possible to make each button play a set for tunes/audio tracks? or its a list of tunes/tracks starting random no matter witch button is being pressed?

Grumpy_Mike

Yes you can do either of those, it is up to you.

To start off just start with the examples that they give you with the shield and when you have changed that to what you want to do add your LEDs.

Start off here https://learn.adafruit.com/adafruit-music-maker-shield-vs1053-mp3-wav-wave-ogg-vorbis-player/play-music

Koraleck

l dont understand how to wire the Shield. from  what l can see is that pin 3, 4. 6. 7 .11. 12 and 13 is "taken" if l stack the Shield on top ?

Grumpy_Mike

Yes the shield uses some pins see https://learn.adafruit.com/adafruit-music-maker-shield-vs1053-mp3-wav-wave-ogg-vorbis-player/pinouts

As your LEDs can be connected to any pins, including the analogue ones, there's lots left over for you to use.

Koraleck

well l must have shit for brain, becasue l dont understand a singel thing.

Grumpy_Mike

OK let's take it a step at a time.

Forget about your toy's LEDs for the moment.

Wire up the shield, as those instructions say. Then load the example code up and get the examples working playing sounds.

The come back for stuff about adding your LEDs. Come back too if you are having trouble with getting the music maker shield to work with the examples.

Koraleck

l¨m geting SD failed, or not present

 l dont have to do the  ICSP Jumpers when l am useing the UNO-controller?

Grumpy_Mike

Quote
dont have to do the  ICSP Jumpers when l am useing the UNO-controller?
Correct.

From my experience on this forum this is almost always due to the wrong sort of SD format being used.

Also you must solder up pin headers, you can't just push them in and it makes contact with the board.

Koraleck

l am useing the stacking heads and they are soled

Grumpy_Mike

#9
Jul 22, 2018, 10:49 am Last Edit: Jul 22, 2018, 10:49 am by Grumpy_Mike
Can you post a picture of your setup please?

Read the how to use this forum sticky post to find out how to do that here.

Koraleck

lam question is are l am suposs to wire between Pin 3(DREQ), Pin 4(CCS), Pin 6(DCS), Pin 7(MCS), Pin 11(MOSI), Pin 12(MISO), Pin 13(SCK) to get the example to work?

Grumpy_Mike

What sort of Arduino do you have. For a Uno the page says
Quote
The Music Maker shield has a bunch of pins required for use. We pre-wire all of them for you but there's still some flexibility in case you want to rewire.
So there is nothing in that respect that you need to do.

For other Arduinos you need to do something more.

Go Up