#include <Servo.h>
Servo myservo; // create servo object to control a servo
Servo myservo2;
Servo myservo3;
///////////////////////////////////Variables dispensador////////////////////////////////////////////////
void(* resetSoftware)(void) = 0;
volatile int sensor = 7;
//motor
int activate = 1;
const int speedPin = 6;
int activarMotor = 0;
//Monedero.
const int SignalPin = 2;
//interrupciones
volatile int pulso = 0;
//Constantes para las monedas.
const int FiveCent = 1;
const int TenCent = 2;
const int TwenyfiveCent = 3;
const int FiftyCent = 4;
////////////////////////////////////////////////////////////////////////////////////
int pos = 0; // variable to store the servo position
int pos2 = 0;
int pos3 = 0;
//volatile unsigned long MillisUltPulso = 0;
unsigned long MillisUltPulso = 0;
int PulsosAcum = 0;
int CreditoAcum = 0;
int MaxTimePulse = 200;
unsigned long lastTime;
int vuelto = 0;
int actvuelto = 0;
//encoder
const int sensorPin = 7;
bool encoder1 = 0;
bool encoder2 = 0;
bool encoderA = 0;
bool encoderB = 0;
bool encoder_state = 0;
int pulsos = 0;
void setup()
{
pinMode(2, INPUT);
pinMode(sensorPin , INPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(speedPin, OUTPUT);
pinMode(sensorPin , INPUT); //definir pin como entrada
Serial.begin(9600);
//Agregamos la interrupcion con el pin indicado.
attachInterrupt(digitalPinToInterrupt(SignalPin), coinInterrupt, RISING);
myservo.attach(22);
myservo2.attach(26);
myservo3.attach(28);
Serial.println("conectado");
}
// Interrupcion.
void coinInterrupt() {
if (activate == 1)
{
// Cada vez que insertamos una moneda valida, incrementamos el contador de monedas y encendemos la variable de control,
pulso++;
MillisUltPulso = millis();
Serial.println("Interrupcion");
}
}
void loop()
{
if (activate == 1)
{
//Calculamos los milisegundos de la ultima ejecusion menos el ultimo tiempo que se genero un pulso.
lastTime = millis() - MillisUltPulso;
}
//Validamos si hay algun puslo, si es asi tambien se valida que el ultimo tiempo asignado sea mayor a la cantidad de milisegundos establecidos.
if ((pulso > 0) && (lastTime >= MaxTimePulse))
{
//La cantidad de creditos es el contador y acumulador de pulsos, hasta que se cumpla alguna condicion.
PulsosAcum = pulso;
pulso = 0;
Serial.print("Pulses: ");
Serial.print(PulsosAcum);
Serial.print(" LastTime: ");
Serial.print(lastTime);
Serial.print(" LastPulse: ");
Serial.println(MillisUltPulso);
}
motor();
switch (PulsosAcum) {
case FiveCent:
activate = 0;
motor();
break;
////////////////////////////////////////////////////////////////////////////////////
case TenCent:
activate = 0;
motor();
if (actvuelto == 1 ) {
Serial.println("DA?????");
vuelto = 1;
vueltos();
}
//////////////////////////////////////////////////////////////////////////////////////////////
break;
case TwenyfiveCent:
activate = 0;
motor();
if (actvuelto == 1 ) {
Serial.println("DA?????");
vuelto = 1;
vueltos();
}
//////////////////////////////////////////////////////////////////////////////////////////////
break;
case FiftyCent:
// activate = 0;
// activarMotor = 1;
// if (activarMotor = 1) {
// Serial.println("Motor");
// motor();
// }
vuelto = 1;
vueltos();
break;
}
}
void motor()
{
if (activate == 0)
{
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
digitalWrite(speedPin, 100);
encoder();
}
if (activate == 1)
{
digitalWrite(8, LOW);
digitalWrite(9, LOW);
}
}
void encoder()
{
//pulsos = 0;
encoder1 = digitalRead(sensorPin ); //lectura digital de pin
encoder2 = !encoder1;
if (encoder1 == 1) {
encoderA = 1;
}
if (encoder1 == 0) {
encoderB = 1;
}
if (encoderA == 1 && encoderB == 1) {
pulsos++;
encoder1 = 0;
encoder2 = 0;
encoderA = 0;
encoderB = 0;
}
Serial.println(pulsos);
if (pulsos >= 200)
{
activate = 1;
pulsos = 0;
PulsosAcum = 0;
actvuelto = 1;
}
}
void vueltos() {
if (vuelto == 1) {
switch (PulsosAcum) {
case TenCent:
vuelto10();
//////////////////////////////////////////////////////////////////////////////////////////////
break;
case TwenyfiveCent:
vuelto25();
//////////////////////////////////////////////////////////////////////////////////////////////
break;
case FiftyCent:
vuelto50();
break;
}
}
}
void vuelto10() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 ms for the servo to reach the position
}
resetSoftware();
}
void vuelto25() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 ms for the servo to reach the position
}
//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
for (pos2 = 0; pos2 <= 180; pos2 += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo2.write(pos2); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 ms for the servo to reach the position
}
for (pos2 = 180; pos2 >= 0; pos2 -= 1) { // goes from 180 degrees to 0 degrees
myservo2.write(pos2); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 ms for the servo to reach the position
}
//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 ms for the servo to reach the position
}
/////////////////////////////////////////////////////////////////////
resetSoftware();
}
void vuelto50() {
////////////////////////////////////////////////////////////////////////////////////////////
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 ms for the servo to reach the position
}
//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
for (pos2 = 0; pos2 <= 180; pos2 += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo2.write(pos2); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 ms for the servo to reach the position
}
for (pos2 = 180; pos2 >= 0; pos2 -= 1) { // goes from 180 degrees to 0 degrees
myservo2.write(pos2); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 ms for the servo to reach the position
}
//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
for (pos3 = 0; pos3 <= 180; pos3 += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo3.write(pos3); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 ms for the servo to reach the position
}
for (pos3 = 180; pos3 >= 0; pos3 -= 1) { // goes from 180 degrees to 0 degrees
myservo3.write(pos3); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 ms for the servo to reach the position
}
//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 ms for the servo to reach the position
}
////////////////////////////////////////////////////////////////////////////////////////////
resetSoftware();
}
Hola!
El procedimiento motor() se ejecuta en el loop() hasta que se verifican las condiciones, es decir, cada vez que se ejecuta el ciclo, literalmente cientos de veces por segundo.
y además. No encontré donde reinicias el valor de PulsosAcum después de pasar la siguiente moneda
Para empezar si hay interrupciones esto no
Serial.println("Interrupcion");
Las interrupciones son veloces, hacen lo que deben y devuelven el control al programa. No se imprime nada porque esa no es la idea.
Pones un flag y si el flag esta activo lo imprimes en el loop.
void(* resetSoftware)(void) = 0;
esto tambien es una mala práctica.
Si necesitas algo asi por si el códiogo se cuelga usa el watchdog que tiene implementado el Arduino.
No se si habrá un gran cambio, solo eliminé resetSoftware() que lo unico que hace es reiniciar 1 y otra vez tu código.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
Servo myservo2;
Servo myservo3;
///////////////////////////////////Variables dispensador////////////////////////////////////////////////
volatile int sensor = 7;
//motor
int activate = 1;
const int speedPin = 6;
int activarMotor = 0;
//Monedero.
const int SignalPin = 2;
//interrupciones
volatile int pulso = 0;
//Constantes para las monedas.
const int FiveCent = 1;
const int TenCent = 2;
const int TwenyfiveCent = 3;
const int FiftyCent = 4;
////////////////////////////////////////////////////////////////////////////////////
int pos = 0; // variable to store the servo position
int pos2 = 0;
int pos3 = 0;
//volatile unsigned long MillisUltPulso = 0;
unsigned long MillisUltPulso = 0;
int PulsosAcum = 0;
int CreditoAcum = 0;
int MaxTimePulse = 200;
unsigned long lastTime;
int vuelto = 0;
int actvuelto = 0;
//encoder
const int sensorPin = 7;
bool encoder1 = 0;
bool encoder2 = 0;
bool encoderA = 0;
bool encoderB = 0;
bool encoder_state = 0;
int pulsos = 0;
void setup()
{
pinMode(2, INPUT);
pinMode(sensorPin , INPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(speedPin, OUTPUT);
pinMode(sensorPin , INPUT); //definir pin como entrada
Serial.begin(9600);
//Agregamos la interrupcion con el pin indicado.
attachInterrupt(digitalPinToInterrupt(SignalPin), coinInterrupt, RISING);
myservo.attach(22);
myservo2.attach(26);
myservo3.attach(28);
Serial.println("conectado");
}
// Interrupcion.
void coinInterrupt() {
if (activate == 1) {
// Cada vez que insertamos una moneda valida, incrementamos el contador de monedas y encendemos la variable de control,
pulso++;
MillisUltPulso = millis();
// Serial.println("Interrupcion");
}
}
void loop() {
if (activate == 1) {
//Calculamos los milisegundos de la ultima ejecusion menos el ultimo tiempo que se genero un pulso.
lastTime = millis() - MillisUltPulso;
}
//Validamos si hay algun puslo, si es asi tambien se valida que el ultimo tiempo asignado sea mayor a la cantidad de milisegundos establecidos.
if ((pulso > 0) && (lastTime >= MaxTimePulse)) {
//La cantidad de creditos es el contador y acumulador de pulsos, hasta que se cumpla alguna condicion.
PulsosAcum = pulso;
pulso = 0;
Serial.print("Pulses: ");
Serial.print(PulsosAcum);
Serial.print(" LastTime: ");
Serial.print(lastTime);
Serial.print(" LastPulse: ");
Serial.println(MillisUltPulso);
}
motor();
switch (PulsosAcum) {
case FiveCent:
activate = 0;
motor();
break;
////////////////////////////////////////////////////////////////////////////////////
case TenCent:
activate = 0;
motor();
if (actvuelto == 1 ) {
Serial.println("DA?????");
vuelto = 1;
vueltos();
}
//////////////////////////////////////////////////////////////////////////////////////////////
break;
case TwenyfiveCent:
activate = 0;
motor();
if (actvuelto == 1 ) {
Serial.println("DA?????");
vuelto = 1;
vueltos();
}
//////////////////////////////////////////////////////////////////////////////////////////////
break;
case FiftyCent:
// activate = 0;
// activarMotor = 1;
// if (activarMotor = 1) {
// Serial.println("Motor");
// motor();
// }
vuelto = 1;
vueltos();
break;
}
}
void motor() {
if (activate == 0) {
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
digitalWrite(speedPin, 100);
encoder();
}
if (activate == 1) {
digitalWrite(8, LOW);
digitalWrite(9, LOW);
}
}
void encoder() {
//pulsos = 0;
encoder1 = digitalRead(sensorPin ); //lectura digital de pin
encoder2 = !encoder1;
if (encoder1 == 1) {
encoderA = 1;
}
if (encoder1 == 0) {
encoderB = 1;
}
if (encoderA == 1 && encoderB == 1) {
pulsos++;
encoder1 = 0;
encoder2 = 0;
encoderA = 0;
encoderB = 0;
}
Serial.println(pulsos);
if (pulsos >= 200) {
activate = 1;
pulsos = 0;
PulsosAcum = 0;
actvuelto = 1;
}
}
void vueltos() {
if (vuelto == 1) {
switch (PulsosAcum) {
case TenCent:
vuelto10();
//////////////////////////////////////////////////////////////////////////////////////////////
break;
case TwenyfiveCent:
vuelto25();
//////////////////////////////////////////////////////////////////////////////////////////////
break;
case FiftyCent:
vuelto50();
break;
}
}
}
void vuelto10() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 ms for the servo to reach the position
}
}
void vuelto25() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 ms for the servo to reach the position
}
//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
for (pos2 = 0; pos2 <= 180; pos2 += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo2.write(pos2); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 ms for the servo to reach the position
}
for (pos2 = 180; pos2 >= 0; pos2 -= 1) { // goes from 180 degrees to 0 degrees
myservo2.write(pos2); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 ms for the servo to reach the position
}
//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 ms for the servo to reach the position
}
/////////////////////////////////////////////////////////////////////
}
void vuelto50() {
////////////////////////////////////////////////////////////////////////////////////////////
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 ms for the servo to reach the position
}
//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
for (pos2 = 0; pos2 <= 180; pos2 += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo2.write(pos2); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 ms for the servo to reach the position
}
for (pos2 = 180; pos2 >= 0; pos2 -= 1) { // goes from 180 degrees to 0 degrees
myservo2.write(pos2); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 ms for the servo to reach the position
}
//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
for (pos3 = 0; pos3 <= 180; pos3 += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo3.write(pos3); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 ms for the servo to reach the position
}
for (pos3 = 180; pos3 >= 0; pos3 -= 1) { // goes from 180 degrees to 0 degrees
myservo3.write(pos3); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 ms for the servo to reach the position
}
//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 ms for the servo to reach the position
}
}
Graciaaas lo voy a probar
Si tiene reinicio en el contador del encoder
Para trabajar el encoder usa la librería Enconder.h disponible en el Administrador de Librerías.
No veo nada en void encoder() que haga reiniciar el programa.
Que es esto?
digitalWrite(speedPin, 100);
Esta es una instrucción que solo depende de un pin y de indicarle que se ponga en 1 o 0, solo esto
digitalWrite(pin, HIGH) ;
o
digitalWrite(pin, LOW) ;
en tu caso lo que quieres haces es con
analogWrite(speedPin, 100);
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.