0

Does this program will work on a Uno board
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Does this program will work on a Uno board

by toni_tonio on Wed Dec 12, 2018 7:05 am

Hello

I am wondering if this program could work on an Arduino Uno or Leonardo.

Code: Select all | TOGGLE FULL SIZE
#include <RBD_Button.h>
#include <Adafruit_VS1053.h>

/**************** Button stuff ****************/
//row 1
RBD::Button button_0(0);
RBD::Button button_1(1);
RBD::Button button_2(46);
RBD::Button button_3(47);
//row 2
RBD::Button button_4(44);
RBD::Button button_5(45);
RBD::Button button_6(42);
RBD::Button button_7(43);
//row 3
RBD::Button button_8(40);
RBD::Button button_9(41);
RBD::Button button_10(38);
RBD::Button button_11(39);
//Volume buttons
RBD::Button button_up(35);
RBD::Button button_down(34);
// array of song-selects
RBD::Button button[14] = {button_0, button_1, button_2, button_3, button_4, button_5, button_6, button_7,
  button_8, button_9, button_10, button_11,
  button_up, button_down};

#define OFF_PIN 36


/**************** Button stuff ****************/
#define SHIELD_CS     18      // VS1053 chip select pin (output)
#define SHIELD_DCS    19      // VS1053 Data/command select pin (output)
#define CARDCS 20     // Card chip select pin
#define DREQ 21       // VS1053 Data request, ideally an Interrupt pin
Adafruit_VS1053_FilePlayer musicPlayer = Adafruit_VS1053_FilePlayer(-1, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);


bool playing = false;

#define DELTA_VOLUME 10
#define MAX_VOLUME   0
#define MIN_VOLUME   250
uint8_t volume = 0;

#define INACTIVITY_TIMEOUT 300000 //ms
unsigned long lastAction;

int idleFadingPos = 0;

/** Reset the button pressed state. */
void initButtons() {
  for (int i = 0; i < 12; i++)
    button[i].onReleased();
  button_up.onReleased();
  button_down.onReleased();
}

void setVolume(uint8_t vol) {
  Serial.print("Volume changed to ");
  Serial.println(vol);
  musicPlayer.setVolume(vol, vol);
  volume = vol;

  lastAction = millis();
}

void initialSettings() {
  setVolume(40);
}


void setup() {
  Serial.begin(19200);
  Serial.println("Maya's radio - fading lights mode");

  //Initialize
  if (!musicPlayer.begin()) {
     Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
     while (1);
  }
  SD.begin(CARDCS);
  musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT);

  randomSeed(analogRead(0));

  initButtons();
  initialSettings();

  pinMode(OFF_PIN, OUTPUT);
  lastAction = millis();
}


void play(int number) {
  String track = "track";
  track += (number / 100) % 10;
  track += (number / 10) % 10;
  track += number % 10;
  track += ".mp3";
  Serial.print("Starting to play ");
  Serial.println(track);
  musicPlayer.startPlayingFile(track.c_str());

  lastAction = millis();
}

void loop() {
  // put your main code here, to run repeatedly:

  for (int i = 0; i < 14; i++) { //also map to louder/softer
    if (button[i].onReleased()) {
      musicPlayer.stopPlaying();
      play(i+1);
      playing = true;
    }
  }

  if (playing) {
    lastAction = millis();
    if (musicPlayer.stopped()) {
      Serial.println("Song ended.");
      playing = false; 
    }
  } else {
          Serial.println("Else --- Song ended.");
  }

  unsigned long sinceLast = millis() - lastAction;
  if (sinceLast < 0) lastAction = millis();
  else if (sinceLast > INACTIVITY_TIMEOUT) {
    Serial.println("Turing off..");
    digitalWrite(OFF_PIN, HIGH);
    lastAction = millis();
  }

  delay(50);
}


At the moment on Leonardo:

Code: Select all | TOGGLE FULL SIZE
WARNING: Category 'Device' in library Music Shield is not valid. Setting to 'Uncategorized'
Sketch uses 17824 bytes (62%) of program storage space. Maximum is 28672 bytes.
Global variables use 2102 bytes (82%) of dynamic memory, leaving 458 bytes for local variables. Maximum is 2560 bytes.
Low memory available, stability problems may occur.


