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.

2016/01/13

6-digit 7-segment LED Counter Display

I need a 6-digit 7-segment LED module to display time and found Sparkfun has an example sketch for 4-digit 7-segment LED display module. So I just modify the example sketch to meet my requirement. The photos taken cannot display actual image saw by human eyes.

Photos



Parts

Arduino Uno R3
6-digit 7-segment LED Display Module KYX-3662BS
Some Wires

LED Module Pinouts


Sketch

/*
 6-13-2011
 Spark Fun Electronics 2011
 Nathan Seidle

 Original used for 4-digit 7-segment LED module
 Modified by Befun Hung on Jan. 13, 2016 to Extend to 6-digit

 This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).

 4 digit 7 segment display:
 http://www.sparkfun.com/products/9483
 Datasheet: 
 http://www.sparkfun.com/datasheets/Components/LED/7-Segment/YSD-439AR6B-35.pdf

 This is an example of how to drive a 7 segment LED display from an ATmega without the use of current limiting resistors.
 This technique is very common but requires some knowledge of electronics - you do run the risk of dumping too 
 much current through the segments and burning out parts of the display. If you use the stock code you should be ok, but 
 be careful editing the brightness values.

 This code should work with all colors (red, blue, yellow, green) but the brightness will vary from one color to the next
 because the forward voltage drop of each color is different. This code was written and calibrated for the red color.

 This code will work with most Arduinos but you may want to re-route some of the pins.

 7 segments
 6 digits
 1 colon
 =
 14 pins required for full control 
 */

// LED module used in this example is KYX-3662BS which is common anode 
int digit1 = 11; // 6-digit 7-segment LED pin 1
int digit2 = 10; // 6-digit 7-segment LED pin 2
int digit3 = 9;  // 6-digit 7-segment LED pin 3
int digit4 = 6;  // 6-digit 7-segment LED pin 4
int digit5 = 2;  // 6-digit 7-segment LED pin 5
int digit6 = 12; // 6-digit 7-segment LED pin 6

//Pin mapping from Arduino to the ATmega DIP28 if you need it
//http://www.arduino.cc/en/Hacking/PinMapping
int segA = A1; // 6-digit 7-segment LED pin 14
int segB = 3;  // 6-digit 7-segment LED pin 13
int segC = 4;  // 6-digit 7-segment LED pin 12
int segD = 5;  // 6-digit 7-segment LED pin 11
int segE = A0; // 6-digit 7-segment LED pin 10
int segF = 7;  // 6-digit 7-segment LED pin  9
int segG = 8;  // 6-digit 7-segment LED pin  8

void setup() {                
  pinMode(segA, OUTPUT);
  pinMode(segB, OUTPUT);
  pinMode(segC, OUTPUT);
  pinMode(segD, OUTPUT);
  pinMode(segE, OUTPUT);
  pinMode(segF, OUTPUT);
  pinMode(segG, OUTPUT);

  pinMode(digit1, OUTPUT);
  pinMode(digit2, OUTPUT);
  pinMode(digit3, OUTPUT);
  pinMode(digit4, OUTPUT);
  pinMode(digit5, OUTPUT);
  pinMode(digit6, OUTPUT);
  
  pinMode(13, OUTPUT);
}

void loop() {
  
  //long startTime = millis();

  displayNumber(millis()/1000, 6);
  displayNumber(millis()/100000, 4);
  displayNumber(millis()/10000000, 2);

  //while( (millis() - startTime) < 2000) {
  //displayNumber(1217);
  //}
  //delay(1000);  
}

//Given a number, we display 10:22
//After running through the 4 numbers, the display is left turned off

//Display brightness
//Each digit is on for a certain amount of microseconds
//Then it is off until we have reached a total of 20ms for the function call
//Let's assume each digit is on for 1000us
//If each digit is on for 1ms, there are 4 digits, so the display is off for 16ms.
//That's a ratio of 1ms to 16ms or 6.25% on time (PWM).
//Let's define a variable called brightness that varies from:
//5000 blindingly bright (15.7mA current draw per digit)
//2000 shockingly bright (11.4mA current draw per digit)
//1000 pretty bright (5.9mA)
//500 normal (3mA)
//200 dim but readable (1.4mA)
//50 dim but readable (0.56mA)
//5 dim but readable (0.31mA)
//1 dim but readable in dark (0.28mA)

