@loricolbert wrote:
I’m trying to replicate this project:
Ultrasonic Sensor in OpenFrameworks Using Arduino : 4 Steps - InstructablesI’ve got the arduino wired and I’m pretty sure its code is running properly.
I’ve got openFrameworks running inside Visual Studio 2017 and I’ve got the following code built/compiled, but when I run it, instead of getting a running output of "cm: " followed by a reading from the ultrasonic sensor, I get these two errors instead:
[ error ] ofSerial: available(): serial not inited
[ error ] ofTrueTypeFont: drawString(): font not allocatedHere’s the code I have in oF…I’m a total beginner, so I’m just trying to copy the code from the Instructables, but something isn’t working and I don’t really understand how the code works at all, so I appreciate any help you can give.
main.cpp
#include “ofMain.h”
#include “ofApp.h”//========================================================================
int main( ){
ofSetupOpenGL(500,200,OF_WINDOW); // <-------- setup the GL context// this kicks off the running of my app // can be OF_WINDOW or OF_FULLSCREEN // pass in width and height too: ofRunApp(new ofApp());}
ofApp.h
#pragma once
#include “ofMain.h”
class ofApp : public ofBaseApp {
public:
//Standard oF functions.
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); //Custom variables for on screen string and font. string msg; ofTrueTypeFont font; //New serial object. ofSerial serial;};
ofApp.cpp
#include “ofApp.h”
//int for storing the byte data from Arduino.
int byteData;
//--------------------------------------------------------------
void ofApp::setup() {
//General setup of look of window.
ofBackground(255);
font.load(“verdana.ttf”, 64);
ofSetColor(0);//serial port setup. using COM3 for Windows port. //Also using baud rate 9600, same in Arduino sketch. serial.setup("COM3", 9600); font.drawString("test1", 50, 100);}
//--------------------------------------------------------------
void ofApp::update() {//Simple if statement to inform user if Arduino is sending serial messages. if (serial.available() < 0) { msg = "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. msg = "cm: " + ofToString(byteData); } }}
//--------------------------------------------------------------
void ofApp::draw() {
//drawing the string version pf byteData on oF window.
font.drawString(msg, 50, 100);//printing byteData into console. cout << byteData << endl;}
//--------------------------------------------------------------
void ofApp::keyPressed(int key) {}
//--------------------------------------------------------------
void ofApp::keyReleased(int key) {}
//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y) {}
//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button) {}
//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button) {}
//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button) {}
//--------------------------------------------------------------
void ofApp::mouseEntered(int x, int y) {}
//--------------------------------------------------------------
void ofApp::mouseExited(int x, int y) {}
//--------------------------------------------------------------
void ofApp::windowResized(int w, int h) {}
//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg) {}
//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo) {}
Posts: 2
Participants: 1