adafruit music player

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
roadshark
 
Posts: 652
Joined: Sun Nov 29, 2015 5:53 am

adafruit music player

Post by roadshark »

Hi Bill,
Hope you are well and cycling in your lovely winter. I seem to having an issue with the music player. In the bridge lifting program the motors are working correctly and lights are also working. Here is the problem. I have a button that lifts the bridge and a button that plays the sound of the bridge lifting. I have a button to lower the bridge and a button to play the bridge lowering sound. The sequence is I initiate the sound then at a designated place in the sound track I push the bridge button. On bridge raising there is no problem with everything working. The issue is that when I initiate the bridge lowering sound and then press the bridge lowering button there is an audible click and the music player and bridge moving all stop. It is like pushing the button shorts everything out yet the bridge button works without the audio and the audio works without the bridge button pressed. I have to disconnect the mega and music player from the power to get the sounds back as a the reset button starts the program correctly but no sounds will work. There is no problem on raising the bridge with sound. I have tried altering the pins on the mega and swapping switches, sound files etc but the problem still is there. What am I missing here?????? Why does the up work but not the down. Hope you can help.

Code: Select all

#include "AccelStepper.h"
// Library created by Mike McCauley at http://www.airspayce.com/mikem/arduino/AccelStepper/

// AccelStepper Setup
AccelStepper stepperLH(AccelStepper::DRIVER, 31, 33);
AccelStepper stepperRH(AccelStepper::DRIVER, 43, 4);
#include <Wire.h> // Enable this line if using Arduino Uno, Meg
#include "Adafruit_GFX.h"

#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>
// These are the pins used for the music maker shield
#define SHIELD_RESET  -1      // VS1053 reset pin (unused!)
#define SHIELD_CS     7      // VS1053 chip select pin (output)
#define SHIELD_DCS    6      // VS1053 Data/command select pin (output)
#define CARDCS 4     // Card chip select pin
#define DREQ 3       // VS1053 Data request, ideally an Interrupt pin
Adafruit_VS1053_FilePlayer musicPlayer =
  // create shield-example object!
  Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);
// Define the Pins used
#define home_switchLH 22 // Pin 22 connected to Home Switch (MicroSwitch)
#define home_switchRH 24// Pin 24 connected to Home switch
#define bridgeDeckUp 26// Raise the bridge deck
#define bridgeDeckDown 28// lowers deck
#define bridgeDeckUpSound 30//plays sound when ship enters port
#define bridgeDeckDownSound 32//plays sound
#define bridgeDeckUpSound2 42//plays sound when ship leaving port
int outPutPins[] = {23, 25, 27, 29, 34, 35, 36, 37, 38,  39, 40, 41};// out put pins (23, 25, 33,35 used to set step rate on the MP6500.
//Pins 27, 39 (used to set current limit)pin 29/41 are sleep pins. LEDS  34 train red light, 38 Bridge red light, 36 train green light. 40 bridge green light
int pinCount = 12;
int buttonState = digitalRead(bridgeDeckDown);
// Stepper Travel Variables
long initial_homing = -1; // Used to Home Stepper at startup
//blinking the bridge red led.
boolean blinkingRedled = false;
int ledState = LOW; // ledState used to set the LED.
unsigned long previousMillis = 0; // will store last time LED was updated.
const long interval = 800; // interval at which to blink (milliseconds)

