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 .
}
}
}