Hello, I’m trying to make some videos play with Tof sensor connected to arduino.
with the value of distance from ToF sensor, videos will play.
I tried to connect arduino with OF watcing Dan Buzzo’s tutorial
In arduino IDE I can see the values change and work fine.
however in OF, the value wont change
sometimes it does work(if I change the baud rate to 115200, the original example code from polou ToF sensor uses 115200 baud rate)
but the values aren’t that stable compared to values in serial monitor in arduino IDE
what am I missing and is there more simple way to connect arduino and OF?
and what files are needed in the same project file? because I’m using arduino_test folder made by Dan Buzzo and I see some MAKE files, are these needed ??
code of arduino is
#include <Boards.h>
#include <Firmata.h>
#include <FirmataConstants.h>
#include <FirmataDefines.h>
#include <FirmataMarshaller.h>
#include <FirmataParser.h>
/*
Ultra-simple sketch to demonstrate the use of Arduino with
openFrameworks.
This sketch is written to be used with the serialExample program
that comes with the openFrameworks library. It initializes a serial
port with a speed of 9600 baud, then waits for serialExample to send
a character ('a' by default), then responds with three dollar signs.
To use this sketch with oF, program your Arduino, make sure the
Arduino is the only vitual COM device in use, and then build and run
the serialExample example
in openFrameworks.
Released under the Creative Commons Share-Alike License 10/2011
*/
#include <Wire.h>
#include <VL53L1X.h>
VL53L1X sensor;
int ledPin = 13;
int greenLedPin = 12;
int redLedPin = 11;
int sensorPin = 0;
int sensorValue = 0;
void setup(){
Serial.begin(9600);
Wire.begin();
Wire.setClock(400000);
sensor.setTimeout(500);
if (!sensor.init())
{
Serial.println("Failed to detect and initialize sensor!");
while (1);
}
sensor.setDistanceMode(VL53L1X::Long);
sensor.setMeasurementTimingBudget(50000);
sensor.startContinuous(50);
}
void loop(){
sensor.read();
char inByte = 0;
// if we get a valid byte, read analog ins:
if (Serial.available() > 0) {
// get incoming byte:
inByte = Serial.read();
}
sensorValue = sensor.ranging_data.range_mm;
Serial.write(sensorValue);
//Serial.print(sensorValue);
//Serial.println();
}
and the header file of OF is
#pragma once
#include "ofMain.h"
class ofApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void keyPressed(int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void mouseEntered(int x, int y);
void mouseExited(int x, int y);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
ofTrueTypeFont font;
ofSerial serial;
string sensorValue;
int byteData;
string msg;
};
and last the cpp is
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup(){
ofSetVerticalSync(true);
ofBackground(255);
ofSetLogLevel(OF_LOG_VERBOSE);
font.load("monospace", 24);
serial.listDevices();
vector <ofSerialDeviceInfo> deviceList = serial.getDeviceList();
// this should be set to whatever com port your serial device is connected to.
// (ie, COM4 on a pc, /dev/tty.... on linux, /dev/tty... on a mac)
// arduino users check in arduino app....
int baud = 9600;
//serial.setup(0, baud); //open the first device
//serial.setup("COM4", baud); // windows example
serial.setup("COM4", baud); // mac osx example
//serial.setup("/dev/ttyUSB0", baud); //linux example
}
//--------------------------------------------------------------
void ofApp::update(){
//Simple if statement to inform user if Arduino is sending serial messages.
if (serial.available() < 0) {
sensorValue = "Arduino Error";
}
else {
//While statement looping through serial messages when serial is being provided.
while (serial.available() > 0) {
//byte data is being writen into byteData as int.
byteData = serial.readByte();
//byteData is converted into a string for drawing later.
sensorValue = "value: " + ofToString(byteData);
}
}
cout << sensorValue << endl; // output the sensorValue
}
//--------------------------------------------------------------
void ofApp::draw(){
ofSetColor(0);
msg = "press key a, b or c to test serial:\n";
font.drawString(msg, 50, 60);
font.drawString("sensorValue: " + sensorValue, 50, 100);
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
switch (key) {
case 'a':
serial.writeByte('a');
cout << "flash green LED" << endl;;
break;
case 'b':
serial.writeByte('b');
cout << "flash red LED" << endl;
break;
case 'c':
serial.writeByte('c');
cout << "flash white LED" << endl;
break;
default:
break;
}
}