add arduino code

This commit is contained in:
gocivici 2024-07-13 15:02:10 +03:00
parent 5a7d04a37a
commit d943bece30
2 changed files with 228 additions and 0 deletions

View File

@ -0,0 +1,64 @@
/*
* LEGO Harkonnen Display
* By Görkem Bozkurt
* https://gorkem.cc
* Requires AccelStepper.h and mp3tf16p.h libraries
*/
#include "mp3tf16p.h"
#include <AccelStepper.h>
#include <Stepper.h>
boolean playedOnce = false;
const int buttonLight = 5; //front button light
const int boxLight = 2; //LED strip inside box
const int buttonPin = 6; //front button pin
int stepperLoc = 0; //initial location of stepper motor
#define HALFSTEP 8
#define motorPin1 9 // IN1 on ULN2003 ==> Blue on 28BYJ-48
#define motorPin2 10 // IN2 on ULN2003 ==> Pink on 28BYJ-48
#define motorPin3 11 // IN3 on ULN2003 ==> Yellow on 28BYJ-48
#define motorPin4 12 // IN4 on ULN2003 ==> Orange on 28BYJ-48
MP3Player mp3(3,4); //initialize mp3 player module
AccelStepper mystepper(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT);
pinMode(buttonLight, OUTPUT);
pinMode(boxLight, OUTPUT);
mystepper.setMaxSpeed(800);
mystepper.setAcceleration(120);
mp3.initialize();
digitalWrite(buttonLight,HIGH);
}
void loop() {
if(digitalRead(buttonPin)){
mp3.playTrackNumber(1,20,false);
digitalWrite(buttonLight,LOW);
playedOnce = true;
stepperLoc = 8000;
}
mystepper.moveTo(stepperLoc);
if(mp3.playCompleted()){
stepperLoc = 0;
digitalWrite(buttonLight,HIGH);
}
if(mystepper.distanceToGo()!=0){
mystepper.enableOutputs();
mystepper.run();
}else{
mystepper.disableOutputs();
}
}

164
LegoHarkonnen/mp3tf16p.h Normal file
View File

@ -0,0 +1,164 @@
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
#define MP3_ERROR_ONLY 1
#define MP3_ALL_MESSAGE 2
class MP3Player
{
private:
SoftwareSerial *mySoftwareSerial;
void statusOnSerial(uint8_t type, int value);
void waitPlayIsTerminated(void);
int p_RX;
int p_TX;
public:
DFRobotDFPlayerMini player;
MP3Player(int RX, int TX);
~MP3Player();
void playTrackNumber(int trackNumber, int volume, boolean waitPlayTerminated = true);
boolean playCompleted(void);
void initialize(void);
int serialPrintStatus(int errorOnly);
};
MP3Player::MP3Player(int RX, int TX)
{
p_TX = TX;
p_RX = RX;
}
MP3Player::~MP3Player()
{
}
void MP3Player::initialize(void)
{
mySoftwareSerial = new SoftwareSerial(p_RX, p_TX);
mySoftwareSerial->begin(9600);
Serial.println(F("Initializing MP3Player ..."));
if (!player.begin(*mySoftwareSerial,true,false))
{
Serial.println(F("Unable to begin:"));
Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!"));
while (true)
;
}
player.volume(10);
Serial.println(F("MP3Player online."));
}
void MP3Player::playTrackNumber(int trackNumber, int volume, boolean waitPlayTerminated)
{
player.volume(volume);
player.play(trackNumber);
if (waitPlayTerminated)
{
waitPlayIsTerminated();
}
}
void MP3Player::waitPlayIsTerminated(void)
{
while (!playCompleted())
{
}
}
boolean MP3Player::playCompleted(void)
{
if (player.available())
{
return player.readType() == DFPlayerPlayFinished;
}
return false;
}
// Print the detail message from DFPlayer to handle different errors and states.
//
int MP3Player::serialPrintStatus(int verbose)
{
if (player.available())
{
uint8_t type = player.readType();
int value = player.read();
if (verbose == MP3_ERROR_ONLY)
{
if (type == DFPlayerError)
{
statusOnSerial(type, value);
}
}
else
{
statusOnSerial(type, value);
}
if(type == DFPlayerError) {
return value;
} else {
return 0;
}
}
}
void MP3Player::statusOnSerial(uint8_t type, int value)
{
switch (type)
{
case TimeOut:
Serial.println(F("Time Out!"));
break;
case WrongStack:
Serial.println(F("Stack Wrong!"));
break;
case DFPlayerCardInserted:
Serial.println(F("Card Inserted!"));
break;
case DFPlayerCardRemoved:
Serial.println(F("Card Removed!"));
break;
case DFPlayerCardOnline:
Serial.println(F("Card Online!"));
break;
case DFPlayerPlayFinished:
Serial.print(F("Number:"));
Serial.print(value);
Serial.println(F(" Play Finished!"));
break;
case DFPlayerError:
Serial.print(F("DFPlayerError:"));
switch (value)
{
case Busy:
Serial.println(F("Card not found"));
break;
case Sleeping:
Serial.println(F("Sleeping"));
break;
case SerialWrongStack:
Serial.println(F("Get Wrong Stack"));
break;
case CheckSumNotMatch:
Serial.println(F("Check Sum Not Match"));
break;
case FileIndexOut:
Serial.println(F("File Index Out of Bound"));
break;
case FileMismatch:
Serial.println(F("Cannot Find File"));
break;
case Advertise:
Serial.println(F("In Advertise"));
break;
default:
break;
}
break;
default:
break;
}
}