And on the Uno:
Code: Select all | TOGGLE FULL SIZE
WARNING: Category 'Device' in library Music Shield is not valid. Setting to 'Uncategorized'
Build options changed, rebuilding all
Sketch uses 15206 bytes (47%) of program storage space. Maximum is 32256 bytes.

Global variables use 2133 bytes (104%) of dynamic memory, leaving -85 bytes for local variables. Maximum is 2048 bytes.
Not enough memory; see http://www.arduino.cc/en/Guide/Troubleshooting#size for tips on reducing your footprint.
Error compiling for board Arduino/Genuino Uno.


1. So I plan to remove the library RBD_Button and right myself the code because it seems to be responsible for the memory size issue?
2. Also, does this program will work on Arduino Uno regardless parallels tasks (playing music VS listen for buttons pressed)?

PS: the project is this one without the leds https://www.thingiverse.com/thing:1480525

toni_tonio
 
Posts: 11
Joined: Mon Dec 10, 2018 5:14 pm

Re: Does this program will work on a Uno board

by kcl1s on Wed Dec 12, 2018 12:53 pm

1. So I plan to remove the library RBD_Button and right myself the code because it seems to be responsible for the memory size issue?
2. Also, does this program will work on Arduino Uno regardless parallels tasks (playing music VS listen for buttons pressed)?

PS: the project is this one without the leds https://www.thingiverse.com/thing:1480525

I think you should be able to run it on an Uno or equivalent without the button library. It looks like it is a memory hog. Be careful which pins you use for buttons as the shield uses 7 of them. Check out the pinout section of the guide. https://learn.adafruit.com/adafruit-music-maker-shield-vs1053-mp3-wav-wave-ogg-vorbis-player/overview You will have to use the analog pins as button pins but you should have enough. There are also 7 separate gpio pins on the shield that you can access for buttons if you want to do something else with the Arduino pins.

Yes you can check the buttons and play in the background. I would abandon the Radio sketch and start with the player_interrupts library example and modify it to play a certain file on button press. That way you know you are starting with code that is working.

EDIT: I just realized you need a couple more pins for volume control so you will need to use some of the shield gpio pins. See the player_gpiotest example for how they work.

Fellow hobbyist
Keith
Last edited by kcl1s on Wed Dec 12, 2018 1:09 pm, edited 1 time in total.

kcl1s
 
Posts: 978
Joined: Tue Aug 30, 2016 12:06 pm

Re: Does this program will work on a Uno board

by toni_tonio on Wed Dec 12, 2018 1:04 pm

Thank you so much Keith

I will follow your recommendations

toni_tonio
 
Posts: 11
Joined: Mon Dec 10, 2018 5:14 pm

Re: Does this program will work on a Uno board

by toni_tonio on Wed Dec 12, 2018 3:36 pm

Code: Select all | TOGGLE FULL SIZE
There are three 'totally fixed' pins, the hardware SPI pins:

    SPI SCK - connected to Digital #13 (but can be connected to the ISP header with a jumper) - used by both the SD card and VS1053
    SPI MISO - connected to Digital #12 (but can be connected to the ISP header with a jumper) - used by both the SD card and VS1053
    SPI MOSI - connected to Digital #11 (but can be connected to the ISP header with a jumper) - used by both the SD card and VS1053

There are a couple other pins that are required for talking to the VS1053 to play MP3s and such

    MCS - this is the VS1053 chip select pin, connected to Digital #7
    DCS - this is the VS1053 data select pin, connected to Digital #6
    CCS - this is the SD Card chip select pin, connected to Digital #4
    DREQ - this is the VS1053 data request interrupt pin - connected to digital #3


But this means I can still use 10-9-8-5-3-2-1-0 ?

I don't know what is the best option, the remaining digital, analog or gpio? I planned many button but I probably need to reduce the amount. Breadboard plan for the project I copy

Image

