How to use WiringPi in oF for PWM

Hi friends! I am trying to run the following demo program on my Raspberry Pi with OpenFrameworks and Wiring Pi: https://github.com/openframeworks/ofSite/blob/master/setup/raspberrypi/Raspberry-Pi-Using-the-GPIO-pins-with-Wiring-Pi-and-openFrameworks.html.mako

I am getting the following error when compiling:

obj/linuxarmv6l/Release/src/main.o:(.bss+0x10): multiple definition of `sensor7'
obj/linuxarmv6l/Release/src/testApp.o:(.bss+0x10): first defined here
obj/linuxarmv6l/Release/src/main.o:(.bss+0x11): multiple definition of `sensor3'
obj/linuxarmv6l/Release/src/testApp.o:(.bss+0x11): first defined here
obj/linuxarmv6l/Release/src/main.o:(.bss+0x12): multiple definition of `sensor0'
obj/linuxarmv6l/Release/src/testApp.o:(.bss+0x12): first defined here
collect2: ld returned 1 exit status
make[1]: *** [bin/GPIOTest] Error 1
make: *** [Release] Error 2

How do I fix this error?
Also, my end goal is to have two PWM outputs from the Pi’s GPIO pins to control 2 motors. Any ideas on the easiest way to do this with or without WiringPi?

this is a c++ error that means you have declared a variable with the same name multiple times. If you are using the code directly from that page change

bool sensor0 = false;
bool sensor3 = false;
bool sensor7 = false;

to

sensor0 = false;
sensor3 = false;
sensor7 = false;

I tried that and I still get the exact same error.

Can you post your code as-is?

1 Like

testApp.h:

#pragma once

#include "ofMain.h"
#include "wiringPi.h"
#include "pthread.h"

bool sensor0;
bool sensor3;
bool sensor7;

class testApp : 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 windowResized(int w, int h);
		void dragEvent(ofDragInfo dragInfo);
		void gotMessage(ofMessage msg);
};

testApp.cpp:

#include "testApp.h"

//--------------------------------------------------------------
void testApp::setup(){
if(wiringPiSetup() == -1){
	printf("Error on wiringPi setup\n");}
	pinMode(0,INPUT);
	pinMode(3,INPUT);
	pinMode(7,INPUT);
sensor0 = false;
sensor3 = false;
sensor7 = false;
}

//--------------------------------------------------------------
void testApp::update(){
if(digitalRead(0) != 0){
	sensor0=true;
} else {
	sensor0=false;}
if(digitalRead(3)!=0){
	sensor3=true;
} else {
	sensor3=false;}
if(digitalRead(7)!=0){
	sensor7=true;
} else {
	sensor7=false; }
}

//--------------------------------------------------------------
void testApp::draw(){
ofBackground(ofColor(0,0,0));
ofSetColor(190);
if(sensor0 == true){
	ofDrawBitmapString("Sensor at pin 0 activated",50,50);
}
if(sensor3 == true){
	ofDrawBitmapString("Sensor at pin 3 activated", 50,70);
}
if(sensor7 == true){
	ofDrawBitmapString("Sensor at pin 7 activated",50,90);
}
}
1 Like

Try changing

bool sensor0;
bool sensor3;
bool sensor7;

class testApp : 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 windowResized(int w, int h);
		void dragEvent(ofDragInfo dragInfo);
		void gotMessage(ofMessage msg);
};

to

class testApp : 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 windowResized(int w, int h);
		void dragEvent(ofDragInfo dragInfo);
		void gotMessage(ofMessage msg);

bool sensor0;
bool sensor3;
bool sensor7;
};