The actual testprogram
In order to change the Ki,Kp,... values on our robot without touching it we had to establish a IR communication. The program of this communication need to answer automatically. After a long way of research en testing we were able to make a very basic program that reacted to a command by answering "Gelukt".Here is the code for the robot
#include <SerialCommand.h>
#define arduinoLED 13 // Arduino LED on board
SerialCommand sCmd; // The demo SerialCommand object
void setup()
{
Serial.begin(2400);
sCmd.addCommand("K", TestLoop); //test moet hoppa terugsturen
// sCmd.setDefaultHandler(Fout); // al de code niet herkend word stuurt hij "homo" terug
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
// Clear Timer on Compare Match (CTC) Mode
bitWrite(TCCR1A, WGM10, 0);
bitWrite(TCCR1A, WGM11, 0);
bitWrite(TCCR1B, WGM12, 1);
bitWrite(TCCR1B, WGM13, 0);
// Toggle OC1A and OC1B on Compare Match.
bitWrite(TCCR1A, COM1A0, 1);
bitWrite(TCCR1A, COM1A1, 0);
bitWrite(TCCR1A, COM1B0, 1);
bitWrite(TCCR1A, COM1B1, 0);
// No prescaling
bitWrite(TCCR1B, CS10, 1);
bitWrite(TCCR1B, CS11, 0);
bitWrite(TCCR1B, CS12, 0);
OCR1A = 210;
OCR1B = 210;
}
void loop() {
sCmd.readSerial();
}
void TestLoop() {
Serial.println("R");
}
If this code receives the character "K" it writes "R" back to the PC via the IR communication. The PC processes it further as you can see below.
And the code for the computer:
#include <SerialCommand.h>
#define arduinoLED 13 // Arduino LED on board
SerialCommand sCmd; // The demo SerialCommand object
void setup()
{
sCmd.addCommand("V", VerzendLoop);
sCmd.addCommand("R", OntvangLoop);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
// Clear Timer on Compare Match (CTC) Mode
bitWrite(TCCR1A, WGM10, 0);
bitWrite(TCCR1A, WGM11, 0);
bitWrite(TCCR1B, WGM12, 1);
bitWrite(TCCR1B, WGM13, 0);
// Toggle OC1A and OC1B on Compare Match.
bitWrite(TCCR1A, COM1A0, 1);
bitWrite(TCCR1A, COM1A1, 0);
bitWrite(TCCR1A, COM1B0, 1);
bitWrite(TCCR1A, COM1B1, 0);
// No prescaling
bitWrite(TCCR1B, CS10, 1);
bitWrite(TCCR1B, CS11, 0);
bitWrite(TCCR1B, CS12, 0);
OCR1A = 210;
OCR1B = 210;
Serial.begin(2400);
}
void loop() {
sCmd.readSerial();
}
void OntvangLoop(){
Serial.println("Gelukt");
}
void VerzendLoop() {
Serial.println("K");
}
If this code receives the caracter "R" and prints "Gelukt" on the serial monitor.
This is a film that tests this project. Please don't mind the language. It's Dutch, we are form Belgium an that is our native language (obviously....)
Short history and information
This communication took us a while to figure out.
At first we discovered the program dirt cheap wireless. This program allowed us to make a 38kHz carrier wave for our led. This meant that we could now successfully modulate the signal to the correct frequency.The program made us (me for the most part) curious. I wanted to know how the set-up managed to create a carrier wave with exactly the right frequency. After I researched the timers on the data sheet of the ATmega I discovered how it al works an I also could make the mathematical link from the 16MHz crystal and the 38kHz carrier wave made by dirt cheap wireless.
Thanks to this knowledge I was able to modify the setup to someting we could use with the 20MHz crystal.
The second thing we discovered was the SerialCommand library. This library allowed us to make commands which, if send over the serial port, would take the program into a specific loop. The library also contained a function that made it possible to process a number send with the initial command.
After a little research on this library I was able to make the test program you can see above. After further research I was able to write loops that could process numbers.
After I put all of this research together in one program I was able to establish a fully working infrared communication on our microcontroller.
Your blogmeister
Giete