Arduino Uno - A Sound, LED & Motor Issues using On/Off/Hold Switches

G'day everyone,

I'm a new guy on the Arduino topic and keen to learn, but I seem to have run into a stop sign with my project.

Overview:
I built a Star Wars inspired Rocking Speeder bike Instructables Link for my son, which has two On/Off Buttons wich shall do different things.

Button 1
Play mp3 file from SD card (blaster sound) using DFplayer and Flash 1 LED while that button is pressed

Button 2
Play mp3 file from same SD card (turbine sound) using DFplayer and make my little dc motor spin my 3D printed turbine.

I tried to search for individual codes that do each trick and somehow merge them into one functional frankensteins monster, but for a beginners project, this seems to be a little over my head.

I hope you can help me out and make that rocking speeder bike a little more awesome.

Here come(s) the code(s) pieces so far

**Flashing LED when Button (not one of those push buttons on a breadboard) is pressed ** (source )

int switch_pin = 4;
int led_pin = 5;

byte leds = 0;

void setup() {
  pinMode(switch_pin, INPUT);
  pinMode(led_pin, OUTPUT);
}

void loop() {
  if(digitalRead(switch_pin) == HIGH){
    digitalWrite(led_pin, HIGH);
  }
  if(digitalRead(switch_pin) == LOW){
    digitalWrite(led_pin, LOW);
  }
}

edit 2: example circuit deleted

Play mp3 from DFplayer when On/Off switch is turned on German Source

#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
int taster=7; 
int tasterstatus=0; //Das Wort „tasterstatus“ steht jetzt zunächst für den Wert 0. Später wird unter dieser Variable gespeichert, ob der Taster gedrückt ist oder nicht.

void setup() 
{
  mySoftwareSerial.begin(9600);
  myDFPlayer.volume(30); 
 pinMode(taster, INPUT); 

}

void loop() 
{
 tasterstatus=digitalRead(taster); 
  if (tasterstatus == HIGH) {
  myDFPlayer.play(1);  
  } 
}

My combination so far:

#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;

int turbine_switch = 4;
int blaster_switch = 5;
int blaster_led = 6;
int motorin2 = 7;
int motorin1 = 8;
int enA = 9;

void setup()
{
  mySoftwareSerial.begin(9600);
  myDFPlayer.volume(30);
  pinMode(turbine_switch, INPUT);
  pinMode(blaster_switch, INPUT);
  pinMode(blaster_led, OUTPUT);
  pinMode(enA, OUTPUT);
  pinMode(motorin1, OUTPUT);
  pinMode(motorin2, OUTPUT);
}

void loop()
{
  if (digitalRead(blaster_switch) == HIGH) {
    myDFPlayer.play(1);
    digitalWrite(blaster_led, HIGH);
    delay(100);
    digitalWrite(blaster_led, LOW);
  }
  else {
    digitalWrite(blaster_led, LOW);
  }

  if (digitalRead(turbine_switch) == HIGH) {
    myDFPlayer.play(2);
    digitalWrite(motorin1, HIGH);
    digitalWrite(motorin2, LOW);
    analogWrite(enA, 200);
  }
}

Then comes moving the motor. Do I need a shield for this? I don't get it. I saw its possible without, but a lot of tutorials seem to love these things. Can somebody enlighten me please?

edit1
Alright, I found a tutorial about the L298N here and included the move-my-dc-motor-code. I'm curious about your comments.

cheers

The first and most obvious problem is that your diagram does not match the code

Hey, sorry, i thought this might be confusing. This was just the diagram of the first code i tried to incorporate. I'll delete it and add my own circuit to the end.

Also, don't use pins 0 and 1 like the diagram.

Good morning. So this is my take on the circuit, please let me know if this would work out! Thank you so much for your help. It is greatly appreciated.

Code:

#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;

int turbine_switch = 4;
int blaster_switch = 5;
int blaster_led = 6;
int motorin2 = 7;
int motorin1 = 8;
int enA = 9;

void setup()
{
  mySoftwareSerial.begin(9600);
  myDFPlayer.volume(30);
  pinMode(turbine_switch, INPUT);
  pinMode(blaster_switch, INPUT);
  pinMode(blaster_led, OUTPUT);
  pinMode(enA, OUTPUT);
  pinMode(motorin1, OUTPUT);
  pinMode(motorin2, OUTPUT);
}

void loop()
{
  if (digitalRead(blaster_switch) == HIGH) {
    myDFPlayer.play(1);
    digitalWrite(blaster_led, HIGH);
    delay(100);
    digitalWrite(blaster_led, LOW);
  }
  else {
    digitalWrite(blaster_led, LOW);
  }

  if (digitalRead(turbine_switch) == HIGH) {
    myDFPlayer.play(2);
    digitalWrite(motorin1, HIGH);
    digitalWrite(motorin2, LOW);
    analogWrite(enA, 200);
  }
}

speeder_Steckplatine|690x467

Dear @UKHeliBob & @ToddL1962. Firstly, thanks for your reply.

I now know that I shouldnt draw circuits sleep deprived. So after a closer look at general sketching tutorials (etc), I think I've made it way more complicated than it should be.

I'd really appreciate it, if you'd have a look at my new circuit in combination with the code. I think this should work.

#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;

int turbine_switch = 4;
int blaster_switch = 5;
int blaster_led = 6;
int motorin2 = 7;
int motorin1 = 8;
int enA = 9;

void setup()
{
  mySoftwareSerial.begin(9600);
  myDFPlayer.volume(30);
  pinMode(turbine_switch, INPUT);
  pinMode(blaster_switch, INPUT);
  pinMode(blaster_led, OUTPUT);
  pinMode(enA, OUTPUT);
  pinMode(motorin1, OUTPUT);
  pinMode(motorin2, OUTPUT);
}

void loop()
{
  if (digitalRead(blaster_switch) == HIGH) {
    myDFPlayer.play(1);
    digitalWrite(blaster_led, HIGH);
    delay(100);
    digitalWrite(blaster_led, LOW);
  }
  else {
    digitalWrite(blaster_led, LOW);
  }
  if (digitalRead(turbine_switch) == HIGH) {
    myDFPlayer.play(2);
    digitalWrite(motorin1, HIGH);
    digitalWrite(motorin2, LOW);
    analogWrite(enA, 200);
  }
}

I have never used a DF Mini Player but I wonder if you call myDFPlayer.play(1) before the track stops playing will it restart it? You may need to use state change detection on the inputs to determine when the button BECOMES pressed to start the player and stop the player when the button BECOMES released. Check out the following tutorial:

StateChangeDetection

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.