Tengo una consulta tengo un pequeño proyecto al cual no lo he podido terminar,
lo que pasa es que activo un interruptor y se enciende la salida por 15 segundo el problema es
que yo quiero crear le un boton de stop y que no cuante los 15 segundo y me apague de inmediato la
salida.
La activacion con un interruptor
Y el stop con boton
un boton activa una salida por 15 segundos
Otro botón debe detener la salida
Bueno es fácil. Un boton debe poner una variable que llamaremos Activar = 1
Cuando esa variable esta en 1 accionas tu salida de 15 segundos, mientras esto ocurra ( y acá viene el problema) tu quieres usar otro boton para detenerlo.
El segundo botón pondrá Activar = 0 y debería detener la salida.. pero si usas delay no podrás hacerlo hasta que terminen los 15 segundos por lo que la solución está en usar millis().
millis() es una función como si fuera un cronómetro que arranca cuando el arduino se enciende o se energiza.
Si tomas un valor parcial y ese valor luego lo usas para compararlo con ese mismo valor mas 15 seg tendrás tu control de salida.
Pero como millis() no detenie el programa, podrás leer el segundo botón y ponerlo a 0 y entonces apagar la salida.
Mira el ejemplo BlinkWithoutDelay.ino del IDE y también busca los tutoriales que existen en Documentación en especial el tutorial de Nick Gammon Función millis() traducido por max_saeta.
NO me explique bien, el progrma deberia ser asi.
El contador inicia cuando un switch(interruptor), esta en estado alto y la salida esta encendia
mientras el contador sea menor que 15 segundo.
con un pulsador(boton) quiero detener en contador, apagar la sailda. ejemplo como si fuese una emergencia
y debe apagarse la salida y detener el contador.
Si te explicaste bien... intenta haciendo el código.
// este es el codigo que cree///
const int boton1= 4;
const int boton2= 6;
const int tiempoAntirebote =10;
const int luz=8;
int cuenta =0;
int estBot1;
int estBotAnt1;
int estBot2;
int estBotAnt2;
int salida1=0;
int salida2=0;
int ig=0;
int start=0;
int contador=0;
int tiempo;
int estado;
boolean antirebote (int pin ) {
int contador =0;
boolean estado;
boolean estadoAnterior;
do {
estado = digitalRead (pin);
if (estado != estadoAnterior ){
contador = 0;
estadoAnterior = estado;
}
else{
contador = contador +1;
}
delay (1);
}
while (contador < tiempoAntirebote);
return estado;
}
void setup (){
Serial.begin(9600);
pinMode(boton1,INPUT);
pinMode(boton2,INPUT);
pinMode(luz,OUTPUT);
}
void loop () {
/// Lectura de botones //////////////////
//---------Boton #1-----------/////////
estBot1 = digitalRead(boton1);
if (estBot1 != estBotAnt1) {
if (antirebote (boton1)){
salida1= 1 - salida1;
}
}
estBotAnt1= estBot1;
////////////////////////////////////
//---------Boton #2-----------/////////
estBot2 = digitalRead(boton2);
if (estBot2 != estBotAnt2) {
if (antirebote (boton2)){
salida2= 1 - salida2;
}
}
estBotAnt2= estBot2;
///////////////////////////////
///////////////////////////////<<<<<<<<<<<<<<<<<<<<<
/// Parte del codigo ///
if (salida1 && contador<tiempo){
delay(1000);
contador = contador + 1;
}
else {
estado = 0;
contador = 0;
}
if ( contador == tiempo || salida2 == HIGH)
{
estado = 0;
contador = 0;
}
digitalWrite(luz, estado);
Serial.print("contador:");
Serial.println(contador);
Serial.print("estado");
Serial.println(estado);
Serial.print("Salida1: ");
Serial.println(salida1);
}
Lee las normas y luego edita este ultimo post usando tags para insertar códigos.
Hola muchas gracias por la sugerencia.
Acabo de creear el codigo este me esta funcionando ala perfeccion.
quisiera una ayudita para depurar, mejorar el codigo.
que le deberia de quitar y que le deberia de poner.
const int boton1= 4;
const int boton2= 6;
const int tiempoAntirebote =10;
const int luz=13;
int cuenta =0;
int estBot1;
int estBotAnt1;
int estBot2;
int estBotAnt2;
int salida1;
int salida2;
int ig=0;
int st=0;
int contador;
int tiempo=15;
int estado=0;
int estado2=0;
boolean antirebote2 (int pin ) {
int contador =0;
boolean estado;
boolean estadoAnterior;
do {
estado = digitalRead (pin);
if (estado != estadoAnterior ){
contador = 0;
estadoAnterior = estado;
}
else{
contador = contador +1;
}
delay (1);
}
while (contador < tiempoAntirebote);
return estado;
}
boolean antirebote (int pin ) {
int contador =0;
boolean estado;
boolean estadoAnterior;
do {
estado = digitalRead (pin);
if (estado != estadoAnterior ){
contador = 0;
estadoAnterior = estado;
}
else{
contador = contador +1;
}
delay (1);
}
while (contador < tiempoAntirebote);
return estado;
}
void setup (){
Serial.begin(9600);
pinMode(boton1,INPUT);
pinMode(boton2,INPUT);
pinMode(luz,OUTPUT);
}
void loop () {
/// Lectura de botones //////////////////
//---------Boton #1-----------/////////
estBot1 = digitalRead(boton1);
if (estBot1 != estBotAnt1) {
if (antirebote (boton1)){
salida1= 1 - salida1;
}
}
estBotAnt1= estBot1;
////////////////////////////////////
//---------Boton #2-----------/////////
estBot2 = digitalRead(boton2);
if (estBot2 != estBotAnt2) {
if (antirebote2 (boton2)){
salida2= 1 - salida2;
}
}
estBotAnt2= estBot2;
///////////////////////////////
///////////////////////////////<<<<<<<<<<<<<<<<<<<<<
/// Parte del codigo ///
if (salida1 && contador<(tiempo*2)){
delay(500);
contador = contador + 1;
estado = 1;
}
else if (estado == LOW){
salida2= 0;
}
else {
estado = 0;
contador = 0;
}
if (contador==(tiempo*2) || salida2 == HIGH){
contador = 0;
salida1 = 0;
}
digitalWrite(luz, estado);
Serial.print("contador: ");
Serial.println(contador);
Serial.println();
Serial.print("estado: ");
Serial.println(estado);
Serial.println();
}
Esto es lo mejor que yo puedo sugerir, al alternativa es usar una librería antirebote
const int boton1 = 4;
const int boton2 = 6;
const int tiempoAntirebote = 10;
const int luz = 13;
int cuenta = 0;
int estBot1;
int estBotAnt1;
int estBot2;
int estBotAnt2;
int salida1;
int salida2;
int ig = 0;
int st = 0;
int contador;
int tiempo = 15;
int estado = 0;
int estado2 = 0;
boolean antirebote (int pin ) {
int contador =0;
boolean estado;
boolean estadoAnterior;
do {
estado = digitalRead (pin);
if (estado != estadoAnterior ){
contador = 0;
estadoAnterior = estado;
}
else{
contador = contador +1;
}
delay (1);
}
while (contador < tiempoAntirebote);
return estado;
}
void setup (){
Serial.begin(9600);
pinMode(boton1,INPUT);
pinMode(boton2,INPUT);
pinMode(luz,OUTPUT);
}
void loop () {
/// Lectura de botones //////////////////
//---------Boton #1-----------/////////
estBot1 = digitalRead(boton1);
if (estBot1 != estBotAnt1) {
if (antirebote (boton1)){
salida1= 1 - salida1;
}
}
estBotAnt1= estBot1;
////////////////////////////////////
//---------Boton #2-----------/////////
estBot2 = digitalRead(boton2);
if (estBot2 != estBotAnt2) {
if (antirebote (boton2)) {
salida2= 1 - salida2;
}
}
estBotAnt2= estBot2;
/// Parte del codigo ///
if (salida1 && contador<(tiempo*2)){
delay(500);
contador = contador + 1;
estado = 1;
} else
if (estado == LOW){
salida2= 0;
}
else {
estado = 0;
contador = 0;
}
if (contador==(tiempo*2) || salida2 == HIGH){
contador = 0;
salida1 = 0;
}
digitalWrite(luz, estado);
Serial.print("contador: ");
Serial.println(contador);
Serial.println();
Serial.print("estado: ");
Serial.println(estado);
Serial.println();
}
Encontre esta
me podrias dar una pequeña ayuda en el uso de .lib
librerias seri primera vez que utilizo una.
thomasfredericks/Bounce2
Bounce2-master.zip
Justamente de esa o de la que yo uso que es Switch.
#include <Bounce2.h>
const int boton1 = 4;
const int boton2 = 6;
const int tiempoAntirebote = 10;
const int luz = 13;
//defines
Bounce debouncer1 = Bounce();
Bounce debouncer2 = Bounce();
int cuenta = 0;
int estBot1;
int estBotAnt1;
int estBot2;
int estBotAnt2;
int salida1;
int salida2;
int ig = 0;
int st = 0;
int contador;
int tiempo = 15;
int estado = 0;
int estado2 = 0;
void setup (){
Serial.begin(9600);
pinMode(boton1,INPUT);
pinMode(boton2,INPUT);
// After setting up the button, setup the Bounce instance :
debouncer1.attach(boton1);
debouncer1.interval(10); // interval in ms
// After setting up the button, setup the Bounce instance :
debouncer2.attach(boton2);
debouncer2.interval(10); // interval in ms
pinMode(luz,OUTPUT);
}
void loop () {
/// Lectura de botones //////////////////
//---------Boton #1-----------/////////
// Actualizo los botones
debouncer1.update();
debouncer2.update();
// Get the updated value :
estBot1 = debouncer1.read();
if (estBot1 == HIGH) {
salida1= !salida1;
}
// Update the Bounce instance :
//---------Boton #2-----------/////////
debouncer2.update();
// Get the updated value :
estBot2 = debouncer2.read();
if (estBot2 == HIGH) {
salida2= !salida2;
}
/// Parte del codigo ///
if (salida1 && contador <(tiempo+tiempo)){
delay(500);
contador++;
estado = 1;
} else
if (estado == LOW){
salida2= 0;
} else {
estado = 0;
contador = 0;
}
if (contador==(tiempo*2) || salida2 == HIGH){
contador = 0;
salida1 = 0;
}
digitalWrite(luz, estado);
Serial.print("contador: ");
Serial.println(contador);
Serial.println();
Serial.print("estado: ");
Serial.println(estado);
Serial.println();
}
Me da error al compilar
Arduino:1.6.6 (Windows 7), Placa:"Arduino/Genuino Uno"
C:\Program Files\Arduino\arduino-builder -dump-prefs -logger=machine -hardware "C:\Program Files\Arduino\hardware" -hardware "C:\Users\TallerGeosat\AppData\Local\Arduino15\packages" -hardware "C:\Users\TallerGeosat\Documents\Arduino\hardware" -tools "C:\Program Files\Arduino\tools-builder" -tools "C:\Program Files\Arduino\hardware\tools\avr" -tools "C:\Users\TallerGeosat\AppData\Local\Arduino15\packages" -built-in-libraries "C:\Program Files\Arduino\libraries" -libraries "C:\Users\TallerGeosat\Documents\Arduino\libraries" -fqbn=arduino:avr:uno -ide-version=10606 -build-path "C:\Users\TALLER~1\AppData\Local\Temp\builda4cbace91fe70d4ec542cbf4bb94d05f.tmp" -warnings=none -prefs=build.warn_data_percentage=75 -verbose "C:\Users\TallerGeosat\Documents\Arduino\sketch_mar17a\sketch_mar17a.ino"
C:\Program Files\Arduino\arduino-builder -compile -logger=machine -hardware "C:\Program Files\Arduino\hardware" -hardware "C:\Users\TallerGeosat\AppData\Local\Arduino15\packages" -hardware "C:\Users\TallerGeosat\Documents\Arduino\hardware" -tools "C:\Program Files\Arduino\tools-builder" -tools "C:\Program Files\Arduino\hardware\tools\avr" -tools "C:\Users\TallerGeosat\AppData\Local\Arduino15\packages" -built-in-libraries "C:\Program Files\Arduino\libraries" -libraries "C:\Users\TallerGeosat\Documents\Arduino\libraries" -fqbn=arduino:avr:uno -ide-version=10606 -build-path "C:\Users\TALLER~1\AppData\Local\Temp\builda4cbace91fe70d4ec542cbf4bb94d05f.tmp" -warnings=none -prefs=build.warn_data_percentage=75 -verbose "C:\Users\TallerGeosat\Documents\Arduino\sketch_mar17a\sketch_mar17a.ino"
"C:\Program Files\Arduino\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -w -x c++ -M -MG -MP -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10606 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\Users\TallerGeosat\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.10\cores\arduino" "-IC:\Users\TallerGeosat\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.10\variants\standard" "C:\Users\TALLER~1\AppData\Local\Temp\builda4cbace91fe70d4ec542cbf4bb94d05f.tmp\sketch\sketch_mar17a.ino.cpp"
"C:\Program Files\Arduino\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -w -x c++ -M -MG -MP -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10606 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\Users\TallerGeosat\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.10\cores\arduino" "-IC:\Users\TallerGeosat\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.10\variants\standard" "-IC:\Users\TallerGeosat\Documents\Arduino\libraries\Bounce2-master" "C:\Users\TALLER~1\AppData\Local\Temp\builda4cbace91fe70d4ec542cbf4bb94d05f.tmp\sketch\sketch_mar17a.ino.cpp"
"C:\Program Files\Arduino\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -w -x c++ -M -MG -MP -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10606 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\Users\TallerGeosat\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.10\cores\arduino" "-IC:\Users\TallerGeosat\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.10\variants\standard" "-IC:\Users\TallerGeosat\Documents\Arduino\libraries\Bounce2-master" "C:\Users\TALLER~1\AppData\Local\Temp\builda4cbace91fe70d4ec542cbf4bb94d05f.tmp\sketch\sketch_mar17a.ino.cpp"
"C:\Program Files\Arduino\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -w -x c++ -M -MG -MP -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10606 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\Users\TallerGeosat\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.10\cores\arduino" "-IC:\Users\TallerGeosat\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.10\variants\standard" "-IC:\Users\TallerGeosat\Documents\Arduino\libraries\Bounce2-master" "C:\Users\TALLER~1\AppData\Local\Temp\builda4cbace91fe70d4ec542cbf4bb94d05f.tmp\sketch\sketch_mar17a.ino.cpp"
"C:\Program Files\Arduino\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -w -x c++ -M -MG -MP -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10606 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\Users\TallerGeosat\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.10\cores\arduino" "-IC:\Users\TallerGeosat\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.10\variants\standard" "-IC:\Users\TallerGeosat\Documents\Arduino\libraries\Bounce2-master" "C:\Users\TallerGeosat\Documents\Arduino\libraries\Bounce2-master\Bounce2.cpp"
"C:\Program Files\Arduino\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -w -x c++ -M -MG -MP -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10606 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\Users\TallerGeosat\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.10\cores\arduino" "-IC:\Users\TallerGeosat\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.10\variants\standard" "-IC:\Users\TallerGeosat\Documents\Arduino\libraries\Bounce2-master" "C:\Users\TALLER~1\AppData\Local\Temp\builda4cbace91fe70d4ec542cbf4bb94d05f.tmp\sketch\sketch_mar17a.ino.cpp"
"C:\Program Files\Arduino\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10606 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\Users\TallerGeosat\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.10\cores\arduino" "-IC:\Users\TallerGeosat\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.10\variants\standard" "-IC:\Users\TallerGeosat\Documents\Arduino\libraries\Bounce2-master" "C:\Users\TALLER~1\AppData\Local\Temp\builda4cbace91fe70d4ec542cbf4bb94d05f.tmp\sketch\sketch_mar17a.ino.cpp" -o ""
avr-g++: error: missing filename after '-o'
Usando librería Bounce2-master con versión 2.1 en la carpeta: C:\Users\TallerGeosat\Documents\Arduino\libraries\Bounce2-master
exit status 1
Error de compilación
Mal instalada cesar!!!!!!!!!!
Mira bien como se instala una librería.
Hay tutos por todos lados.
Ya instale Bounce1
me reconoce la libreria pero
Bounce2 no
tengo un Arduino UNO R3..
estos son los archivos dentro de la
examples
Bounce1.zip
Bounce2.cpp
Bounce2.h
BouncySwitch_lockout.png
BouncySwitch_stable.png
INSTALL.txt
library.properties
LICENSE
README.md
Tiene error al leer los botones.
#include <Bounce2.h>
const int boton1 = 4;
const int boton2 = 6;
const int tiempoAntirebote = 10;
const int luz = 13;
//defines
Bounce debouncer1 = Bounce();
Bounce debouncer2 = Bounce();
int cuenta = 0;
int estBot1;
int estBotAnt1;
int estBot2;
int estBotAnt2;
int salida1;
int salida2;
int ig = 0;
int st = 0;
int contador;
int tiempo = 15;
int estado = 0;
int estado2 = 0;
void setup (){
Serial.begin(9600);
pinMode(boton1,INPUT);
pinMode(boton2,INPUT);
// After setting up the button, setup the Bounce instance :
debouncer1.attach(boton1);
debouncer1.interval(10); // interval in ms
// After setting up the button, setup the Bounce instance :
debouncer2.attach(boton2);
debouncer2.interval(10); // interval in ms
pinMode(luz,OUTPUT);
}
void loop () {
/// Lectura de botones //////////////////
//---------Boton #1-----------/////////
// Actualizo los botones
debouncer1.update();
debouncer2.update();
// Get the updated value :
estBot1 = debouncer1.read();
if (estBot1 == HIGH) {
salida1= !salida1;
}
// Update the Bounce instance :
//---------Boton #2-----------/////////
debouncer2.update();
// Get the updated value :
estBot2 = debouncer2.read();
if (estBot2 == HIGH) {
salida2= !salida2;
}
/// Parte del codigo ///
if (salida1 && contador <(tiempo+tiempo)){
delay(500);
contador++;
estado = 1;
} else
if (estado == LOW){
salida2= 0;
} else {
estado = 0;
contador = 0;
}
if (contador==(tiempo*2) || salida2 == HIGH){
contador = 0;
salida1 = 0;
}
digitalWrite(luz, estado);
Serial.print("contador: ");
Serial.println(contador);
Serial.println();
Serial.print("estado: ");
Serial.println(estado);
Serial.println();
}
Esfuerzate un poco.. yo ya te guié por donde debes trabajar.
Ejecuta los ejemplos, prueba como funcionan, aprende como trabaja la librería y lo resolverás.
hola mira lo hice de la manera convencional, no puede instalar la libreria Bounce2
quiera un poco de ayuda con la libreria Bounce
ya que es la unica libreria que me reconoce el IDE de arduino