// displayNumber() has added i parameter to display only 2 digit on specific position
void displayNumber(int toDisplay, int i) {
#define DISPLAY_BRIGHTNESS  500

#define DIGIT_ON  HIGH
#define DIGIT_OFF  LOW

  long beginTime = millis();

  for(int digit = i ; digit > i-2 ; digit--) {
    // digitalWrite(13, HIGH);

    //Turn on a digit for a short amount of time
    switch(digit) {
    // case 0:
      // digitalWrite(13, HIGH);
      // break;
    case 1:
      digitalWrite(digit1, DIGIT_ON);
      break;
    case 2:
      digitalWrite(digit2, DIGIT_ON);
      break;
    case 3:
      digitalWrite(digit3, DIGIT_ON);
      break;
    case 4:
      digitalWrite(digit4, DIGIT_ON);
      break;
    case 5:
      digitalWrite(digit5, DIGIT_ON);
      break;
    case 6:
      digitalWrite(digit6, DIGIT_ON);
      break;
    }

    //Turn on the right segments for this digit
    lightNumber(toDisplay % 10);
    toDisplay /= 10;

    delayMicroseconds(DISPLAY_BRIGHTNESS); //Display this digit for a fraction of a second (between 1us and 5000us, 500 is pretty good)

    //Turn off all segments
    lightNumber(10); 

    //Turn off all digits
    digitalWrite(digit1, DIGIT_OFF);
    digitalWrite(digit2, DIGIT_OFF);
    digitalWrite(digit3, DIGIT_OFF);
    digitalWrite(digit4, DIGIT_OFF);
    digitalWrite(digit5, DIGIT_OFF);
    digitalWrite(digit6, DIGIT_OFF);
    digitalWrite(13, LOW);
  }

  while( (millis() - beginTime) < 10) ; //Wait for 20ms to pass before we paint the display again
}

//Given a number, turns on those segments
//If number == 10, then turn off number
void lightNumber(int numberToDisplay) {

#define SEGMENT_ON  LOW
#define SEGMENT_OFF HIGH

  switch (numberToDisplay){

  case 0:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_OFF);
    break;

  case 1:
    digitalWrite(segA, SEGMENT_OFF);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_OFF);
    break;

  case 2:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_OFF);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 3:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 4:
    digitalWrite(segA, SEGMENT_OFF);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 5:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_OFF);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 6:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_OFF);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 7:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_OFF);
    break;

  case 8:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 9:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 10:
    digitalWrite(segA, SEGMENT_OFF);
    digitalWrite(segB, SEGMENT_OFF);
    digitalWrite(segC, SEGMENT_OFF);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_OFF);
    break;
  }
}

2016/01/02

Burning Arduino Uno Optiboot Bootloader Using Duemilanove Compatible

This is the note recording the procedure I used burning the Optiboot bootloader to ATMEGA328P-PU microcontroller.

Tools used:

1. Duemilanove ( no need for adding a 10uF capacitor between rest and ground) compatible Justduino TTL ( I made a few years ago) with ArduinoISP uploaded (since Justduino TTL been made).
2. AVR ISP Shield (no need for wiring, more productive and error free).
3. Arduino IDE 0.022 and 0.023 (In Arduino IDE 1.0 and above, ArduinoISP sketch need to be modified, compiled and uploaded) .

Procedure:

1. Stack AVR ISP Shield upon Justduino TTL.
2. Power the Justduino TTL by plugging FT232 Cable compatible cable into USB port on computer, the PLS led on the AVR ISP Shield starts breathing:) ( Kit Review - AVR ISP Shield on tronixstuff.com) .
3. Open Arduino IDE 0.022 or 0.023 (I used both without fail).
4. Load ATMEGA328P-PU IC upon 28-pin ZIF socket and pull ZIF lever down.
5. Select the items in the Tools > Serial Port menus that correspond to the board you are using as the programmer.
6. Select the item in the Tools > Board menu that corresponds to the board (Arduino Uno) on which you want to burn the bootloader.
7. Select the w/ Arduino as ISP in the Tools>Burn Bootloader.
8. The PROG led will light on during the burning process, the process takes about one minute.

That's it. Easy!

After inserting the ATMEGA328P-PU upon the socket on the Justduino TTL, the D13 led doesn't blink the same way as the new Arduino Uno dose. I just compile the example blink sketch and upload it in Arduino IDE 1.0.5. Now, the D13 led blinks the same way as the new Arduino Uno does.

I find that I cannot re-burn the bootloader to the ATMEGA328P-PU.

Next Step:

I want to buy some Yamaichi IC51-0324-1498 socket and make some TQFP32 to TQFP28 adapters to stack upon the AVR ISP Shield for burning Optiboot bootloader to ATMEGA328P-AU for use on the coming projects featuring watchdog and software reset.

2015/12/24

MakerNTP - A Low Cost Arduino Mega2560 GPS-based Stratum 1 NTP Server

My homebrew low locst (not more than 120 USD for retail) MakerNTP is now in operation for about one month without any problem in my home environment. The features of MakerNTP are:

Protocol: SNTP V4
Performance: 40 requests/second
Ethernet Port: 1
Default IP: 192.168.1.123
Power Supply: 9V (recommended) / 5V (USB)
Enclosure: ABS
Application: home automation, security surveillance ......

