Salve ragazzi, ho sistemato il problema non era lo sketch ma nell'utilizzo di una resistenza sbagliata.
Il problema ora resta questo.
Io vorei tramite iPhone e supporto di TouchOCS accendere e spegnere il led e regolarne l'intensità.
Ci sarei anche riuscito se non fosse per il fatto che sono costretto a tenere premuto i vari tasti sul display del mio iPhone per farlo funzionare.
Ora io ho progettato il tutto in questo modo.
//Arduino
int message = 0; // This will hold one byte of the serial message
int redLEDPin = 11; // What pin is the red LED connected to?
int redLED = 0; // The value/brightness of the LED, can be 0-255
float sinVal;
int ledVal;
void setup() {
Serial.begin(9600); //set serial to 9600 baud rate
}
void loop(){
if (Serial.available() > 0) { // Check if there is a new message
message = Serial.read(); // Put the serial input into the message
if (message == 'R'){
// If a capitol R is received...
analogWrite(redLEDPin, 255);
}else if (message == 'r'){ // If a lowercase r is received...
analogWrite(redLEDPin, 0);
}else if(message!='0'){
int x = int(message);
sinVal = (sin(x*(3.1412/180)));
ledVal = int(sinVal*255);
analogWrite(redLEDPin, x);
}
}
// Write an analog value between 0-255
}
//Processing
import oscP5.*; // Load OSC P5 library
import netP5.*; // Load net P5 library
import processing.serial.*; // Load serial library
Serial arduinoPort; // Set arduinoPort as serial connection
OscP5 oscP5; // Set oscP5 as OSC connection
int intensita=0;
int redLED; // redLED lets us know if the LED is on or off
int [] led = new int [2]; // Array allows us to add more toggle buttons in TouchOSC
void setup() {
size(100,100); // Processing screen size
noStroke(); // We don’t want an outline or Stroke on our graphics
oscP5 = new OscP5(this,8000); // Start oscP5, listening for incoming messages at port 8000
arduinoPort = new Serial(this, Serial.list()[1], 9600); // Set arduino to 9600 baud
}
void oscEvent(OscMessage theOscMessage) { // This runs whenever there is a new OSC message
String addr = theOscMessage.addrPattern(); // Creates a string out of the OSC message
int val = (int)theOscMessage.get(0).floatValue();
//System.out.println(
if(addr.indexOf("/1/toggle1") !=-1){ // Filters out any toggle buttons
System.out.println("bottone verde premuto");
int i = int((addr.charAt(9) )) - 0x30; // returns the ASCII number so convert into a real number by subtracting 0x30
redLED=int(theOscMessage.get(0).floatValue());
// led[i] = int(theOscMessage.get(0).floatValue()); // Puts button value into led[i]
// Button values can be read by using led[0], led[1], led[2], etc.
}else if(addr.indexOf("/1/fader3") !=-1){
System.out.println("slider premuto");
val = (int)theOscMessage.get(0).floatValue();
intensita=val;
}
}
void draw() {
background(50); // Sets the background to a dark grey, can be 0-255
if(intensita >0 && intensita <= 255){
System.out.println("regolo intensita");
arduinoPort.write(intensita);
}
else if(redLED == 0){ // If led button 1 if off do....
// arduinoPort.write("r"); // Sends the character “r” to Arduino
System.out.println("off "+redLED);
OscMessage myMessage = new OscMessage("/1/toggle1");
myMessage.add((int)s1.value());
oscP5.send(myMessage, Ipod);
// redLED = 0; // Sets redLED color to 0, can be 0-255
}
else if(redLED == 1){ // If led button 1 is ON do...
arduinoPort.write("R"); // Send the character “R” to Arduino
System.out.println("on "+redLED);
}
//fill(redLED,0,0); // Fill rectangle with redLED amount
// ellipse(50, 50, 50, 50); // Created an ellipse at 50 pixels from the left...
// 50 pixels from the top and a width of 50 and height of 50 pixels
}
Ora la schermata TouchOCS l'ho organizzato in questo modo ho inserito un pulsante che vorrei fare tipo per On/Off per il led e l'altro invece è un "fader" con valore che va da 0 a 255 che ho usato per far aumentare e diminuire l'intensitò del led.
Funziona tutto, se non fosse per il fatto che per far accendere la luce devo avere sempre il dito sul pulsante.
Dove sbaglio???