Projeto Arduino
Gostaria de reagir a esta mensagem? Crie uma conta em poucos cliques ou inicie sessão para continuar.

IMPLEMENTAÇÃO CÓDIGO COM DEBOUNCE

Ir para baixo

IMPLEMENTAÇÃO CÓDIGO COM DEBOUNCE Empty IMPLEMENTAÇÃO CÓDIGO COM DEBOUNCE

Mensagem  RSALVINO Dom 31 Mar - 16:16

Olá amigos,

Estou tentando inserir uma rotina de envio de SMS trabalhando com Debounce.

Quero que seja enviado um SMS somente após 5 segundos em que o sensor de nível (liguei pino dig 4) estiver acionado.

Após pesquisas realizadas, vi que a melhor forma de fazer isto é através de Debounce, porém da forma que estou fazendo, ficam sendo enviados constantes SMS e não somente após os 5 segundos de acionamento do sensor, poderiam me indicar onde estou errando?

Segue o código que estou utilizando:

// Inicio do Programa

#include //Biblioteca NewSofSerial, usada para comunicação serial.

SoftwareSerial mySerial(2, 3); //RX, TX

//ligar pino 1 (placa GSM) no pino 3 ARDUINO
int powerkey = 5;
int statuspin = 6;
int pinState = 0;

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 4; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin

// Variables will change:
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 5000; // the debounce time; increase if the output flickers

void setup() {
pinMode(powerkey, OUTPUT);
pinMode(statuspin, INPUT);
mySerial.begin(9600); // the GPRS baud rate
Serial.begin(9600);

mySerial.println("AT");
delay(500); // the GPRS baud rate
mySerial.println("AT");
delay(500); // the GPRS baud rate

// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}

void loop() {

pinState = digitalRead(statuspin);
if(pinState==LOW){
digitalWrite(powerkey, HIGH); // set the LED on
delay(2000);
digitalWrite(powerkey, LOW);
}

// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);

// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:

// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}

if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
buttonState = reading;
}

// set the LED using the state of the button:
digitalWrite(ledPin, buttonState);

mySerial.println("AT+CMGF=1");
Serial.println("Enviando Mensagem...");
delay(1000);
mySerial.println("AT+CMGS=\"+031xxxxxxxx\"");
delay(2000);
mySerial.print("Nível do tanque atingido");
delay(2000);
mySerial.write(26);//Equivalente a CTRL+Z, para fazer somente um envio de SMS.

Serial.println("Mensagem Enviada");


// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonState = reading;
}

RSALVINO

Mensagens : 9
Data de inscrição : 17/02/2013

Ir para o topo Ir para baixo

Ir para o topo

- Tópicos semelhantes

 
Permissões neste sub-fórum
Não podes responder a tópicos