arduino + openFrameworks + ledPin as output


hello, working on project 3 timers @ where:
1/ 1st timer started- ledpin not light up
2/ 2nd timer started (and 1st timer finished) - ledpin fading in
3/ 3rd timer started (and 2nd timer finished) - ledpin on

i using ofserial connecting sensor of, , working perfectly.
my ledpin lighting gradually ignoring code. may ask if arduino or of code not working please? or should not use ofserial firmata in first place?

arduino code:

code: [select]

  #include <firmata.h> 
  #include <capacitivesensor.h>

byte sensorbyte;
int inputpin = 4;
capacitivesensor   sensorpin = capacitivesensor(inputpin,2); 
int ledpin = 11;
int ledvalue;


void setup() {
     sensorpin.set_cs_autocal_millis(0xffffffff);
     serial.begin(9600);
     pinmode(ledpin, output);   
}

void loop() {
    sensing();
    byte ledvalue;
   
    if(serial.available()){   
      ledvalue = serial.read();     
      analogwrite(ledpin, ledvalue);
      delay(10);
    }
}

long sensing(){
long total1 = sensorpin.capacitivesensor(10);

sensorbyte = map(total1, 0, 170, 0, 255);
sensorbyte = constrain(sensorbyte,0,255);

serial.write(sensorbyte);

delay(10);   
}

my of code:

code: [select]

#include "ofapp.h"

//--------------------------------------------------------------
void ofapp::setup()
{

ofsetverticalsync(true);

ofbackground(34);
ofsetloglevel(of_log_verbose);

timer1.setup( 5000 ) ;
timer2.setup( 3000 ) ; 
timer3.setup (9000);


timer1.start( false ) ;

font.loadfont("franklingothic.otf",20);

ofaddlistener( timer1.timer_complete , this, &ofapp::timer1completehandler ) ;
ofaddlistener( timer2.timer_complete , this, &ofapp::timer2completehandler ) ;
ofaddlistener( timer3.timer_complete , this, &ofapp::timer3completehandler );
ofaddlistener( timer1.timer_paused , this, &ofapp::timer1pausehandler ) ;
ofaddlistener( timer2.timer_paused , this, &ofapp::timer2pausehandler ) ;
ofaddlistener( timer3.timer_paused , this, &ofapp::timer3pausehandler ) ;

ofaddlistener( timer1.timer_started , this, &ofapp::timer1startedhandler ) ;
ofaddlistener( timer2.timer_started , this, &ofapp::timer2startedhandler ) ;
ofaddlistener( timer3.timer_started , this, &ofapp::timer3startedhandler ) ;

serial.listdevices();
vector<ofserialdeviceinfo> devicelist = serial.getdevicelist();

int baud = 9600;
serial.setup(0, baud);
serial.setup("/dev/tty.usbmodem1421", baud);

music1.loadsound("music1.mp3");
music2.loadsound("music2.mp3");
music1.play();

     }

//--------------------------------------------------------------
void ofapp::update(){

ofsoundupdate();

timer1.update( ) ;
timer2.update( ) ;
timer3.update();

}

//--------------------------------------------------------------
void ofapp::draw(){
   
ofsetcolor(0) ;
if (bsendserialmessage){
    (int = 0; i< 255; i++){
        serial.writebyte(i);
        cout << << endl;
    }
   
    bsendserialmessage = false;
}

if (lightup){
    serial.writebyte(255);
    cout << "lightup" << endl;
}
lightup = false;


}


void ofapp::timer1completehandler( int &args )
{

timer2.start(false);
music1.stop();
music2.setvolume(1);
music2.play();

bsendserialmessage = true;
lightup = false;

}

void ofapp::timer2completehandler( int &args )
{

timer3.start(false);
int bytein = serial.readbyte();
printf("%d\n",bytein);

if( bytein == of_serial_no_data || bytein == of_serial_error){
   
}else{
    mapbytein = ofmap(bytein,0,255,0,1);
    printf("%f\n",mapbytein);
    cout <<  "playmusic" << endl;
    music1.setvolume(min(mapbytein,1));
    music1.play();
}

music2.stop();

bsendserialmessage = false;
lightup = true;
}

  void ofapp::timer3completehandler( int &args )
{
timer1.start(false);
music1.setvolume(0.5);
music1.play();
music2.stop();
bsendserialmessage = false;
lightup = false;
}


void ofapp::timer1pausehandler( int &args ) {
cout<<"timer1 paused"<<endl;

}
void ofapp::timer2pausehandler( int &args ) {
cout<<"timer2 paused"<<endl;

}
void ofapp::timer3pausehandler( int &args ) {
cout<<"timer3 paused"<<endl;

}

void ofapp::timer1startedhandler( int &args ) {
cout<<"timer1 started"<<endl;
timer2.togglepause();
}
void ofapp::timer2startedhandler( int &args ) {
cout<<"timer2 started"<<endl;
timer1.togglepause();
}
void ofapp::timer3startedhandler( int &args ) {
cout<<"timer2 started"<<endl;
timer1.togglepause();
}

code: [select]
    (int = 0; i< 255; i++){
        serial.writebyte(i);
        cout << << endl;
    }

how long going take execute loop?

code: [select]
    if(serial.available()){   
      ledvalue = serial.read();     
      analogwrite(ledpin, ledvalue);
      delay(10);
    }

how long going take read 255 bytes other code sent?

which going take longer? going happen when serial buffer on arduino fills up?


Arduino Forum > Using Arduino > Interfacing w/ Software on the Computer > arduino + openFrameworks + ledPin as output


arduino

Comments