NOTE: Most SNTP clients will generally sync your system time to within 100 milliseconds of the correct time - often within 10 milliseconds. Of course, the accuracy is highly dependent on the accuracy of the server that it is connecting to.

Photos



2015/11/03

Convert Millisecond (Decimal Fraction) to 32-bits Binary Using Arduino

The function millis2bin() can be used to convert millisecond to Network Time Protocol (NTP) timestamp fraction (32-bits unsigned long) used by both NTP servers and clients.

Code

/*
  Program for convert millisecond (decimal fraction) to 32-bits binary 
  By Befun Hung on Nov. 2, 2015
 */

void setup() {   
  
  Serial.begin(115200);
  
  int i;
  unsigned long tempval=1;
  for (i=0;i<32;i++) {
    Serial.print(i);
    Serial.print(" ");
    Serial.print(tempval << (31-i));
    Serial.print(" ");
    tempval = 1;
    Serial.println(tempval << (31-i),BIN);
  }
  for (i=0;i<1000;i++) {
    Serial.print(i);
    Serial.print(" ");
    Serial.println(millis2bin((float) i/1000),BIN);
    // millis2bin((float) i/1000);
  }
}

unsigned long millis2bin( float fraction) {
  int j=0;
  unsigned long return_value = 0;
  unsigned long one = 1;
  
  while (fraction != 0 && j < 32) {
    fraction = fraction * 2.0;
    if (fraction >= 1.0) {
      return_value = return_value | (one << (31 - j));
      fraction = fraction -1;
    }
    j++;
  }
  // Serial.print(return_value);
  // Serial.print(" ");
  // Serial.println(return_value,BIN);
  return return_value;
}

// the loop routine runs over and over again forever:
void loop() {
}

Serial Monitor Output


2015/04/26

Parts List of Shapeoko 2 Compatible MakerCarve Mechanical Kit

The parts list of Shapeoko 2 compatible MakerCarve mechanical kit is listed with quantity as follows:

1. MDF x 2
2. X/Y Axis Rail Extrusion x 4
3. Z-Axis Rail Extrusion x 1
4. 20x20x527mm Extrusion x 2
5. Motor Mount Plate x 4
6. End Plate x 4
7. Z-Axis Motor Mount Plate x 2
8. Spindle Mount Plate x 1
9. Z-Axis Shim Plate x 1
10. Universal Spindle Mount And Mount Strap x 2
11. Delrin V-Wheel x 20
12. Smooth Idler Wheel x 6
13. M5 Shim Washer x 26
14. 5x16x5 Bearing (625-2RS) x 52
15. Eccentric Nut x 10
16. M5 Flat Washer x 108
17. Delrin Lead Nut x 1
18. M8x200mm Threaded Rod x 1
19. 8x22x7 Flange Bearing (F608-ZZ) x 1
20. M3x12mm, SHCS x 3
21. M3 Flate Washer x 3
22. Wave Disc Spring x 1
23. 3x8mm Nylon Spacer x 6
24. 5x10mm Nylon Spacer x 4
25. Z-Axis Coupler 5mm-8mm x 1
26. M3x50mm Standoff Spacer x 3
27. M3x10mm, Set Screw x 3
28. Fiberglass Belt Clip x 6
29. 3/8"x5/16" #12 Spacer x 10
30. 3/8"x1" #12 Spacer x 6
31. 3/8"x1-1/4" #12 Spacer x 6
32. M5x10mm, BHCS x 4 (Z-Axis Extrusion)
33. M5x12mm, BHCS x 38
34. M5x20mm, BHCS x 18 (Motor Mount Carriage V-Wheel x 12 + Z Assembly Carriage V-Wheel x 4 + Z-Axis Motor Mount x 2)
35. M5x30mm, BHCS x 10 (Motor Mount Carriage Idler x 6 + Spindle Carriage V-Wheel x 4)
36. M5x8mm, SHCS x 6 (Belt Clip)
37. M5x45mm, SHCS x 4 (Universal Spindle Mount Strap)
38. M5x70mm, SHCS x 6
39. M5 Flat Nut x10 (Z-Axis Extrusion x 4 + Belt Clip x 6)
40. M8 Flat Nut x 2 (M8 Threaded Rod)
41. M5 Hex Nut x 26
42. M5 T-Nut x 16 (20x20x527mm Extrusion - MDF And End Plate)
43. Spare Parts Pack x 1 (M5 Flat Washer x 12 + M5 T-Nut x 2)

Update (Jul. 25, 2015)  The following is the packing list of MakerCarve mechanical kit:

Update (Nov. 28, 2016) Shapeoko 2 Assembly Video

Shapeoko 2 Works Kit Tutorial - Desktop CNC 3D Carver Router by Inventables

https://www.youtube.com/watch?v=Yy6A3Mif0QY