Hello Arduino community, I'm trying to create a one way communication with one transmitter and many recievers using the virtual wire library. (Like one of those pager systems at restaurants that tell you when your table is ready.)
I have three arduino uno's a 4x4 matrix keypad, rbg leds and xd fs1000a 5v 433mhz transmitter and receivers. (In the future I want to have thousands of receivers using cheaper attiny or something similar.) Each receiver is equipped with a rbg led, when it recieves the correct code it should light the corresponding led. I also tried to have a function to save a new id number for the receiver; and a function to turn all of the receivers coresponding leds on or off.
I don't have internet and am writing this on my phone so please forgive me,
//This is my transmitter
#include <VirtualWire.h>
#include <Keypad.h>
const byte ROWS = 4;
const byte COLS = 4;
char keys [ROWS] [COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins [ROWS] ={
9, 8, 7, 6};
byte colPins [COLS] ={
5, 4, 3, 2};
Keypad keypad = Keypad (makeKeymap (keys), rowPins, colPins, ROWS, COLS );
byte ledPin = 11;
const int transmit_pin = 12;
#define Message_Length 12
char Data[Message_Length];
byte dataCount = 0;
void setup() {
vw_set_tx_pin (transmit_pin);
Serial.begin (9600);
pinMode (ledPin, OUTPUT);
keypad.addEventListener (keypadEvent);
vw_setup (1200);
}
void loop() {
char key = keypad.getKey();
if ( key) {
Data [dataCount] = key;
Serial.println (Data [dataCount]);
dataCount++;
}
}
void keypadEvent (KeypadEvent key){
switch (keypad.getState()) {
case PRESSED:
if (key =='#') {
digitalWrite (ledPin, HIGH);
vw_send ((unit8_t *) Data, 12);
vw_wait_tx();
clearData();
digitalWrite (ledPin, LOW);
delay (1000);
}
if (key == 'C') {
clearData();
}
break;
}
}
void clearData() {
while (dataCount !=0)
{
Data [dataCount--] = 0;
}
return;
}
This works well except the clearData function. It will leave either a 'C' or a '#' in the buffer after the first transmition or clear function. I worked around this by ignoring the zero index on the receiver end.
This is my receiver
#include <VirtualWire.h>
String panId ="#0000";
int ledRed = 5;
int ledGreen = 10;
int ledBlue = 6;
void setup() {
Serial.begin (9600);
Serial.println ("setup");
vw_set_rx_pin (11);
vw_setup(1200);
vw_rx_start();
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(10, OUTPUT);
}
void loop() {
unit8_t buf[VW_MAX_MESSAGE_LEN];
unit8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)) {
int i;
Serial.print("Got");
for (i = 0; i < buflen; i++) {
char c = (buf[i];
Serial.print (c);
Serial.print(" ");
// turns all receivers leds off
if (buf[6] =='6') {
digitalWrite (ledRed, LOW);
digitalWrite (ledGreen, LOW);
digitalWrite (ledBlue, LOW);
}
// turns all receivers green led on
if (buf[6]=='8') {
digitalWrite (ledGreen, HIGH);
}
//nothing from here down seems to work
if (buf[1] ==(panId[1])) {
if (buf[2]== (panId[2])) {
if (buf[3]== (panId[3])) {
if (buf[4]== (panId[4])) {
if (buf[6]== '1') {
analogWrite (ledGreen, 200);
}
if (buf[6]== '2') {
analogWrite (ledBlue, 50);
}
if (buf[6]=='3') {
analogWrite (ledRed, 150);
}
if (buf[6]=='4') {
digitalWrite (ledRed, LOW);
digitalWrite (ledGreen, LOW);
digitalWrite (ledBlue, LOW);
}
// trying to save a new panId
if (buf[6]=='7') {
panId[1] = buf[7];
panId[2] = buf[8];
panId[3] = buf[9];
panId[4] = buf[10];
Serial.print(panId);
}
}
}
}
}
}
}
}
The receiver code can only turn all green leds on and off. I can see the received code on the serial monitor and everything coming from the transmitter looks good. But the individually addressed functions don't work yet. Am I using to many if statements? Is there an easier way? I'm new to coding and trying to learn every day, so I thank you in advance for your help and patients.
Sincerely Travis Doyle travisdoylecdt@gmail.com