Pages: 1 2 [3]   Go Down
Author Topic: Aiuto per lettura valori RGB  (Read 2432 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 78
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ciao ragazzi come promesso, copio unpò di codice ed allego lo zip.

Premetto che il codice può essere notevolmente migliorato quindi magari se avete migliorie postatele....

CODICE ARDUINO
Code:
int message = 0;     //  This will hold one byte of the serial message
int redLEDPin = 11;   //  What pin is the red LED connected to?
int greenLEDPin = 10;
int blueLEDPin = 9;
int numeroPin=0;
int cont=0;
long vet[9] = {-2, -2, -2, -2};
int intensita=0;
void setup() { 
  Serial.begin(9600);  //set serial to 9600 baud rate

}

void loop(){
 
    if (Serial.available() > 0) { //  Check if there is a new message
      cont=0;
      intensita=100;
      Serial.println("prova di led");
     while(Serial.available()>0){
        //ho riempito il vettore
        message = Serial.read();
        //Serial.println(message);
        if(cont==0){
          vet[cont] = message;
        }else{
          vet[cont] = message-48;
        }
        cont++;
        delay(60);
      }
    if(cont>0){
      //so che il primo valore è una lettera che mi dice
      //quale led accendere
        message=vet[0];
        Serial.print("Lettera ");
        Serial.println(message);
        //ora bisogna costruire il valore.....
       
        if(vet[2] ==-2 && vet[3] == -2){
          //dopo la lettera esiste solo un numero
          intensita = vet[1];
        }else if (vet[3] == -2){
          //ho un numero da 10 a 99
          intensita = vet[1] * 10 + vet[2];
        }else{
          intensita = vet[1] * 100 + vet[2]* 10+ vet[3];
        }
        Serial.print("intensita ");
        Serial.println(intensita);
      }
     
     // message = Serial.read();    //  Put the serial input into the message

   if (message == 'R'){  //  If a capitol R is received...
     Serial.println("rosso");
     analogWrite(redLEDPin, intensita);
   else if (message == 'G'){  //  If a lowercase r is received...
   Serial.println("verde");
     analogWrite(greenLEDPin, intensita);
   }else if (message == 'B'){  //  If a lowercase r is received...
   Serial.println("blue");
     analogWrite(blueLEDPin, intensita);
   }
   
   }   
}


CODICE PROCESSING

Code:
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 led = 0;        //  redLED lets us know if the LED is on or off
int redLed=0;
int greenLed=0;
int blueLed=0;
int intensitaRed=0;
int intensitaBlue=0;
int intensitaGreen=0;
int [] ledRed = new int [2];    //  Array allows us to add more toggle buttons in TouchOSC
int [] ledGreen = new int [2];
int [] ledBlue = new int [2];

int [] intensitaLedRed = new int [2];
int [] intensitaLedBlu = new int [2];
int [] intensitaLedGreen = new int [2];
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
   System.out.println("prova");
}

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
    System.out.println(theOscMessage.get(0).floatValue());
    if(addr.indexOf("/1/toggle1") !=-1){   // Filters out any toggle buttons
      int i = int((addr.charAt(9) )) - 0x30;   // returns the ASCII number so convert into a real number by subtracting 0x30
   System.out.println("evento osc");
      ledRed[1]  = int(theOscMessage.get(0).floatValue());     //  Puts button value into led[i]
      redLed=1;
      greenLed=0;
      blueLed=0;
    }else if(addr.indexOf("/1/toggle2") !=-1){   // Filters out any toggle buttons
      int i = int((addr.charAt(9) )) - 0x30;   // returns the ASCII number so convert into a real number by subtracting 0x30
   //   System.out.println(theOscMessage.get(0).floatValue());
      ledGreen[1]  = int(theOscMessage.get(0).floatValue());     //  Puts button value into led[i]
      redLed=0;
      greenLed=1;
      blueLed=0;
    }else if(addr.indexOf("/1/toggle3") !=-1){   // Filters out any toggle buttons
      int i = int((addr.charAt(9) )) - 0x30;   // returns the ASCII number so convert into a real number by subtracting 0x30
   //   System.out.println(theOscMessage.get(0).floatValue());
      ledBlue[1]  = int(theOscMessage.get(0).floatValue());     //  Puts button value into led[i]
       redLed=0;
      greenLed=0;
      blueLed=1;
    }else if(addr.indexOf("/1/fader2") !=-1){   // Filters out any toggle buttons
       int val = (int)theOscMessage.get(0).floatValue();
       intensitaLedRed[1]=val;
       intensitaRed=1;
       System.out.println("Valore slider rosso: "+val);
    }else if(addr.indexOf("/1/fader3") !=-1){   // Filters out any toggle buttons
       int val = (int)theOscMessage.get(0).floatValue();
       intensitaBlue=1;
       intensitaLedBlu[1]=val;
       System.out.println("Valore slider blue: "+val);
    }else if(addr.indexOf("/1/fader4") !=-1){   // Filters out any toggle buttons
       int val = (int)theOscMessage.get(0).floatValue();
       intensitaLedGreen[1]=val;
       intensitaGreen=1;
       System.out.println("Valore slider verde: "+val);
    }


}

