Hi!
I'm very new to Arduino, but know languages (C, C++, Java, TCL, Pascal, etc....) for long time, and I'm trying to learn the Arduino world.
I need some help here, because I cannot see what's wrong that is making my servo not work.
Here is the problem:
I made a simple circuit with a Rotary Encoder, a Stepper Motor and a servo only.
The RE Switch when pressed, turns the servo to specific angles (10 and 100 in this case). The rotary encoder will control de Stepper Motor.
I Since my initial intent is learn, and I was trying another Stepper Motor (and also will try others), I decided to make a Library to store my stepper motors configuration (initially made a STRUCT, but later changed to another class just to try to make it work, but I prefer a struct) and made a CLASS to deal with them. This class will inherit Stepper class, just to store the motor configuration and run on or two simple methods.
The fact is that my servo never moved when I pushed the encoder switch. The function of pressing the switch is being called, but the servo aperared dead, dont react to the write command. I tried a lot of things. First I thought it was the wiring, changed all, disconnected stepper motor, leaving just the servo, etc... but them I decided to run the SWEEP sketch tutorial and the servo was working perfetly.
After some tries, I decided to compile and run just adding parts of the program, and found out that it stops working when I was declaring my Stepper Motor though my library.
As I said, I tried to change my struct to a class, tried to change some codes (happily I learnt a lot more on these), but nothing works. I cannot see what my library is doing to make the servo stop working.
If somebody could take a look and point to the right direction, I would be very pleased...
thankyou.
Part of the variables are in portuguese, let me translate some for you:
- passos = steps
- modelo = model
- pinos = pins
- velMax = Max Speed
- angulo = angle
My library:
motores.h (possibly will change back MODELO to a struct).
#ifndef MOTORES_H
#define MOTORES_H
#include <Arduino.h>
#include <Stepper.h>
class MODELO {
public:
long steps;
byte velMax;
};
// Structs com as configurações dos motores
//struct MODELO {
// long steps; // Numero de passos para uma volta completa
// byte velMax;
//} ;
extern MODELO MICRO_SLIDE;
extern MODELO MICRO_DIRETO;
extern MODELO MITSUMI_M365SP_7NP;
extern const MODELO GENERICO_28BYJ_48;
const int HORARIO = 1;
const int ANTIHORARIO = -1;
class Motor: public Stepper {
private:
MODELO modelo;
byte pinos[];
public:
Motor(MODELO modelo, byte pinos[]);
void giraMotor(int sentido, long passos);
void giraAngulo(long angulo);
};
#endif
motores.cpp
#include "motores.h"
#define sgn(x) ((x) < 0 ? -1 : ((x) > 0 ? 1 : 0))
extern MODELO MICRO_SLIDE = {50, 100};
extern MODELO MICRO_DIRETO = {50, 100};
extern MODELO MITSUMI_M365SP_7NP = {50, 100};
extern const MODELO GENERICO_28BYJ_48 = {2048, 17}; // EIXO INTERNO=32 , EXTERNO=2048 VelMax=17 ou 700?
Motor::Motor(MODELO modelo, byte pinos[]): Stepper(modelo.steps, pinos[0], pinos[1],
pinos[2], pinos[3]) {
this->pinos[0] = pinos[0]; this->pinos[1] = pinos[1];
this->pinos[2] = pinos[2]; this->pinos[3] = pinos[3];
this->modelo.steps = modelo.steps;
this->modelo.velMax = modelo.velMax;
}
// Funcao que faz o Giro do motor em passos no sentido desejado
void Motor::giraMotor(int sentido, long passos) {
Serial.print("\t Sentido e passos:");
Serial.print(sentido);
Serial.print("\t ");
Serial.println(passos);
step(sentido*passos);
}
// Funcao que faz o giro do motor no angulo solicitado
void Motor::giraAngulo(long angulo) {
float passos_a_fazer = (long)(modelo.steps * angulo) / 360;
Serial.println("------------");
Serial.print("StepsTotal:");
Serial.println(modelo.steps);
Serial.print("Angulo:");
Serial.println(angulo);
Serial.print("Passos:");
Serial.println(passos_a_fazer);
giraMotor(sgn(passos_a_fazer), abs(passos_a_fazer));
}
sketch:
#include <motores.h>
#include <Servo.h>
// Variaveis e CONSTANTES
#define motorUsado GENERICO_28BYJ_48
#define passosVelocidade 1 // Número de passos de incremento da VelocidadeAtual
#define velocidadeMinima 1 // Velocidade mínima dos passos do motor
#define anguloAtivado 10 // Angulo do SERVO quando usando o Foco
#define anguloDesativado 100 // Angulo do SERVO quando o Foco está desativado
int posicaoAtual = 0; // Posicao do Motor de Foco
int posicaoAnterior;
int posicaoProxima;
boolean focoAtivado = false;
// ENCODER
volatile boolean giroDetectado; // VOLATILE para interrupts
volatile boolean direcaoEncoder; // Para que lado o Encoder está rodando
// PINAGEM
// Encoder[]
#define encCLK 2
#define encDT 3
#define encSW 4
#define intEnc 1 // Interrupcao para o pino 2: 0 para NANO, 1 para Leonardo
// Motor e Servo
const byte pinosFoco[] = { 8, 9, 10, 11 };
#define ativacaoPIN 13
#define passosPORgiro 10
// BOTOES
#define botaoUP A2 // Botao para aumento dos passos do motor
#define botaoDOWN A1 // Botao para diminuicao dos passos do motor
#define botaoFIM A0 // Botao para indicar fim de curso do focalizador
// Display
#define displayCLK 5
#define displayDIO 6
#define displayBRILHO 5
//TM1637 display(displayCLK, displayDIO);
// Componentes
Servo Ativacao;
Motor Foco(motorUsado, pinosFoco); // Motor de Ajuste de Foco
unsigned int velocidadeAtual = 5; // VelocidadeAtual Atual do motor, em passos
//---------------------------------------------------
//----------------------------------------------
void activate() {
Serial.print("ACTIVATE: \t");
Serial.println(anguloAtivado);
Ativacao.write(anguloAtivado);
focoAtivado = true;
delay(100);
}
//----------------------------------------------
void deactivate() {
Serial.print("DEACTIVATE: \t");
Serial.println(anguloDesativado);
Ativacao.write(anguloDesativado);
focoAtivado = false;
delay(100);
}
void setup() {
// put your setup code here, to run once:
Ativacao.attach(ativacaoPIN);
}
void loop() {
// put your main code here, to run repeatedly:
if (digitalRead(encSW) == LOW) { // Checa se o botao foi pressionado (ativa /desativa Foco)
if (focoAtivado)
deactivate();
else
activate();
while (digitalRead(encSW) == LOW) delay(100); // Espera o botão ser liberado
}
}