Introduction: Infra Red Vest Code

About: Happiness is invisible? Then you cannot sell it? You cannot copy it nor like it:-) You have to arrive there yourself??? But i am just a consumer! See Baudrillard, La societe de consommation, p60....
This instructable is about the code and the devices used for the infra red vest. The sewing you have to invent yourself!



This vest shows the infrared codes which are bombarding us all the time in rooms. Infrared is used for the remote controls of television sets, computers, video/dvd players, lamps, robots, and so on. The codes are sent at a frequency of 38 kHz.

The signal itself is sent within the light spectrum range of infra red which is invisible for the human eye, but visible when you point your digital camera on the led. Cameras but also insects (for instance) do have infra red vision.

Some remote controls are sending several codes, or are in regular contact with the device they are supposed to control.
The codes are just a sequence of 1's and 0's. All the codes are different, some have an elaborate signature at the beginning and the different buttons on the remote control make different endings.

When a infra red code is detected by the vest the led's light up. One shoulder is showing the reception, the other shoulder is showing the code.

The vest provide enough space for a large collection of remote controls as a belt and in the pocket at the right hand side, where also the chips and the batteries are hidden.

Step 1: Test Arduino Code

The challenge is to get all the necessary code on a stand alone atmega328 chip using AVR Gcc.
The problem is, we have the code for the flexible LED strip in C ...

see my instructable: https://www.instructables.com/id/Making-a-flexible-ring-of-LEDs/

... but we have the infra red library in Arduino code! Do you think these two bits of code can be merged like that? Wrong!


So first get our infra red sketch working on the Arduino.

In the Jeelabs.org store you can buy a infra red plug and in the library Ports of Jeelabs.org there are examples getting the infra red signal.
These is all very convenient, and it works, but...you have not learnt too much! Certainly not how to port it, merge it with other code.

Infra red devices:
You can also, and rather easily, make the receiver and sender yourself. The special IR diode with frequency controller (only receiving the 38 kHz) is about 1.60 euro at conrad.nl and works right away.
The sender needs a 555 timer to send at 38kHz, and you can find the schematics everywhere, at jeelabs.org, but also here:
link:
http://davebodnar.com/railway/Pulsed_IR_Article/

Porting the code
If you start exploring the infra red part of Ports the first thing you notice is the difference in digitalWrite and digiWrite of Jeelabs. Since my goal is AVR C code in a AVR project I have to rewrite the digiWrites in digitalWrites (and then to C in the next step). This is not too difficult, because you can find an easy conversion table at the Jeelabs site:
http://jeelabs.net/projects/hardware/wiki/JeeNode

Functions left
Ports is in C++ and we go to Arduino C, so the c++ objects have to be rewritten. This is also easy, no real object structure is used. So we get a Arduino script with the functions from the library, not using the library Ports anymore.

These are the functions left for infrared use:

configure(uint8_t slot4, uint8_t gap256);
poll();
done();

and we need some globals:
uint8_t slot, gap, buf [40];
char fill;
uint32_t prev;

Now this char fill variable works in Arduino code, but not in AVR Gcc! It has to be changed in an int, since it uses negative values...

If you have the infra red diode you can test with the following sketch:
http//:www.contrechoc.com/instructables/infra_red_arduino_code.pde

where PC3, or analog 3, or digital 17 is the input pin for the infra red diode device (used here as a digital PIN).
I have used pin 6 and 7 to power my liquid display for testing.

You can see I included libraries used by the functions from the Ports lib:
WProgram.h
stdint.h
avr/pgmspace.h
util/atomic.h

Otherwise you get errors on ATOMIC STATE etc.

(In this sketch the fill is still a char.)

Checking
Checking the infra red code working from the Arduino is easy: just use the serial window.

Checking from avr gcc is not so straightforward, so already in the Arduino Code phase I added a Liquid Crystal to check if the Serial output was also appearing on the small Liquid Crustal screen. (This Liquid Crystal is attached to a Attiny2313.)

Hidden Arduino files
Arduino code is convenient if you compare it to AVR C. This making it easy is the big success of the Arduino! But going one small step further like me, and you have to understand a bit how this success was made possible.
So behind the ARduino srcipt is lain C.
Ports uses also the special Arduino files (hidden in the arduino folders and linked with an invisible makefile).
For instance:
In AVR Gcc i cannot use digitalWrite etc unless either I make my own function or use the Arduino files. Likewise for the function micro's.



Step 2: Re-using Arduino Files for Micro and the Timer

You can get the Arduino functions in AVR gcc if you want to, just copying the parts you need from the Arduino lib inside the Arduino folder. For some reason I keep using the Arduino 018 versions so in the folder, but that does not matter too much.

You can find the files needed in the Arduino folder:

hardware/arduino/cores/arduino (yes two times arduino)
there you find the arduino files, which saves a lot of people learning the C of AVR :-), but at a cost of speed and size.

I needed the micro functions and the interrupt, which I found in wiring.c
I copied: (also copying the variables and defines I needed from wiring.h)

SIGNAL(TIMER0_OVF_vect)
unsigned long millis()
unsigned long micros()

but very important, to get the timer/interrupt started is the:
void init()
There is a lot of code you can skip if you know you use a atmega328.

Then we get this code in two nice files, a source and a header:
http://http//:www.contrechoc.com/instructables/infra-red.zip
(This is a total AVR project you can use for further fun.)

Also included here are, a UART.c and UART.h, for communication with the liquid crystal.

You see I have made the fill an int variable.

Also the analog read is activated for an LDR, and one pin is used to make the other shoulder light up when a signal at 38kHz is received.

initTimers(); is used to start the interrupts, the init() function from wiring.c

In the header file you find the connections with the groups of LED's (flexible LED strips)
//connections to PIN's from the led circle
unsigned char  rows[8] = { 7, 6, 5, 4, 3 , 2, 1, 0 };
unsigned char  cols[8] = { 15, 14, 13, 12, 11, 10, 9, 8 };

The interrupt function is different from the flexible LED strips I used before:
SIGNAL(TIMER0_OVF_vect)

In this function, besides the "Arduino code" I have added my own interruptFie();
This function takes care of the LED block MATRIX activities.

In the setIRPattern(uint8_t count) function you see the matrix for the LED's on the shoulder band.
The 3's indicate the missing LED's form the 64 (I didn't use the full 64 = 8 x 8 LED's in the vest.)





Step 3: One Last Small Challenge

The flexible LED's were functioning a the left shoulder showing the code.
The Atmega328 worked on 2 AAA batteries at 2.8 Volts and the LED's were used without resistors. (On the Arduino with 5V you have to use a resistor not to let your LED's explode.)

I wanted a quick solution at the other shoulder, which will be replaced of course!

So there was a cheap set of 20 LED's running on three AA batteries. Christmas LED's! For about 2 euro's:

Using this is simple, see this tiny instructable for that:
https://www.instructables.com/id/Simple-Transistor-Switch/

The trick was the voltage: these LED's run on 5V, but the LED's in the vest on 3V. So how do we solve that? The three batteries for the group of LED's can also be connected using only 2 batteries for the atmega328 and the blue LED's!

Batteries are always the hidden challenge of smart textile and other electronic projects.

Step 4: Finishing!

So we have a vest showing the codes of the remote controls trying to control our devices and who knows, trying to control us.

The main thing we learnt is getting functions from Arduino and other libraries to AVR Gcc code projects.

Shortly we will add a nice video!