void draw() {
 
//Leggiamo  i valori della porta analog 0 di Arduino


 background(50);        // Sets the background to a dark grey, can be 0-255
   if(ledRed[1] == 0 && redLed==1){        //  If led button 1 if off do....
     System.out.println("spengo il led");
     arduinoPort.write("R0");    // Sends the character “r” to Arduino
     System.out.println(arduinoPort.read());
     delay(300);
     led = 0; 
     redLed=0;     // Sets redLED color to 0, can be 0-255
  }else if(ledRed[1] == 1 && redLed==1){        // If led button 1 is ON do...
    arduinoPort.write("R255");    // Send the character “R” to Arduino
    System.out.println("accendo il led");
     delay(300);
    led = 255;        // Sets redLED color to 255, can be 0-255
     redLed=0;     // Sets redLED color to 0, can be 0-255
  }else if(ledGreen[1] == 0 && greenLed==1){        //  If led button 1 if off do....
    arduinoPort.write("G0");    // Sends the character “r” to Arduino
    delay(300);
    led = 0;        // Sets redLED color to 0, can be 0-255
    greenLed=0;
  }else if(ledGreen[1] == 1 && greenLed==1){        // If led button 1 is ON do...
    arduinoPort.write("G255");    // Send the character “R” to Arduino
    delay(300);
    led = 255;        // Sets redLED color to 255, can be 0-255
    greenLed=0;
  }else if(ledBlue[1] == 0 && blueLed==1){        //  If led button 1 if off do....
    arduinoPort.write("B0");    // Sends the character “r” to Arduino
    delay(300);
    led = 0;        // Sets redLED color to 0, can be 0-255
    blueLed=0;
  }else if(ledBlue[1] == 1 && blueLed==1){        // If led button 1 is ON do...
    arduinoPort.write("B255");    // Send the character “R” to Arduino
    delay(300);
    led = 255;        // Sets redLED color to 255, can be 0-255
    blueLed=0;
  }else if(intensitaLedRed[1] != 0 && intensitaRed==1){
     System.out.println("r"+(int)intensitaLedRed[1]);
     arduinoPort.write("R"+(int)intensitaLedRed[1]);
     delay(300);
     intensitaRed=0;
  }else if(intensitaLedBlu[1] != 0 && intensitaBlue==1){
     System.out.println("b"+(int)intensitaLedBlu[1]);
     arduinoPort.write("B"+(int)intensitaLedBlu[1]);
     delay(300);
     intensitaBlue=0;
  }else if(intensitaLedGreen[1] != 0 && intensitaGreen==1){
     System.out.println("g"+(int)intensitaLedGreen[1]);
     arduinoPort.write("G"+(int)intensitaLedGreen[1]);
     delay(300);
     intensitaGreen=0;
  }

fill(led,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
}


Nel file .zip è presente anche il file touchosc se lo volete caricare sul vostro smartphone (io ho usato il mio iPhone 4 e iPad).

* iphone_lerd_rgb.zip (4.21 KB - downloaded 5 times.)
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 78
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ora ragazzi il prossimo passo per me sarà, fare la stessa cosa utilizzando però l'ethernet shield, successivamente sempre tramite ethernet shield regolare l'intensità di una lampada da 220V.

Se avete suggerimenti o link da suggerire io sono in ascolto.

Colgo l'occasione per ringraziare tutti gli utenti che hanno partecipato a questa discussione aiutandomi a risolvere il mio problema.
Logged

Pages: 1 2 [3]   Go Up
Jump to: