Hello, I am working on a project with 3 timers where at 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 is ON
I am using ofSerial for connecting another sensor with oF, and working perfectly.
My ledPin is lighting up gradually ignoring my code. May I ask if it is my arduino or oF code that is not working please? Or should I not use ofSerial but firmata in the first place?
Arduino code:
#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:
#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){
for (int i = 0; i< 255; i++){
serial.writeByte(i);
cout << i << 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();
}