Skip to content

Instantly share code, notes, and snippets.

@jedahan
Last active December 29, 2015 01:29
Show Gist options
  • Select an option

  • Save jedahan/7593553 to your computer and use it in GitHub Desktop.

Select an option

Save jedahan/7593553 to your computer and use it in GitHub Desktop.
For some reason, I am only getting a signal for xPin (packet[0]) and packet[1] does not move...
#include <TimerOne.h>
const byte xPin = 9;
const byte yPin = 10;
const byte ledPin = 3;
const byte period = 32; // for 8 bit DAC
unsigned char packet[3];
void setup(void)
{
pinMode(xPin, OUTPUT);
pinMode(yPin, OUTPUT);
pinMode(ledPin, OUTPUT);
Timer1.initialize(period);
Timer1.pwm(xPin, 512);
Timer1.pwm(yPin, 512);
Serial.begin(9600);
}
void loop(void)
{
analogWrite(ledPin, packet[2]);
Timer1.setPwmDuty(xPin, 4*packet[0]);
Timer1.setPwmDuty(yPin, 4*packet[1]);
delay(1);
}
void serialEvent() {
if(Serial.available()){
unsigned int thisByte = Serial.read();
if(thisByte == 255) {
Serial.readBytes((char*)packet,3);
}
}
}
#include "testApp.h"
void testApp::setup(){
ofSetWindowShape(900, 600);
serial.setup("/dev/cu.usbmodem1431", 9600);
}
void testApp::update(){
unsigned char x = ofClamp(255 * mouseX/ofGetWidth(),0,254);
unsigned char y = ofClamp(255 * mouseY/ofGetHeight(),0,254);
unsigned char message[4] = {255,x,y,ledVal};
serial.writeBytes(message, 4);
}
void testApp::draw(){
ofSetColor(ledVal,0,0);
ofCircle(mouseX,mouseY,10);
}
void testApp::mouseMoved(int x, int y ){
mouseX = x;
mouseY = y;
}
void testApp::mouseDragged(int x, int y, int button){
mouseX = x;
mouseY = y;
}
void testApp::mousePressed(int x, int y, int button){
ledVal = 255;
}
void testApp::mouseReleased(int x, int y, int button){
ledVal = 0;
}
#pragma once
#include "ofMain.h"
class testApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void keyPressed(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);
ofSerial serial;
float mouseX, mouseY;
unsigned char ledVal;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment