openFrameworks + Ultrasonic Sensor to Draw Shapes (Week 6)_Alyssa

 Goal: Use a sensor to control the screen display.

With this assignment, I wanted to test the connection between Arduino and openFrameworks for the first time. This mini project was just to get started with interactivity and drawing to the screen using a sensor. I selected the ultrasonic sensor to determine the radius of each circle drawing to the screen.

Components: Breadboard, ultrasonic sensor (x1), jumper cables, Arduino, openFrameworks

I used this tutorial to get set up:

http://www.instructables.com/id/Ultrasonic-sensor-in-openFrameworks-using-Arduino/

I think tweaked the oF code to draw circles instead of write text to the screen.

As the subject moves closer to the sensor, the the radius of each circle shrinks and vice versa.

This was a useful proof of concept. Next, I’d use a better sensor, or refine the ping field for this one so it’s not as buggy as you see here in terms of the flickering screen.

Code:

Arduino:

//Cormac Joyce 2015
//initialising library.
#include
// Arduino pin assigned to trigger pin on the ultrasonic sensor.
#define TRIGGER_PIN  12  
// Arduino pin assigned to echo pin on the ultrasonic sensor.
#define ECHO_PIN     11  
// Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
#define MAX_DISTANCE 500
//int for reading the distance in cm into.
int dist;
// NewPing setup of pins and maximum distance.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
void setup() {
  Serial.begin(9600); // Open serial monitor at 115200 baud to see ping results.
}
void loop() {
  
  // ( setting ping distance in cm to dist variable (0 = outside set distance range).
  dist = sonar.ping_cm();
  // Wait 50ms between pings (about 20 pings/sec).
  delay(50);
  //here the dist variable is being written in bytes so that is can be read in oF as an int.                       
  Serial.write(dist);
}

openFrameworks:

main.cpp

#include "ofMain.h"
#include "ofApp.h"
//========================================================================
int main( ){
    //Using small window to just show serial message.
    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.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.loadFont(“Borgen.ttf”, 64);

    ofSetColor(0);

    //serial port setup. using COM3 for Windows port.

    //Also using baud rate 9600, same in Arduino sketch.

    serial.setup(“/dev/cu.usbmodemFA141”, 9600);

}

//————————————————————–

void ofApp::update(){

     ofBackground(0);

    //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();

        }    

    }

    }

//————————————————————–

void ofApp::draw(){

    ofSetColor(255, 0, 255);

    std::string msg  = ofToString(byteData);

    float dist = std::stoi(msg);

    ofDrawCircle(300, 300, dist);

     ofSetColor(255, 255, 0);

    ofDrawCircle(100, 500, dist*2);

     ofSetColor(25, 60, 255);

    ofDrawCircle(900, 800, dist*3);

}

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;
        
};

Leave a Reply

Your email address will not be published. Required fields are marked *