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();
}

2016/04/13

Burning Optiboot Bootloader to Arduino Pro Mini Using Uno R3 with ArduinoISP

This is the memorandum recording the procedure I use burning Optiboot bootloader to Arduino Pro Mini.

The procedure:

1. Upload ArduinoISP sketch to Uno R3 as any general sketch.
2. Connect Arduino Pro Mini RST to Uno R3 D10, Vcc to Vcc, Gnd to Gnd, D11 to D11, D12 to D12 and D13 to D13 respectly.
3. Select Board ->  Arduino Uno, Programmer -> Arduino as ISP, Burn Bootloader from Tools menu. Wait for a minute or so, the status bar of Arduino IDE will show "Done burning bootloader.".

That's it.

No need to download Optiboot hex file from any website, to compile optiboot from source or to edit board.txt, Arduino Uno use Optiboot as its default bootloader and the hex file is include in the Arduino IDE ( the Arduino IDE I use is 1.0.5-r2). The parameters set for Uno in board.txt is suitable for all boards using ATMEGA328P-PU or ATMEGA328P-AU microcontroller.

I burn Optiboot to Arduino Pro Mini following the procedure above and upload the example blink sketch to the Arduino Pro Mini by selecting Tools -> Board -> Arduino Uno repeatly and successfully.

If Arduino Uno R3 with ArduinoISP uploaded is used, skip to step 2 directly. Step 2 can be done before or after plugging USB type A connector to computer.

Following the procedure, I can minimize the needed size of board and bytes occupied by bootloader and maximize the functions the bootloader provides such as watchdog timer.