toni_tonio
 
Posts: 11
Joined: Mon Dec 10, 2018 5:14 pm

Re: Does this program will work on a Uno board

by kcl1s on Wed Dec 12, 2018 11:16 pm

But this means I can still use 10-9-8-5-3-2-1-0 ?

Pin 3 is your interrupt pin but the other 7 are available. The analog pins A0-A5 are also digital 14-19 so that is 6 more for a total of 13. The original design uses 12 selection pins and 2 pins for up and down volume so 14 pins or one short of what you need.

Several ways around this.
-Reduce the number of selections.
-Switch to a potentionmeter on one analog pin for volume control and have 12 selection pins.
-Use the shield inputs as well and have plenty to spare.

Keith

kcl1s
 
Posts: 978
Joined: Tue Aug 30, 2016 12:06 pm

Re: Does this program will work on a Uno board

by kcl1s on Thu Dec 13, 2018 1:00 am

If you are looking for inspiration here is a multi - button, button check function I like to use.

Code: Select all | TOGGLE FULL SIZE
const byte btnPin[] = {0, 1, 2, 5, 8, 9, 10, 14, 15, 16, 17, 18};
bool btnState[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
bool oldState[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
// the setup function runs once when you press reset or power the board
void setup() {
  Serial.begin(9600);
  for (byte i = 0; i < 12; i++) {
    pinMode(btnPin[i], INPUT_PULLUP);
  }
}

void loop() {
  int btnIndex = btnCheck();
  if (btnIndex > -1) {
    Serial.print(btnIndex);
    Serial.println(" highest pressed");
  }
}

int btnCheck() {
  int btnPressed = -1;
  for (int i = 0; i < 12; i++) {
    btnState[i] = digitalRead(btnPin[i]);
    if ((btnState[i] == LOW) && (oldState[i] == HIGH)) {   //We have a new button press
      btnPressed = i;
    }
    oldState[i] = btnState[i];
  }
  delay(50);    //for button debounce
  return btnPressed;
}

I pulled it out of a Simon game sketch I made a while back. I modified it to check 12 of the buttons you have available and returns the pin array index of the pressed pin. As is, it only uses 238 bytes of dynamic memory so maybe a little better than the library in the original.

You could use the index value to insert into the play function from the original project to create your file names.
play(btnIndex);


Code: Select all | TOGGLE FULL SIZE
void play(int number) {
  String track = "track";
  track += (number / 100) % 10;
  track += (number / 10) % 10;
  track += number % 10;
  track += ".mp3";
  Serial.print("Starting to play ");
  Serial.println(track);
  musicPlayer.startPlayingFile(track.c_str());

  lastAction = millis();
}

Good luck

Keith

kcl1s
 
Posts: 978
Joined: Tue Aug 30, 2016 12:06 pm

Re: Does this program will work on a Uno board

by toni_tonio on Thu Dec 13, 2018 1:50 am

Thanks a lot

I did a first experiment with your first answer using GPIO and Analog as input. It works well. I have 11 inputs. I use a variable resistor for audio control so it consumes only one input not two with buttons.
I will definitely try your proposal.

Thanks a lot again

toni_tonio
 
Posts: 11
Joined: Mon Dec 10, 2018 5:14 pm

Re: Does this program will work on a Uno board

by toni_tonio on Mon Dec 24, 2018 8:48 pm

Thanks, the project is done! The code is here https://github.com/benoittgt/hoerbert_hack/blob/master/player_simple_play_stop_button_v2_samples/player_simple_play_stop_button_v2_samples.ino

Image

Image

Image glurp

I still have random white noise that needs to figure it out but otherwise, it works perfectly.

toni_tonio
 
Posts: 11
Joined: Mon Dec 10, 2018 5:14 pm

Re: Does this program will work on a Uno board

by toni_tonio on Tue Jan 01, 2019 12:55 pm

Final project with code and photos https://github.com/benoittgt/hoerbert_hack

toni_tonio
 
Posts: 11
Joined: Mon Dec 10, 2018 5:14 pm

Please be positive and constructive with your questions and comments.