Whats wrong with my code? OpenOSC, Processing and Arduino - LED brightness

edited August 2014 in Arduino

Im trying to change the brightness of a LED. The brightness is controlled by a slider on my iPhone that communicates with Processing though OpenOSC. The slider sends values between 0 and 255. My idea is that OpenOSC communicates with Processing. Processing then communicates with Arduino though serial communication.

I managed to establish the connection between the iPhone and Processing. I also managed to make an Arduino code that works when i manually type the serial input. For some reason the whole thing doesn't work. I guess the problem is the serial communication between Processing and the Arduino.

Processing code:

Arduino code:

Answers

  • Screenshots can be slow to load, hard to read, and cannot be searched nor copy / pasted. Prefer pasting code and error messages in textual mode in your message. Don't forget to format the code using the C button or Ctrl+K on the selected text.

  • edited August 2014

    Thanks - Didn't knew how to format the code:

    Arduino:

    int ledPin = 9;
    int brightness = 0;
    
    void setup() 
    { 
      pinMode(ledPin, OUTPUT); 
      digitalWrite(ledPin, LOW);
      Serial.begin(9600); 
    }
    
    void loop() 
    {
      if (Serial.available() > 0)
      {
        brightness = Serial.parseInt();
        Serial.setTimeout(50);
        Serial.println(brightness); 
      }
      analogWrite(ledPin, brightness); 
    }
    

    Processing:

    import processing.serial.*;
    import oscP5.*;
    import netP5.*;
    
    float red = 0; 
    
    OscP5 oscP5;
    Serial Arduino;
    
    void setup() {
      size(320, 480);
      background(100); 
    
      oscP5 = new OscP5(this, 8000);
      Arduino = new Serial(this, Serial.list()[2], 9600);
    }
    
    void oscEvent(OscMessage ThisOscMessage) {
      String addr = ThisOscMessage.addrPattern();
      float val = ThisOscMessage.get(0).floatValue();
    
      if (addr.equals("/1/red/")) {
        red = val;
        println(val);
        Arduino.write("val");
      }
    }
    
    void draw() {
      background(red+50, 0, 0);
      fill(0);
      rect(124, 39, 60, 255);
      fill(red, 40, 40);
      rect(124, 255+39, 60, -red);
    }
    

    My idea is that the problem is line 25 in the Processing code:

    Arduino.write("val");

    I would prefer the line to be this but then it gives an error:

    Arduino.write(val);

  • Try: Arduino.write(str(val));

Sign In or Register to comment.