hi all,
I am new to arduino and wanted to build my first project a arduino controlling a relai (on pin 11) with a 433mhz wirless signal.
first i have made this program but it only works with one caracter (i need more for secuitity / so that i not control it with a other arduino by accident)
here the code:
#include <VirtualWire.h>
void setup()
{
Serial.begin(9600); // Debugging only
Serial.println("setup");
// Initialise the IO and ISR
vw_setup(2000); // Bits per sec
vw_set_rx_pin(9);
vw_rx_start(); // Start the receiver PLL running
pinMode(11, OUTPUT);
}
void loop()
{
digitalWrite(11, LOW);
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)) // Non-blocking
{
int i;
digitalWrite(13, true); // Flash a light to show received good message
// Message with a good checksum received, dump it.
Serial.print("Got: ");
for (i = 0; i < buflen; i++)
{
Serial.print(buf[i]);
if(buf[i] == '~'){digitalWrite(11, HIGH);}
delay(220);
}
Serial.println("");
digitalWrite(13, false);
}
}
and for the transmitter:
#include <VirtualWire.h>
void setup()
{
Serial.begin(9600); // Debugging only
Serial.println("setup");
// Initialise the IO and ISR
vw_setup(2000); // Bits per sec
vw_set_tx_pin(2);
pinMode(11, INPUT);
digitalWrite(11, HIGH);
}
void loop()
{
char *msg;
digitalWrite(6, HIGH);
delay(20);
if(digitalRead(11) == LOW){
char *msg = "~";
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
delay(20);
digitalWrite(13, false);}
}
I have found a program om this topic post: RF link transmitter and Receiver pair - Programming Questions - Arduino Forum that works (can read the signal over serial) and have modified it, but i can't control the relai on pin 11 with it...
Here the code:
transmitter
#include <VirtualWire.h> // include virtualwire libary
void setup()
{
Serial.begin(9600); // Debugging only
Serial.println("setup"); // print setup op serial
pinMode(13, OUTPUT);
vw_set_ptt_inverted(true);
vw_setup(2000); // Bits per sec
vw_set_tx_pin(2); //Tx pin (zend)
pinMode(11, INPUT); // pin 11 is input
digitalWrite(11, HIGH); // zet pin 11 hoog.
}
void loop()
{
delay(100); //Send a message every half second
if(digitalRead(11) == LOW){
char *msg = "875987";
digitalWrite(13, HIGH ); // Flash a light to show transmitting
vw_send((byte *)msg, strlen(msg));
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(13, LOW);
}
}
and receiper;
#include <VirtualWire.h>
int i =0;
int msg_received = 0;
void setup()
{
Serial.begin(9600);
Serial.println("Started");
vw_setup(2000); // Bits per sec
vw_set_rx_pin(9); //Rx pin
vw_rx_start();
pinMode(13, OUTPUT);
pinMode(11, OUTPUT);
}
void loop()
{
byte buf[VW_MAX_MESSAGE_LEN];
byte buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)) // Non-blocking
{
digitalWrite(13, HIGH);
Serial.print('\n');
Serial.println("Receiving data");
Serial.print('\n');
for (i = 0; i < buflen; i++)
{
Serial.print(buf[i]);
}
for (i = 0; i < buflen; i++)
{
buf[i] = buf[i] -48;
}
Serial.print('\n');
for (i = 0; i < buflen; i++)
{
Serial.print(buf[i]);
}
Serial.print('\n');
Serial.print("------------------------");
if(buf[i] == '875987'){digitalWrite(11, HIGH);}
delay(400);
delay(10);
digitalWrite(13, LOW);
}
}
Could you guys have a look at it and find the problem, i could find it in the forum (looked for ohr's :~ ).
[sorry for my bad english]
Thanks!
lordp