// Projeto para Criar / Ler Arquivos .txt junto ao Cartão SD Arduino
// Visite nossa loja através do link www.usinainfo.com.br
// Mais projetos em www.www.usinainfo.com.br/blog/</pre>
#include <SD.h>
#include <SPI.h>
#include <Arduino.h>
#include <Stepper.h>
#include <vector>
#include <map>
File myFile;
#define GLOBAL_SPEED 15
const int GLOBAL_STEPS = 2048;
const int GLOBAL_TARGET = 100;
class SD_CARD{
public:
SD_CARD( ) {}
static void EscreverArquivo(std::vector<int> positions){
if (SD.begin()) { // Inicializa o SD Card
Serial.println("SD Card pronto para uso."); // Imprime na tela
}
else {
Serial.println("Falha na inicialização do SD Card.");
}
myFile = SD.open("/wokwi.txt", FILE_WRITE); // Cria / Abre arquivo .txt
if (myFile) { // Se o Arquivo abrir imprime:
Serial.println("Escrevendo no Arquivo .txt"); // Imprime na tela
// myFile.println(xx); // Escreve no Arquivo
// myFile.println(yy);
for (int i=0; i< positions.size(); i++){
myFile.println(positions[i]);
}
myFile.close(); // Fecha o Arquivo após escrever
Serial.println("Terminado."); // Imprime na tela
Serial.println(" ");
}
else { // Se o Arquivo não abrir
Serial.println("Erro ao Abrir Arquivo .txt"); // Imprime na tela
}
}
static std::vector<int> LerArquivo(){
std::vector<int> positions;
if (SD.begin()) { // Inicializa o SD Card
Serial.println("SD Card pronto para uso."); // Imprime na tela
}
else {
Serial.println("Falha na inicialização do SD Card.");
}
myFile = SD.open("/wokwi.txt"); // Abre o Arquivo
if (myFile) {
Serial.println("Conteúdo do Arquivo:"); // Imprime na tela
while (myFile.available()) { // Exibe o conteúdo do Arquivo
positions.push_back(myFile.read());
}
myFile.close(); // Fecha o Arquivo após ler
}
else {
Serial.println("Erro ao Abrir Arquivo .txt"); // Imprime na tela
}
myFile = SD.open("/wokwi.txt"); // Abre o Arquivo
while (myFile.available()) { // Exibe o conteúdo do Arquivo
Serial.write(myFile.read());
}
myFile.close();
return positions;
}
};
class Engine {
public:
int AP, AN, BP, BN;
char CORD;
int target;
int position;
public:
Stepper *stepperConstructor;
Engine(char _cord, int _bn, int _bp, int _ap, int _an) : AP(_ap), AN(_an), BP(_bp), BN(_bn), CORD(_cord) {
this->stepperConstructor = new Stepper(GLOBAL_STEPS, _bn, _bp, _ap, _an);
this->stepperConstructor->setSpeed(GLOBAL_SPEED);
this->target = GLOBAL_TARGET;
this->position = 0;
}
char getCord() {
return this->CORD;
}
int getTarget() {
return this->target;
}
void reverseTarget() {
this->target*=(-1);
}
void runToTarget() {
if(this->target > 0) {
this->stepperConstructor->step(1);
this->position++;
}
else {
this->stepperConstructor->step(-1);
this->position--;
}
}
int getPosition() { return this->position; }
};
class EnginesSet {
private:
std::vector<Engine *> engines;
std::vector<Engine *> enginesToPlay;
public:
EnginesSet(Engine *E0, Engine *E1, Engine *E2, Engine *E3, Engine *E4, Engine *E5) {
this->engines.push_back(E0);
this->engines.push_back(E1);
this->engines.push_back(E2);
this->engines.push_back(E3);
this->engines.push_back(E4);
this->engines.push_back(E5);
setPosition();
}
EnginesSet() {}
void setPosition(){
std::vector<int> pos;
int i=0;
pos=SD_CARD::LerArquivo();
for (auto mot: engines ){
mot->position=pos[i];
Serial.println("Posições setadas");
Serial.println(mot->position);
i++;
}
}
void writePosition(){
std::vector<int> aux;
for (auto mot: engines){
aux.push_back(mot->position);
}
SD_CARD::EscreverArquivo(aux);
}
void insertMotor(char cord, int BN, int BP, int AP, int AN) {
Engine *engineConstrutor = new Engine(cord, BN, BP, AP, AN);
this->engines.push_back(engineConstrutor);
}
void boot(); // Lógica de fim de curso
void addToenginesToPlay(std::string cords) {
for (auto &cord : cords)
{
for (auto engine : this->engines)
{
if (engine->getCord() == cord)
{
this->enginesToPlay.push_back(engine);
}
}
}
}
void playMany(int times = 1) {
Engine* anyEngine = this->enginesToPlay[0];
for(int i=0; i<times; i++) {
if(anyEngine->getTarget() > 0) {
while(anyEngine->getPosition() < anyEngine->getTarget()) {
for(auto engine : this->enginesToPlay) {
engine->runToTarget();
delay(1);
}
}
} else {
while(anyEngine->getPosition() > anyEngine->getTarget()) {
for(auto engine : this->enginesToPlay) {
engine->runToTarget();
delay(1);
}
}
}
for(auto engine : this->enginesToPlay) {
engine->reverseTarget();
}
}
this->enginesToPlay.clear();
writePosition();
}
void parseFile(std::string stream) {
auto ch = stream.begin();
for (auto ch = stream.begin(); ch != stream.end(); ++ch)
{
if (*ch == ' ')
delay(500);
else
{
// Coloca no enginesToPlay cada corda lida junta
for (auto sub = ch; sub != stream.end(); ++sub)
{
if (*sub == ' ')
{
ch = sub - 1;
break;
}
this->addToenginesToPlay(std::string(1, *sub));
}
this->playMany();
}
}
}
};
EnginesSet violao;
SD_CARD sd;
void setup() { // Executado uma vez quando ligado o Arduino
Serial.begin(9600); // Define BaundRate
pinMode(5, OUTPUT); // Declara pinoSS como saída
violao.insertMotor('A', 26, 25, 33, 32); //2
violao.insertMotor('D', 15, 2, 4, 5); //3
violao.insertMotor('G', 13, 12, 14, 27); //4
violao.addToenginesToPlay("A");
violao.playMany();
violao.addToenginesToPlay("A");
violao.playMany();
// std::vector<int> aux;
// aux.push_back(0);
// aux.push_back(100);
// sd.EscreverArquivo(aux);
sd.LerArquivo();
}
void loop() {
// Como a função é executada somente uma vez, esta área permanece em branco
delay(1);
}