Lab3 Radar
Basic example to visualize data from an ultrasonic distance sensor.
Arduino Code:
/* lab3 radar * * based on the example "Read ultrasonic distance Sensor" and "Stepper motor controller" * * uses SRF02 library from grapelabs, http://www.grapelabs.de * uses metro library, http://www.arduino.cc/playground/Code/Metro * * remove red jumper near built-in poti to use I2C bus * connect plug with three wires on A5 and plug with one wire on A4 * * SensorAktor Workshop * Lab3 2008 * Kunsthochschule fuer Medien Koeln * Academy of Media Arts Cologne * http://interface.khm.de * pin mapping SensorAktor Shield * import SensorAktor.h to get already defined mapping * * analog inputs: analog0 = pin0, analog1 = pin1, analog2 = pin2, * analog3 = pin3, analog4 = pin4, analog5 = pin5 * built-in poti: poti = pin5 * amplified input: mic = pin0 * switches: switch1 = pin1, switch2 = pin2, switch3 = pin3 * leds: led1 = pin3, led2 = pin5, led3 = pin6 * power outs: out1 = pin3, out2 = pin5, out3 = pin6 * motor outs: motor1_1 = pin8, motor1_2 = pin13, motor2_1 = pin11, motor2_2 = pin12 * motor pwms: pwmMotor1 = pin10, pwmMotor2 = pin9 * servo outs: servo1 = pin2, servo2 = pin4, servo3 = pin7 */ // import sensor aktor library for pin mapping #include <SensorAktor.h> // import wire library to use I2C bus #include "Wire.h" // import SRF02 library #include "SRF02.h" // ultrasonic distance sensor SRF02 distanceSensor(0x70, SRF02_CENTIMETERS); // import stepper library #include <StepperL293.h> // stepper StepperL293 myStepper; // total steps int totalSteps = 0; int directionSteps = 1; // import metro library (for serial output) #include // radar metro Metro radarMetro = Metro(100); void setup() { // start serial communication (for printing to serial port) Serial.begin(9600); // start I2C communication Wire.begin(); // set interval for ultrasonic distance sensor SRF02::setInterval(100); // attach pins to stepper (motor pin1, motor pin2, motor pin3, motor pin4, number of steps of motor) myStepper.attach(motor1_1, motor1_2, motor2_1, motor2_2, 48); // set mode (0 = half step moede, 1 = full step mode) myStepper.setMode(0); // set power (value between 0 and 100) myStepper.setPower(100); // set speed (values between 1 and 100) myStepper.setSpeed(10); } void loop() { // update sensor data SRF02::update(); // check radar metro if(radarMetro.check() == 1){ // check direction steps (0=left, 1=right) if(directionSteps == 1){ // counter steps totalSteps++; // 95 steps is one full rotation in half step mode if(totalSteps >= 95){ // turn left directionSteps = 0; } } else{ // counter steps totalSteps--; if(totalSteps <= 0){ // turn right directionSteps = 1; } } // set new step myStepper.setStep(totalSteps); /* build message for serial port (to receive data in processing) format to send = recectStep, space, sensorValue, space, linebreak */ Serial.print( totalSteps); Serial.print(32,BYTE); Serial.print(distanceSensor.read()); //Serial.print(analogRead(a0)); Serial.print(32,BYTE); Serial.println(); } // drive stepper myStepper.drive(); }
Processing Code:
/* lab3 radar * * visualize data from ultrasonic distance sensor * */ // import serial library (need to read date from arduino board) import processing.serial.*; // serial port Serial port; // Serial variables String serialString; String rxString = null; // Anwort vom Ser Port int cr = 13; // carriage return int lf = 10; // line feed // distance int distance; // current angle int currentAngle; // this block is executed one time when programm starts void setup(){ // size of window size(800,600); // print a list of the serial ports, to get port of arduino println("Serial ports"); println(Serial.list()); // init serial port, on my computer the arduino port is the first port port = new Serial(this, Serial.list()[0], 9600); frameRate(24); background(0); } // this block is executed in a loop after setup is called one time void draw(){ // clear background fill(0,1); rect(0,0,width,height); // translate translate(width/2, height/2); // use current rotate(radians((currentAngle/96.0)*360.0)); // draw line stroke(255); line(0,0,distance/1.5,0); } // serial event void serialEvent(Serial p){ // get string till line break (ASCII > 13) serialString = port.readStringUntil(13); // just if there is data if (serialString != null) { // try catch function because of possible garbage in received data try { String[] test = splitTokens(serialString); // get current angle currentAngle = Integer.parseInt(test[0]); // get distance distance = Integer.parseInt(test[1]); } catch (Exception e){ } } }
Files needed
Since Arduino 0015 there are some problems with the old “SensorAktor.h” file.
Use this one instead.