Etape 3: Traitement IDE Source Code
Après que nous avons téléchargé le sketch Arduino précédent, que nous devons recevoir les données dans l’IDE de traitement et de dessiner la boussole numérique. La boussole est composée d’une image de fond, une image fixe de la flèche et une image de rotation du corps de la boussole. Si les valeurs pour le champ magnétique eart calculé avec l’Arduino sont utilisés pour faire pivoter la boussole.
Voici le code source de l’IDE de traitement :
<p>/* Arduino Compass <br> * * by Dejan Nedelkovski, * <a href="http://www.HowToMechatronics.com"> www.HowToMechatronics.com </a> * */ import processing.serial.*; import java.awt.event.KeyEvent; import java.io.IOException;</p><p>Serial myPort; PImage imgCompass; PImage imgCompassArrow; PImage background;</p><p>String data=""; float heading;</p><p>void setup() { size (1920, 1080, P3D); smooth(); imgCompass = loadImage("Compass.png"); imgCompassArrow = loadImage("CompassArrow.png"); background = loadImage("Background.png"); myPort = new Serial(this, "COM4", 115200); // starts the serial communication myPort.bufferUntil('\n'); }</p><p>void draw() { image(background,0, 0); // Loads the Background image pushMatrix(); translate(width/2, height/2, 0); // Translates the coordinate system into the center of the screen, so that the rotation happen right in the center rotateZ(radians(-heading)); // Rotates the Compass around Z - Axis image(imgCompass, -960, -540); // Loads the Compass image and as the coordinate system is relocated we need need to set the image at -960x, -540y (half the screen size) popMatrix(); // Brings coordinate system is back to the original position 0,0,0 image(imgCompassArrow,0, 0); // Loads the CompassArrow image which is not affected by the rotateZ() function because of the popMatrix() function textSize(30); text("Heading: " + heading,40,40); // Prints the value of the heading on the screen</p><p> delay(40); }</p><p>// starts reading data from the Serial Port void serialEvent (Serial myPort) { data = myPort.readStringUntil('\n');// reads the data from the Serial Port and puts it into the String variable "data". heading = float(data); // Convering the the String value into Float value }</p>
J’espère que vous aimerez ce projet. Si c’est le cas, vous pouvez aussi visiter mon site Web pour des projets plus cool.