void setup() {
  Serial.begin(9600);
  SD.begin(CARDCS);    // initialise the SD card
  if (! musicPlayer.begin()) { // initialise the music player
    Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
    while (1);
  }


  musicPlayer.setVolume(0, 0);// Set volume for left, right channels. lower numbers == louder volume!

  musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT);


  for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    pinMode(outPutPins[thisPin], OUTPUT);
  }

  pinMode (bridgeDeckUp, INPUT_PULLUP);
  pinMode (bridgeDeckDown, INPUT_PULLUP);
  pinMode (bridgeDeckUpSound, INPUT_PULLUP);
  pinMode (bridgeDeckUpSound2, INPUT_PULLUP);
  pinMode (bridgeDeckDownSound, INPUT_PULLUP);
  pinMode(home_switchLH, INPUT_PULLUP);
  pinMode(home_switchRH, INPUT_PULLUP);
  digitalWrite (29, HIGH);// Enables the sleep pin on the MP6500/stepperLH
  digitalWrite (41, HIGH);// Enables the sleep pin on the MP6500/stepperRH
  digitalWrite (27, LOW);// StepperLH set the voltage
  digitalWrite (39, LOW);// StepperRH
  // Pins to drive steps
  //Change to HIGH OR LOW designates step rate pin 23 and 25 are for LHstepper 35 and 37 for RHstepper To speed up homing set to full step.
  digitalWrite (23, HIGH);
  digitalWrite (25, LOW);
  digitalWrite (35, HIGH);
  digitalWrite (37, LOW);

  //  Set Max Speed  of each Steppers at startup for homing
  stepperLH.setMaxSpeed(40.0);
  stepperRH.setMaxSpeed(40.0);// Set Max Speed of Stepper (Slower to get better accuracy)

  // Start Homing procedure of Stepper Motor at startup

  while (digitalRead(home_switchLH) || digitalRead(home_switchRH))
  { // Move motors towards home until both switches are activated
    if (digitalRead(home_switchLH))
    {
      stepperLH.moveTo(initial_homing);// homing switch on this motor should stop both motors
      stepperLH.run();
    }
    if (digitalRead(home_switchRH))
    {
      stepperRH.moveTo(initial_homing);// Set the position to move to
      stepperRH.run();
    }
    initial_homing--;  // Decrease by 1 for next move if needed
  }



  stepperLH.setCurrentPosition(0);  // Set the current position as zero for now
  stepperRH.setCurrentPosition(0);
  stepperLH.setMaxSpeed(25.0);      // Set Max Speed of Stepper (Slower to get better accuracy)
  stepperRH.setMaxSpeed(25.0);
  digitalWrite (23, HIGH);//Sets the step rate high for accuracy sgtep set at one eighth.
  digitalWrite (25, HIGH);
  digitalWrite (35, HIGH);
  digitalWrite (37, HIGH);

  initial_homing = 0;

  // Move motors AWAY from home until both switches are deactivated and set position to 0.

  while (!digitalRead(home_switchLH) || !digitalRead(home_switchRH))
  {
    if (!digitalRead(home_switchLH))
    {
      stepperLH.moveTo(initial_homing);// homing switch on this motor should stop both motors
      stepperLH.run();
    }
    if (!digitalRead(home_switchRH))
    {
      stepperRH.moveTo(initial_homing);// Set the position to move to
      stepperRH.run();
    }
    initial_homing++;  // Increase by 1 for next move if needed
  }

  stepperLH.setCurrentPosition(0);
  stepperRH.setCurrentPosition(0);

  digitalWrite (29, LOW);// Disables the sleep pin on the MP6500/stepperLH
  digitalWrite (41, LOW);// Disables the sleep pin on the MP6500/stepperRH
  Serial.println(stepperRH.currentPosition());
  Serial.println(stepperLH.currentPosition());
  //Set the warning lights.  (Pin 40)is HIGH and train red light 38 is LOW) Bridge red light 39 is HIGH and bridge green light 41 is LOW.
  digitalWrite (36, HIGH);//Bridge is down so Train light is green
  digitalWrite (40, LOW); //Bridge is down so bridge green light is off
  digitalWrite (38, HIGH);//Bridge is down so bridge light is red indicating to ships not to proceed.
  digitalWrite (34, LOW);// Bridge is down so train red light is off.
}



