2016/04/29

RF Remote

Code:

// Modified by Befun Hung on Apr. 29, 2016
// 1. Use RadioHead Library
// 2. Max Length of Digits is 20 Characters.
// 3. Numbers followed by '*' to Switch On, '#' to Switch Off
//    Example: 23456789* To Swith On Device 23456789, 23456789# To Switch Off Device 23456789
// Modified by Befun Hung on Oct. 28, 2014   
// Blink led after sending message to notice user 5 times

#include <Keypad.h>
#include <RH_ASK.h>
#include <SPI.h>

RH_ASK driver;

#define maxLength 20
#define keyOn '*'
#define keyOff '#'

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','#'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = {5, 4, 3, 2};
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = {8, 7, 6};    // connect to the column pinouts of the keypad
char keyins[maxLength+1] = "";
int i=0, j=0, sendTimes=5;
int led=13;

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup() {
  Serial.begin(115200);
  if (!driver.init()) Serial.println("init failed");
  Serial.println("Ready!");
  pinMode(led, OUTPUT);
  digitalWrite(led, HIGH);
  delay(20);
  digitalWrite(led, LOW);
  delay(20);
}
  
void loop() {
    char key = keypad.getKey();
    if (key) {
      Serial.print(key);
      keyins[i] = key;
      i++;
      if (key == keyOn || key == keyOff) {
        for (j=0;j<sendTimes;j++) {
          Serial.println();
          send(keyins);
        }
        digitalWrite(led, HIGH);
        delay(20);
        digitalWrite(led, LOW);
        delay(20);
        i = 0;
      } 
    }
}

void send(char *message)
{
  driver.send((uint8_t *) message, strlen(message));
  driver.waitPacketSent();
}

No comments:

Post a Comment