Send Multiple integer to Arduino

edited March 2015 in Arduino

I just wanted to send two " integer " of two " sliders " are different in processing and received by arduino to two led , which means " SLIDER_A " for LED1 and " SLIDER_B " for LED2 . This is a processing code:

    import controlP5.*;
    import processing.serial.*;

    ControlP5 cP5a;
    ControlP5 cP5b;

    Serial arduino;

    void setup() {
      size(350,120);
      println(Serial.list());
      arduino = new Serial(this, Serial.list()[0], 9600);

      cP5a = new ControlP5(this);
      cP5a.addSlider("SLIDER_A", 0, 255, 0, 10, 10, 255, 35);
      cP5b = new ControlP5(this);
      cP5b.addSlider("SLIDER_A", 0, 255, 0, 10, 55, 255, 35);
    }

    void draw()  {
      background(0);

      }

    void controlEvent(ControlEvent theEvent) {
      if(theEvent.isController()) {

      print("control event from : "+theEvent.controller().name());
      println(", value : "+theEvent.controller().value());

     if(theEvent.controller().name()=="SLIDER_A") {
       int val1 = int(theEvent.getController().getValue());
       arduino.write("a" + val1);

     }

     if(theEvent.controller().name()=="SLIDER_B") {
       int val2 = int(theEvent.getController().getValue());
       arduino.write("b" + val2);
       }
      }
    }

and this is a processing code :

    int led1 = 6;
    int led2 = 5;

       void setup() {
         Serial.begin(9600);
         pinMode(led1, OUTPUT);
         pinMode(led2, OUTPUT);
         analogWrite(led1, LOW);
         analogWrite(led2, LOW);
         while (!Serial);
         Serial.begin(9600);
       }
       void loop() {
         if (Serial.available()) { 
         { int value1  = Serial.parseInt();
           analogWrite(led1, value1); }

           {int value2  = Serial.parseInt();
            analogWrite(led2, value2); } 
         }
       }

but the thing is, when I control one of the sliders ( " SLIDER_A " or " SLIDER_B " ) LED1 and LED2 light up simultaneously in accordance with the control exerted on the slider , please correct it and help to keep both integer of each slider can teririm and accepted by the intended recipient . thank you very much.

Answers

  • edited March 2015 Answer ✓

    HI,try this and controls the line15, line 17 same name

    void loop() {
      while (Serial.available() > 0) {
        int value  = Serial.parseInt();
        char cc =  Serial.read();
        if (cc == 'a') {
          analogWrite(led2, value );
        }
        if (cc == 'b') {
          analogWrite(led1, value );
        }
      }
    }
    
  • Thank you very much camperos, This works very well . Your answer was very helpful to me and for me this is really a very precious knowledge . thankyou very very much, Camperos

Sign In or Register to comment.