Interfacing the Arduino to the IRCF360 360 degree sensor
Arduino -> IRCF360 Interface
Download Processing.org Sketch
In this tutorial, we assume you're using an Arduino Uno, Arduino Duemilanove, Nano, or Diecimila. If you have another board, read the corresponding page in your getting started guide to ensure you can upload sketches to your Arduino board.
1. Connect the Arduino to the IRCF360 as follows:
Ensure all wires are connected to a connector underneath the IRCF360 PCB.
The digital pins 2 & 3 on the Ardunio UNO will become the 'soft' serial port for controlling the IRCF360. You may like to mount the IRCF360 on wooden stand (about 20cm-30cm high) using the 4 mouting holes on PCB. This will help to avoid noisy signals caused by ground reflections.
2. Follow the "getting started" steps that came with you Ardunio. This is to ensure your Arduino UNO or other Arduino is connected to your PC using the USB cable and you are able to upload sketches without any problems.
3. Copy and paste and upload the Arduino-IRCF360 sketch in the next section on this page.
The sketch uses the soft serial port developed by Tom Igoe.
4. Read how to interface Processing sketch to Arduino -> here
and
http://arduino.cc/playground/Main/InterfacingWithHardware#Communication
5. Download and Run the associated IRCF360 processing.org sketch which interfaces with the Arduino (available here soon)
Arduino-IRCF360 Sketch
Copy and paste all code below this line to your Arduino environment. This example code is in the
public domain.
Update 24th July 2011
This version will echo any serial command sent from Processing directly via Arduino to the IRCF360
________________________________
/*
Language: Arduino
This Arduino UNO sketch interfaces the IRCF360 - 360 degree sensor from
www.robotmaker.eu
It uses a softserial port Serial Call and Response developed by Tom Igoe
Processing sketch will need to send a corresponding ASCII character
to it startup. Once started the IRCF360 will either continue on it's
own or stop after each command has been completed. This is
programmable feature.
The circuit:
* Plug +5v and +0v to the IRCF360 power and GND pins respectively
* Connect IRCF360 RX pin to soft TX pin on Arduino
* Connect IRCF360 TX pin to soft RX pin on Arduino
Created: 24 July. 2011
by: Colin Bacon
Modified:
by:
This example code is in the public domain.
http://www.robotmaker.eu/products-2/ircf360/sensorium-project/arduino-interface
*/
#include <NewSoftSerial.h>
NewSoftSerial mySerial(2, 3);
int StartByteReceived = false;
int serialCommand[5]; //The IRCF360 expects 4 bytes of data (0-3)|| for each command and 1 byte for the acknowledgement received from the IRCF360
int serialCount=0;
byte inByte =0;
void setup()
{
//if serialCommmand = 3
// set the data rate for the Arduino RS232 port (also USB port)
Serial.begin(9600);
Serial.println("IRCF360 Interface for processing.org");
mySerial.begin(9600);
// Turn off serial communication echo
mySerial.print(10,BYTE);
mySerial.print(0,BYTE);
mySerial.print(0,BYTE);
mySerial.print(0,BYTE);
// Turn on serial communication startbit to facilitate handshaking
delay (100);
mySerial.print(18, BYTE);
mySerial.print(10,BYTE);
mySerial.print(0,BYTE);
mySerial.print(255,BYTE);
//Run the 360 degree sensing command as default
delay (100);
mySerial.print(30, BYTE);
mySerial.print(10,BYTE);
mySerial.print(0,BYTE);
mySerial.print(255,BYTE);
serialCount=0;
Serial.flush();
}
void loop() // run over and over again
{
//---serial communication with Processing ---------
if (Serial.available() > 0 ) //Check if a byte has arrived on USB port from processing Sketch. If not then keep looping
{
// Read the serial messages comming in from Processing
inByte = Serial.read();
serialCommand[serialCount]=inByte; //Store the bytes into an array
serialCount++; //Keep count of the array postiion
if (serialCount>3 ) //Note that softserial does strange things if you use = rather than > here
{
mySerial.print(serialCommand[0] ,BYTE ); //The commmand
mySerial.print(serialCommand[1] ,BYTE); // The value related to command
mySerial.print(serialCommand[2],BYTE ); // Output Configuration Byte (e.g. if ASCII or DEC output)
mySerial.print(serialCommand[3],BYTE ); // continuous or single shot
serialCount=0; //reset counter
Serial.flush(); // flush any rubbish
}
}
//Echo whatever comes in from IRCF360 to the USB port/Processing
if (mySerial.available())
{
Serial.write( mySerial.read() );
}
}
/* Corresponding Processing Code
void setup() {
// Print a list of the serial ports, for debugging purposes:
println(Serial.list());
size(800, 800, P3D);
pg = createGraphics(80, 80, P3D);
// I know that the first port in the serial list on my mac
// is always my FTDI adaptor, so I open Serial.list()[0].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you're using.
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
}
//-------------------------------------------
void serialEvent(Serial myPort) {
// read a byte from the serial port:
int inByte = myPort.read();
println ("inByte " + inByte);
if (inByte==255) startByte=true;
if (startByte==true) {
if (inByte !=255) {
serialInArray[serialCount] = inByte ;
println ("inByte " + serialCount + " " + inByte);
serialCount++; //if its not the startbit then store this value
// If we have 8 bytes:
if (serialCount > 7) {
N = serialInArray[0];
NE = serialInArray[1];
E = serialInArray[2];
SE = serialInArray[3];
S = serialInArray[4];
SW = serialInArray[5];
W = serialInArray[6];
NW = serialInArray[7];
// print the values (for debugging purposes only):
//println(N + "\t" + NE + "\t" + E + "\t" + SE + "\t" + S + "\t" + SW + "\t" + W + "\t" + NW);
// Reset serialCount:
serialCount = 0;
myPort.clear(); // clear the serial port buffer
startByte=false; //reset startbyte flag
ignore=false;
//}
}
}
}
}
//-------------------------------------------------------------
void mousePressed()
{
//if(circleOver) {
//myPort.write(200); //start the transmission)
//delay (100);
myPort.write(30); //
//delay (100);
myPort.write(10); //
//delay (100);
myPort.write(1); //
//delay (100);
myPort.write(255); // |
//delay (100);
myPort.clear();
serialCount=0;
// }
}
*/