void loop() {

  if (!digitalRead(bridgeDeckUpSound)) {
    musicPlayer.startPlayingFile("bridgeup.mp3");//Bridge sound effects siren and then electic motors
  }
  //if (!digitalRead(bridgeDeckUpSound2)) {
  // musicPlayer.startPlayingFile("bridgeU2.mp3");//Bridge sound effects siren and then electic motors
  //}
  if (!digitalRead(bridgeDeckDownSound)) {
    musicPlayer.startPlayingFile("bridgedn.mp3");
  }

  // Serial.println(buttonState);



  if ((!digitalRead(bridgeDeckUp)) && (stepperLH.distanceToGo() == 0))
  {
    blinkingRedled = true;
    digitalWrite (36, LOW);//Turns off green train light as bridge is about to lift.
    digitalWrite (34, HIGH);//Turns on red train light as bridge is lifting.
    digitalWrite (29, HIGH);// Enables the sleep pin on the MP6500/stepperLH
    digitalWrite (41, HIGH);// Enables the sleep pin on the MP6500/stepperRH

    stepperLH.setMaxSpeed(40.0);
    stepperLH.setAcceleration(25.0);
    stepperRH.setMaxSpeed(40.0);
    stepperRH.setAcceleration(25.0);
    stepperLH.moveTo(800);//Deck moves to top and stops.
    stepperRH.moveTo(800);
    while ((stepperLH.distanceToGo() != 0) || (stepperRH.distanceToGo() != 0))
    {

      stepperRH.run();
      stepperLH.run();
      if (blinkingRedled == true) {
        unsigned long currentMillis = millis();
        if (currentMillis - previousMillis >= interval) {

          previousMillis = currentMillis;

          if (ledState == LOW) {
            ledState = HIGH;
          } else {
            ledState = LOW;
          }
          digitalWrite(38, ledState);// Blinks red bridge light.set the LED with the ledState of the variable:
        }
      }
      // Serial.print(stepperLH.currentPosition());
      // Serial.print(" - ");
      //Serial.println(stepperRH.currentPosition());
    }
    Serial.println("Bridge is UP!");
  }
  //Bridge deck at top so turn off motors.

  digitalWrite (29, LOW);// Enables the sleep pin on the MP6500/stepperLH
  digitalWrite (41, LOW);// Enables the sleep pin on the MP6500/stepperRH

  if (stepperLH.currentPosition() == 800) {
    blinkingRedled = false; //turns of bridge blinking red light
    digitalWrite (34, HIGH);//Turns on Red train LED
    digitalWrite (38, LOW);//Turns off Red Bridge LED
    digitalWrite (40, HIGH);//Turns on Green Bridge LED.

  }
  Serial.println(buttonState);

  
  //MOVES THE BRIDGE DECK DOWN TO THE BOTTOM



  if ((!digitalRead(bridgeDeckDown)) && (stepperLH.currentPosition() == 800))
  {
    //Serial.println(buttonState);
    digitalWrite (38, HIGH);//Turns on Red Bridge LED
    digitalWrite (40, LOW);//Turns off Green Bridge LED.

    digitalWrite (29, HIGH);// Enables the sleep pin on the MP6500/stepperLH
    digitalWrite (41, HIGH);// Enables the sleep pin on the MP6500/stepperRH

    stepperLH.setMaxSpeed(50.0);
    stepperLH.setAcceleration(30.0);
    stepperRH.setMaxSpeed(50.0);
    stepperRH.setAcceleration(30.0);
    stepperLH.moveTo(0);//Deck moves to bottom and stops.
    stepperRH.moveTo(0);
    while ((stepperLH.distanceToGo() != 0) || (stepperRH.distanceToGo() != 0))
    {
      stepperRH.run();
      stepperLH.run();



    }
    if (stepperLH.distanceToGo() == 0) {

      //Bridge deck at bottom so turn off motors.
      Serial.println("Bridge is DOWN!");
      digitalWrite (29, LOW);// Enables the sleep pin on the MP6500/stepperLH
      digitalWrite (41, LOW);// Enables the sleep pin on the MP6500/stepperRH

      digitalWrite (36, HIGH);//Bridge is down so Train light is green
      digitalWrite (34, LOW); //Bridge is down so train light is red
      digitalWrite (38, HIGH);//Bridge is down so bridge light is red indicating to ships not to proceed.
      digitalWrite (40, LOW);// Bridge is down so bridge light green is off .
    }

  }
}

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: adafruit music player

Post by adafruit_support_bill »

Strange. Looking at the code I don't see anything suspicious.

Just to be clear: If you press the bridgeDeckDown button when the sound is NOT playing, the bridge moves normally?

If you press the bridgeDeckDown button when the sound IS playing:
* is there any sign of the bridge starting to move?
* do the lights change to the appropriate color?
* if you measure the stepper enable pins (29 & 41) do they go HIGH?

User avatar
roadshark
 
Posts: 652
Joined: Sun Nov 29, 2015 5:53 am

Re: adafruit music player

Post by roadshark »

