Skip to content

Instantly share code, notes, and snippets.

@phoenixperry
Created March 28, 2015 07:58
Show Gist options
  • Save phoenixperry/0e09d0776882f953abe6 to your computer and use it in GitHub Desktop.
Save phoenixperry/0e09d0776882f953abe6 to your computer and use it in GitHub Desktop.
//This code works.
ofSerial serial;
int sensorPadValue;
void ofApp::setup(){
//this is the strange line. If I don't explicity call the ofSerial construtor all hell breaks loose. The error is Thread 1: EXC_BAD_ACCESS//(code=1, address 0xffffff4).
serial = new ofSerial();
serial->listDevices();
serial->setup(0, 9600);
}
//--------------------------------------------------------------
void ofApp::update(){
//This gets sent to arduino, calling for a new set of readings
serial->writeByte('a');
if(serial->available()){
unsigned char bytesReturned[6]; //000 new line + null terminator
// memset(bytesReadString, 0, 3);
memset(bytesReturned, 0, sizeof(bytesReturned));
// This reads the data now that arduino is sending a response,
serial->readBytes(bytesReturned, 5);
//check the data to make sure the new line isn't doing weird stuff
string serialData = (char*) bytesReturned;
sensorPadValue = ofToInt(serialData);
cout << sensorPadValue << endl;
//This was allows for the whole process to repeat without
// getting strange overlapping readings from the encoder:
serial->flush();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment