Hi all,
I'm using this code for send out a data using key pad, when i press one the first should send one and then when i press one again i received back 0. But what I see that each time that press a button on keypad I not have only one pressed but alternatly one a zero ...like a train of pulse ... is there something for adjust this ?
below reported the code for the trasmitter and the receiver:
#include <VirtualWire.h> // include la libreria per la comunicazione
#include <Keypad.h>
// definizione del Keypad
byte buttonState = LOW;
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {8, 7, 6, 5}; //Righe collegate ai PIN 8,7,6,5
byte colPins[COLS] = {4, 3, 2}; //Colonne collegate ai PIN 4,3,2
// byte buttonState = LOW;
// Inizializzazione del Keypad
Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup()
{
Serial.begin(9600);
vw_setup(1200); // setta la velocità di trasmissione a 1200 Bits per sec
vw_rx_start(); // Start the receiver PLL running
}
void loop() // loop del micro controllore
{
// Leggo il Keypad
char customKey = customKeypad.getKey();
delay(10);
if (customKey != NO_KEY)
{
char msg[1];
msg[0] = customKey;
digitalWrite(13, true); // Flash a light to show transmitting
vw_send((uint8_t *)msg, strlen(msg)); // invia la lettera a
digitalWrite(13, false); // spegni il led quando ha finito di trasmettere
}
else
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)) // Non-blocking
{
int i;
for (i = 0; i < buflen; i++)
{
if (buf[i] == '1') // controllo se il carattere è uguale ad "a"
{
buttonState = !buttonState; // se si inverti il valore se è ACCESO spegni il led se è SPENTO accendilo
if (buttonState == 0)
{
Serial.println("Led 1 Spento");
}
else
{
if (buttonState == 1)
{
Serial.println("Led 1 Acceso");
}
}
}
}
}
}
}
receiver
#include <VirtualWire.h> // includi la libreria per la comunicazione
const int ledPin4 = 4;
const int ledPin = 13; // definisci come led il builtin sul piedino 13
byte buttonState = LOW; // definisci il lo stato del pulsante se premuto o meno
// const int ledPin8 = 8; // Piedino8 collegato al relè
void setup()
{
Serial.begin(9600); // setto la velocità della seriale usb che mi serve per vedere sul mio monitor cosa fa il micro
Serial.println(" Ricevo i Dati dal Master ");
// vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(1200); // setto la velocità di ricezione a 1200 Bits per sec
// Start the receiver PLL running
vw_rx_start();
pinMode(ledPin, OUTPUT);
pinMode(ledPin4, OUTPUT);
}
void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)) // Non-blocking
{
int i;
for (i = 0; i < buflen; i++)
{
if (buf[i] == '1') // controllo se il carattere è uguale ad "a"
{
buttonState = !buttonState; // se si inverti il valore se è ACCESO spegni il led se è SPENTO accendilo
digitalWrite(ledPin, buttonState); // passo lo stato del led che si accederà e spegnera in base al valore della variabile
digitalWrite(ledPin4, buttonState);
Serial.println(buttonState);
if (buttonState == 0)
{
char msg[1];
msg[0] = buttonState;
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
// digitalWrite(13, true); // Flash a light to show transmitting
vw_send((uint8_t *)msg, strlen(msg));
vw_wait_tx(); // Wait until the whole message is gone
Serial.println("Inviato Stato Spento");
}
else
{
if (buttonState == 1)
{
char msg[1];
msg[0] = buttonState;
// const char *msg = "acceso";
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
// digitalWrite(13, true); // Flash a light to show transmitting
vw_send((uint8_t *)msg, strlen(msg));
vw_wait_tx(); // Wait until the whole message is gone
Serial.println("Inviato Stato Acceso");
}
}
}
}
}
// delay(0.96);
}
Thanks Andrea