Just to be clear: If you press the bridgeDeckDown button when the sound is NOT playing, the bridge moves normally?
Yes that is correct.
is there any sign of the bridge starting to move?[/quote
I hear the motors ticking, like taking a step, but there is no movement.

Code: Select all

 do the lights change to the appropriate color?
Yes the lights change as soon as I press the bridge down button.
if you measure the stepper enable pins (29 & 41) do they go HIGH?
not sure how to do this Bill? Serial read the pin?
Will take a video and give you he url.

User avatar
roadshark
 
Posts: 652
Joined: Sun Nov 29, 2015 5:53 am

Re: adafruit music player

Post by roadshark »


User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: adafruit music player

Post by adafruit_support_bill »

You should be able to measure the state of your motor enable pins (29 & 41) with a multimeter. If you are getting some noise from the motors, I'd expect that they are HIGH as intended. Assuming all that checks out, it rules out any pin conflict issues.

Electrical interference from the motors would be another possibility, but it looks like your motor wiring is well separated from the logic wiring. And those conditions are identical for both up and down motions.

I think we can rule out wiring issues as well. The only difference between the up & down is the buttons. And we know that the buttons are working.

I'd try adding some serial output just before and just after the stepper run() commands in your loop. That should tell us if we are actually getting into the loop.

Just finished rebuilding the drivetrain on my fair-weather road bike. Weather is looking good for a test ride.

User avatar
roadshark
 
Posts: 652
Joined: Sun Nov 29, 2015 5:53 am

Re: adafruit music player

Post by roadshark »

Hope the ride went well. It was 37 degrees c here today with a 50 + klmh North wind. I heard the wind chimes and saw the trees bending over so I pulled the plug on a ride. There is always tomorrow now I am a man of leisure!!
I ended up swapping over the mega and musicplayer and switches with new ones. No luck:( Still runs into the same problem, as soon as I push the down button, if the music player is playing, everything stops. I noticed on the voltage step down converter, when I push the button, that there is a voltage drop (same reading I get when motors are working) so I assume that the have been turned on which is meant to happen when the code enters the bridge down button loop. I am thinking that there has to be some sort of pin conflict issue (with the musicplayer). I wired the Mega trying to keep the wiring hook ups neat, keeping the motor drivers on one side from pins 23 - 45 and the switches and LEDs on the other side 22 - 40.
What is frustrating (like the pic I sent, 2 in two days different bikes) is the up button is fine, motors working with sound. Even if I change the pin numbers of the two buttons still the same problem.
Aside from that, when I push a button, the music player starts, then starts again. Obviously it is reading the pin quickly so it needs to be debounced. I have two pins that need this. I looked at the examples in arduino using millis but I do remember you used a 'while' command that has the same effect???

Code: Select all

{

  if (!digitalRead(soundFreighter)) {
    musicPlayer.startPlayingFile("shiphrn2.mp3");//Ship sound effects.



  }
Attachments
20230302_073548.jpg
20230302_073548.jpg (91.21 KiB) Viewed 150 times
20230304_115703.jpg
20230304_115703.jpg (111.74 KiB) Viewed 150 times

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: adafruit music player

Post by adafruit_support_bill »

After 3 months of fat knobbies and heavy studded tires, yesterday's ride felt like flying.

Mind you, the studs in my winter tires are pretty small compared to the ones in your photos. Looks like you've been riding through a hardware store! I hope it didn't cause any damage to your frame.

Some serial print statements in your deck-down loop will tell you if it is actually getting into the loop. I'd put one before and one after the run() commands.

And is your motor power separate from the power source for the rest of your circuitry?

User avatar
roadshark
 
Posts: 652
Joined: Sun Nov 29, 2015 5:53 am

Re: adafruit music player

Post by roadshark »

didn't cause any damage to your frame.
Yep it took a chip out of the 'Golden Boy Derosa' :( Some nail polish took care of it.
is your motor power separate from the power source for the rest of your circuitry?
Yes.
However success! I pulled everything out and replaced it and handled calling the sound file slightly differently.
All good now. As always thanks for your support. Still have a couple of issues. One is how to debounce the button that starts the music player. (Previous Post) and I thought I need a fail safe if the bridge when lowering goes passed 0 and hits the bottom with the motors still turning. I tried this

Code: Select all

if(!digitalRead(home_switchLH) || !digitalRead(home_switchRH))
{ digitalWrite (29, LOW);// Disables the sleep pin on the MP6500/stepperLH
  digitalWrite (41, LOW);// Disables the sleep pin on the MP6500/stepperRH
  {
I placed it inside the stepper running loop but it just stopped the motors straight away even when the switches were not pushed. I also tried it at the start of the loop itself but same problem. Have I use this || and this ! correctly.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: adafruit music player

Post by adafruit_support_bill »

took a chip out of the 'Golden Boy Derosa'
DeRosa makes a fine machine. Used to ride one of Ugo's track bikes back in my racing days.
I pulled everything out and replaced it and handled calling the sound file slightly differently.
All good now.
I'd be interested to see your revised code. I'm still puzzled about why the original code acted so strangely.
Have I use this || and this ! correctly.
looks right to me. If the switch is open and pullups enabled, both reads should return HIGH (TRUE). The ! takes precedence over the ||, so both terms should evaluate to FALSE. So the whole expression should be FALSE as well.

I'd start by verifying that the switch reads are indeed reading HIGH.

User avatar
roadshark
 
Posts: 652
Joined: Sun Nov 29, 2015 5:53 am

Re: adafruit music player

Post by roadshark »

I'd be interested to see your revised code.

Code: Select all

#include "AccelStepper.h"
// Library created by Mike McCauley at http://www.airspayce.com/mikem/arduino/AccelStepper/

// AccelStepper Setup
AccelStepper stepperLH(AccelStepper::DRIVER, 31, 33);
AccelStepper stepperRH(AccelStepper::DRIVER, 43, 45);
#include <Wire.h> // Enable this line if using Arduino Uno, Meg
#include "Adafruit_GFX.h"

#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>
// These are the pins used for the music maker shield
#define SHIELD_RESET  -1      // VS1053 reset pin (unused!)
#define SHIELD_CS     7      // VS1053 chip select pin (output)
#define SHIELD_DCS    6      // VS1053 Data/command select pin (output)
#define CARDCS 4     // Card chip select pin
#define DREQ 3       // VS1053 Data request, ideally an Interrupt pin
Adafruit_VS1053_FilePlayer musicPlayer =
  // create shield-example object!
  Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);
// Define the Pins used
#define home_switchLH 22 // Pin 42 connected to Home Switch (MicroSwitch)
#define home_switchRH 24// Pin 44 connected to Home switch
#define bridgeDeckUp 26// Raise the bridge deck
#define bridgeDeckDown 28// lowers deck
#define soundFreighter 30//plays sound
#define soundTug 32//plays sound
int outPutPins[] = {23, 25, 27, 29, 35, 37, 39, 41, 34, 36, 38, 40};// out put pins (22, 24, 26,28 used to set step rate on the MP6500.
//Pins 25 and 37 used to set current limit)pin 30/32 are sleep pins. LEDS  38 train red light, 39 Bridge red light, 40 train green light. 41 bridge green light
int pinCount = 12;
int buttonState = digitalRead(bridgeDeckDown);
// Stepper Travel Variables
long initial_homing = -1; // Used to Home Stepper at startup
//blinking the bridge red led.
boolean blinkingRedled = false;
int ledState = LOW; // ledState used to set the LED.
unsigned long previousMillis = 0; // will store last time LED was updated.
const long interval = 500; // interval at which to blink (milliseconds)





void setup() {
  Serial.begin(9600);
  SD.begin(CARDCS);    // initialise the SD card
  if (! musicPlayer.begin()) { // initialise the music player
    Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
    while (1);
  }


  musicPlayer.setVolume(0, 0);// Set volume for left, right channels. lower numbers == louder volume!

  musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT);


  for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    pinMode(outPutPins[thisPin], OUTPUT);
  }

  pinMode (bridgeDeckUp, INPUT_PULLUP);
  pinMode (bridgeDeckDown, INPUT_PULLUP);
  pinMode (soundFreighter, INPUT_PULLUP);
  pinMode (soundTug, INPUT_PULLUP);
  pinMode(home_switchLH, INPUT_PULLUP);
  pinMode(home_switchRH, INPUT_PULLUP);
  digitalWrite (29, HIGH);// Enables the sleep pin on the MP6500/stepperLH
  digitalWrite (41, HIGH);// Enables the sleep pin on the MP6500/stepperRH
  digitalWrite (27, LOW);// StepperLH set the voltage
  digitalWrite (39, LOW);// StepperRH
  // Pins to drive steps
  //Change to HIGH OR LOW designates step rate pin 22 and 24 are for LHstepper 26 and 28 for RHstepper To speed up homing set to full step.
  digitalWrite (23, HIGH);
  digitalWrite (25, LOW);
  digitalWrite (35, HIGH);
  digitalWrite (37, LOW);

  //  Set Max Speed  of each Steppers at startup for homing
  stepperLH.setMaxSpeed(40.0);
  stepperRH.setMaxSpeed(40.0);// Set Max Speed of Stepper (Slower to get better accuracy)

  // Start Homing procedure of Stepper Motor at startup

  while (digitalRead(home_switchLH) || digitalRead(home_switchRH))
  { // Move motors towards home until both switches are activated
    if (digitalRead(home_switchLH))
    {
      stepperLH.moveTo(initial_homing);// homing switch on this motor should stop both motors
      stepperLH.run();
    }
    if (digitalRead(home_switchRH))
    {
      stepperRH.moveTo(initial_homing);// Set the position to move to
      stepperRH.run();
    }
    initial_homing--;  // Decrease by 1 for next move if needed
  }



  stepperLH.setCurrentPosition(0);  // Set the current position as zero for now
  stepperRH.setCurrentPosition(0);
  stepperLH.setMaxSpeed(25.0);      // Set Max Speed of Stepper (Slower to get better accuracy)
  stepperRH.setMaxSpeed(25.0);
  digitalWrite (23, HIGH);//Sets the step rate high for accuracy sgtep set at one eighth.
  digitalWrite (25, HIGH);
  digitalWrite (35, HIGH);
  digitalWrite (37, HIGH);

  initial_homing = 0;

  // Move motors AWAY from home until both switches are deactivated and set position to 0.

  while (!digitalRead(home_switchLH) || !digitalRead(home_switchRH))
  {
    if (!digitalRead(home_switchLH))
    {
      stepperLH.moveTo(initial_homing);// homing switch on this motor should stop both motors
      stepperLH.run();
    }
    if (!digitalRead(home_switchRH))
    {
      stepperRH.moveTo(initial_homing);// Set the position to move to
      stepperRH.run();
    }
    initial_homing++;  // Increase by 1 for next move if needed
  }

  stepperLH.setCurrentPosition(0);
  stepperRH.setCurrentPosition(0);

  digitalWrite (29, LOW);// Disables the sleep pin on the MP6500/stepperLH
  digitalWrite (41, LOW);// Disables the sleep pin on the MP6500/stepperRH
  Serial.println(stepperRH.currentPosition());
  Serial.println(stepperLH.currentPosition());
  //Set the warning lights.
  digitalWrite (36, HIGH);//Bridge is down so Train light is green
  digitalWrite (34, LOW); //Bridge is down so Train red light is off
  digitalWrite (38, HIGH);//Bridge is down so bridge red light is on indicating ships not to proceed.
  digitalWrite (40, LOW);// Bridge is down so bridge green light is off.
}



void loop() {


  if (!digitalRead(soundFreighter)) {
    musicPlayer.startPlayingFile("shiphrn2.mp3");//Bridge sound effects siren and then electic motors



  }
  if (!digitalRead(soundTug)) {
    musicPlayer.startPlayingFile("shiphrn3.mp3");


  }

  Serial.println(buttonState);
  if (!digitalRead(bridgeDeckUp)) {

    blinkingRedled = true;
    digitalWrite (36, LOW);//Turns off green train light as bridge is about to lift.
    digitalWrite (34, HIGH);//Turns on red train light as bridge is lifting.
    digitalWrite (29, HIGH);// Enables the sleep pin on the MP6500/stepperLH
    digitalWrite (41, HIGH);// Enables the sleep pin on the MP6500/stepperRH

  }
  if ((!digitalRead(bridgeDeckUp)) && (stepperLH.distanceToGo() == 0))
  {
    musicPlayer.startPlayingFile("br_lift1.mp3");
    delay (8000);  //Gives time for warning sound to start before bridge motors begin to lift bridge
    stepperLH.setMaxSpeed(100.0);
    stepperLH.setAcceleration(30.0);
    stepperRH.setMaxSpeed(100.0);
    stepperRH.setAcceleration(30.0);
    stepperLH.moveTo(3400);//Deck moves to top and stops.
    stepperRH.moveTo(3400);
    while ((stepperLH.distanceToGo() != 0) || (stepperRH.distanceToGo() != 0))
    {
      

      
      stepperRH.run();
      stepperLH.run();
      if (blinkingRedled == true) {
        unsigned long currentMillis = millis();
        if (currentMillis - previousMillis >= interval) {

          previousMillis = currentMillis;

          if (ledState == LOW) {
            ledState = HIGH;
          } else {
            ledState = LOW;
          }
          digitalWrite(38, ledState);// Blinks red bridge light.set the LED with the ledState of the variable:
        }
      }

    }

  }
  //BRIDGE DECK IS UP

  digitalWrite (29, LOW);// Enables the sleep pin on the MP6500/stepperLH
  digitalWrite (41, LOW);// Enables the sleep pin on the MP6500/stepperRH

  if (stepperLH.currentPosition() == 3400) {
    blinkingRedled = false; //turns of bridge blinking red light
    digitalWrite (34, HIGH);//Turns on Red train LED
    digitalWrite (38, LOW);//Turns off Red Bridge LED
    digitalWrite (40, HIGH);//Turns on Green Bridge LED.

  }


  //BRIDGE DECK DOWN


  if ((!digitalRead(bridgeDeckDown)) && (stepperLH.currentPosition() == 3400))
  {

    digitalWrite (38, HIGH);//Turns on Red Bridge LED
    digitalWrite (40, LOW);//Turns off Green Bridge LED.
    blinkingRedled = true;

    digitalWrite (29, HIGH);// Enables the sleep pin on the MP6500/stepperLH
    digitalWrite (41, HIGH);// Enables the sleep pin on the MP6500/stepperRH
    musicPlayer.startPlayingFile("br_lift1.mp3");
    delay (8000);//Gives time for warning sound to start before bridge motors begin to lift bridge
    stepperLH.setMaxSpeed(100.0);
    stepperLH.setAcceleration(30.0);
    stepperRH.setMaxSpeed(100.0);
    stepperRH.setAcceleration(30.0);
    stepperLH.moveTo(0);//Deck moves to bottom and stops.
    stepperRH.moveTo(0);
    while ((stepperLH.distanceToGo() != 0) || (stepperRH.distanceToGo() != 0))
    {
      stepperRH.run();
      stepperLH.run();
      if (blinkingRedled == true) {
        unsigned long currentMillis = millis();
        if (currentMillis - previousMillis >= interval) {

          previousMillis = currentMillis;

          if (ledState == LOW) {
            ledState = HIGH;
          } else {
            ledState = LOW;
          }
          digitalWrite(38, ledState);// Blinks red bridge light.set the LED with the ledState of the variable:
        }
      }

    }
    if (stepperLH.distanceToGo() == 0) {

      //Bridge deck at bottom so turn off motors.
      Serial.println("Bridge is DOWN!");
      digitalWrite (29, LOW);// Enables the sleep pin on the MP6500/stepperLH
      digitalWrite (41, LOW);// Enables the sleep pin on the MP6500/stepperRH
      //musicPlayer.stopPlaying();
      digitalWrite (36, HIGH);//Bridge is down so Train light is green
      digitalWrite (40, LOW); //Bridge is down so bridge green light is off
      digitalWrite (38, HIGH);//Bridge is down so bridge light is red indicating to ships not to proceed.
      digitalWrite (34, LOW);// Bridge is down so train red light is off.
    }

  }
}

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: adafruit music player

Post by adafruit_support_bill »

You've eliminated the need for a second button press. But I still don't see what was causing it to fail before. In any case, it sound like you are back on track with your bridge motion.

Sorry I missed your question about debouncing. One technique I use often is a while loop to wait for the button release:

Code: Select all

 if (!digitalRead(soundFreighter)) 
 {
	 while (!digitalRead(soundFreighter)) {} // wait for button release

User avatar
roadshark
 
Posts: 652
Joined: Sun Nov 29, 2015 5:53 am

Re: adafruit music player

Post by roadshark »

Have placed the fail safe code into the down bridge loop and it works:) I will send you a video of the bridge when it is working. Thank you for your code for the button press. Keep upright on the bike.

User avatar
roadshark
 
Posts: 652
Joined: Sun Nov 29, 2015 5:53 am

Re: adafruit music player

Post by roadshark »

Hi Bill,
Hope this finds you well. On my ride today I met one of our local wildlife inhabitants on the road and he was not about to give way. Had to brake hard and saw the whites of his eyes before he weaved off. (Kangaroo). At least we have nothing in Australia that can eat you. (Bears and cougars??). Well I have now connected the motors to the bridge. I have an issue in that the bridge has trouble homing on the switches. If I manually press the switches everything works well as you can see in the video.(Sorry for the shaky images:( Still recovering from the Kangaroo scare) So the code is working fine. I think the issue is the bridge is hanging on the channel it slides down at the bottom I think. I have used push button switches for homing switches (They are the lever ones you have and I removed the levers to just use the little button.) I have also used pogo pins to provide power to the train track. So the bridge hits these first then because of the springs, the bridge keep descending until it hits the homing switch. The idea is that the motors move the bridge back off the home switch with the pogo pins pushing the bridge up (as well as the counter weights) but still keep in contact with the train track to provide power. I am thinking of maybe using an analogue pressure switch if I find the issue is with the bridge homing switches?
In this piece of code, I am I correct in my assumption that the program will stay in this loop until both switches are pressed. If one switch is pressed that motor stop whilst other motor will keep working until it hits the home switch?

Code: Select all

 while (digitalRead(home_switchLH) || digitalRead(home_switchRH))
  { // Move motors towards home until both switches are activated
    if (digitalRead(home_switchLH))
    {
      stepperLH.moveTo(initial_homing);
      stepperLH.run();
    }
    if (digitalRead(home_switchRH))
    {
      stepperRH.moveTo(initial_homing);// Set the position to move to
      stepperRH.run();
    }
    initial_homing--;  // Decrease by 1 for next move if needed
  }
https://www.youtube.com/watch?v=BU5aaCpujiI

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: adafruit music player

Post by adafruit_support_bill »

The bridge is looking good! The motion & sound is very smooth and realistic.

That code should loop until both switches close. Is your cable system pulling in both the up and down directions, or does it rely on gravity to bring the bridge back down? If only gravity, the pogo springs will offset the weight of the bridge before it contacts the home switches.

Only spotted a bear once while out on the MTB. But it was at a safe distance away. Deer are the more common collision hazard around here.

User avatar
roadshark
 
Posts: 652
Joined: Sun Nov 29, 2015 5:53 am

Re: adafruit music player

Post by roadshark »

Is your cable system pulling in both the up and down directions
The motors pull it down and counter weights pull it up. The motors turn and unwind the cable but its the counter weights that move the bridge up. When the motors are pulling the bridge down I think they are powerful enough to overcome the force of the pogo springs. I will take a video of what happens on the homing phase. Is this how I would substitute a pressure sensor into the code. Not sure about how to if ==0 is correct syntax:)

Code: Select all

nt homeSensorL_H = 0;// pressure sensor connected to anolog pin 0
int homeSensorR_H = 1;// pressure sensor connected to anolog pin 1

while ((analogRead(homeSensorL_H) == 0) || (analogRead(homeSensorR_H) == 0))
  { // Move motors towards home until both pressure sensors are activated
    if (analogRead(homeSensorL_H) == 0)
    {
      stepperLH.moveTo(initial_homing);
      stepperLH.run();
    }
    if (analogRead(homeSensorR_H) == 0)
    {
      stepperRH.moveTo(initial_homing);// Set the position to move to
      stepperRH.run();
    }
    initial_homing--;  // Decrease by 1 for next move if needed
  }
}
 while ((analogRead(homeSensorL_H)>0) || (analogRead(homeSensorR_H) > 0))//sensor been pressed
  {
    if ((analogRead(homeSensorL_H)>0)
    {
      stepperLH.moveTo(initial_homing);//
      stepperLH.run();
    }
    if ((analogRead(homeSensorR_H)>0)
    {
      stepperRH.moveTo(initial_homing);// Set the position to move to
      stepperRH.run();
    }
    initial_homing++;  // Increase by 1 for next move if needed
  }

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”