Week13: Input Devices

Assignment


Input Device and Board

I tried to add Ultrasonic Distance Sensor to my week10's Output-Device board. To read signal form Sensor, FTDI pins are added instead of power supply pins. My week10's board had 3 LED, but only one is used in this assignment.
Ultrasonic Distance Sensor Week10 Input Devices

Board Design
I designed a board by Eagle and milled by SRM-20.
R1: Resistance 10k [ohm](1002) x 1
C1: Capacitor 1u [F] x 1
IC1: Attiny 44 x 1
JP1 ISP: Pin header 2x3 x 1
FTDI: Pin header 1X4 x 1
HC-SR04: Pin holder 1X4 x 1
LED: WS2812B x 3
R2: Resistance 100 [ohm](101) x 3
C2: Capacitor 0.1u [F] x 3
download .sch .brd


After making tool path by Fabmodule, board is milled like below.

C-Code and python

I download "hello.HC-SR04.c" from class page and change some lines for this board.

PA1 is connected to FTDI's RT(= ATtiny's TX)
PB0 is connected to Sensor's Trigger
PB1 is connected to Sensor's Echo

hello.HC-SR04.c myhello.HC-SR04.c
#define serial_port PORTB
#define serial_direction DDRB
#define serial_pin_out (1 << PB2)
#define trigger_port PORTB
#define trigger_direction DDRB
#define trigger_pin (1 << PB3)
#define echo_pins PINB
#define echo_direction DDRB
#define echo_pin (1 << PB4)
#define serial_port PORTA
#define serial_direction DDRA
#define serial_pin_out (1 << PA1)
#define trigger_port PORTB
#define trigger_direction DDRB
#define trigger_pin (1 << PB0)
#define echo_pins PINB
#define echo_direction DDRB
#define echo_pin (1 << PB1)

"hello.HC-SR04.make" is also changed. Make fuse line is copied from week8's hello.ftdi.44.echo.c.make.
  PROJECT=myhello.HC-SR04
  MMCU=attiny44
  avrdude -p t44****

  program-usbtiny-fuses: $(PROJECT).hex avrdude -p t44 -P usb -c usbtiny -U lfuse:w:0x5E:m

when I compile, error comes out.
$ make -f myhello.HC-SR04.make 
avr-gcc -mmcu=attiny44 -Wall -Os -DF_CPU=8000000 -I./ -o myhello.HC-SR04.out myhello.HC-SR04.c
myhello.HC-SR04.c: In function 'main':
myhello.HC-SR04.c:141:7: error: 'TIFR' undeclared (first use in this function)
       TIFR |= (1 << TOV0);
       ^
myhello.HC-SR04.c:141:7: note: each undeclared identifier is reported only once for each function it appears in
make: *** [myhello.HC-SR04.out] Error 1  
The problem is 'TIFR' undeclared. I could find how to fix from FabAcademy graduate's page. I need to replace "TIFR" in ATtiny45 by "TIFR0" in ATtiny44.

In "hello.HC-SR04.c" which for ATtiny45, TIFR(Timer/Counter Interrupt Flag Register) and TOV0(Timer/Counter0 Overflow Flag) are used.
In ATtiny45's data sheet 11.9.8, the below is written.

On the other hand, TIFR0 and TOV0 are written in ATtiny44's data sheet 11.9.7.
"TIFR0" is used in ATtiny44 instead of "TIFR". After I change this, I could compile "myhello.HC-SR04.c".
$ make -f myhello.HC-SR04.make 
$ sudo make -f myhello.HC-SR04.make program-usbtiny-fuses 
$ sudo make -f myhello.HC-SR04.make program-usbtiny
$ ls /dev | grep usb 
cu.usbserial-AI02RJUC
tty.usbserial-AI02RJUC
$ python hello.HC-SR04.py /dev/tty.usbserial-AI02RJUC

download .c .make

Arduino Compatible

I change this board to Arduino Compatible as week8.

This board uses two library that are "SoftwareSerial.h" and "Adafruit_NeoPixel.h". Because of memory shortage on ATtiny44 both at same time can not be used. Arduino IDE says "Sketch use 133% of flash memory". So I need to choose one of them each time.

Measure Distance
This is a program that "SoftwareSerial.h" used.

download .ino

Change color by Distance
This is a program that "Adafruit_NeoPixel.h" used. When sensor comes close to cat, LED color changes green to red.

download .ino