Go Down

Topic: Processing nightmare (Read 1 time) previous topic - next topic

Kctess5

I am trying to send a data packet from processing to my arduino. I have a visual representation of 9 high powered leds on processing (9 boxes), when I click on a box I want it to go on or off. I also have 3 sliders under the boxes that I want to control the color. I've been trying to make a simple communication protocol but it just will not work for some reason. Ill give the full processing code but the part that is the problem is the serial communication bit at the end.
Code: [Select]
  import processing.serial.*;
import controlP5.*;

ControlP5 controlP5;
Serial port;
int val;

CheckBox checkbox;
int myColorBackground;
int Red = 255;
int Green = 18;
int Blue = 18;

void setup() {
  size(1270,670);
 
println(Serial.list());
port = new Serial(this, Serial.list()[0], 57600);

 
  smooth();
   controlP5 = new ControlP5(this);
  controlP5.addSlider("Red",0,255,0,155,120,960,20);
  controlP5.addSlider("Green",0,255,0,155,150,960,20);
  controlP5.addSlider("Blue",0,255,255,155,180,960,20); 
 
  Slider sR = (Slider)controlP5.controller("Red");
  Slider sG = (Slider)controlP5.controller("Green");
  Slider sB = (Slider)controlP5.controller("Blue");
 
  sR.setColorForeground(color(255,0,0));
  sG.setColorForeground(color(0,255,0));
  sB.setColorForeground(color(0,0,255));
 
  sR.setColorActive(color(255,0,0));
  sG.setColorActive(color(0,255,0));
  sB.setColorActive(color(0,0,255));
 
 
  checkbox = controlP5.addCheckBox("checkBox",155,20); 
  // make adjustments to the layout of a checkbox.
  checkbox.setColorBackground(color(20,20,20));
  checkbox.setColorForeground(color(0,255,2));
  checkbox.setColorActive(color(Red,Green,Blue));
  checkbox.setColorLabel(color(18,2,128));
  checkbox.setItemsPerRow(9);
  checkbox.setSpacingColumn(30);
  checkbox.setSpacingRow(10);
  checkbox.setSize(80,80);
  // add items to a checkbox.
  checkbox.addItem("1",27);
  checkbox.addItem("2",27);
  checkbox.addItem("3",27);
  checkbox.addItem("4",27);
  checkbox.addItem("5",27);
  checkbox.addItem("6",27);
  checkbox.addItem("7",27);
  checkbox.addItem("8",27);
  checkbox.addItem("9",27);
 

 
}

void keyPressed() {
  println(char(1)+" / "+keyCode);
  if(key==' '){
    for (int i = 0; i < 9; i++){
    checkbox.toggle(i);
    }
  } else {
    for(int i=1;i<10;i++) {
     
      if(keyCode==(48 + i)) {
        checkbox.toggle(i-1);
        println("toggle "+checkbox.getItem(i-1).name());
       
      }
    }
  }
}

void draw() {
    checkbox.setColorActive(color(Red,Green,Blue));

  background(myColorBackground);
  fill(37);
  rect(100,0,1070,670);
 
}

void controlEvent(ControlEvent theEvent) {
  if(theEvent.isGroup()) {
    myColorBackground = 0;
   
    for(int i=0;i<theEvent.group().arrayValue().length;i++) {
      int n = (int)theEvent.group().arrayValue()[i];
     // print(n);
      if(n==1) {
        myColorBackground += ((RadioButton)theEvent.group()).getItem(i).internalValue();
      }
    }
  }
}

void mouseClicked() {
  port.write(500);
  port.write(500);
  port.write(500);
  port.write(500);
  port.write(500);
  port.write(500);
  port.write(500);
  for (int i = 0; i < 9; i++){
  if (checkbox.getState(i) == true) port.write(1);
  if (checkbox.getState(i) == false) port.write(0);
  port.write(500);
  }
  port.write(Red);
  port.write(500);
  port.write(Green);
  port.write(500);
  port.write(Blue);
  port.write(500);
  port.write(20);
  port.write(500);
  port.write(500);
  port.write(500);
  port.write(500);
  port.write(500);
  port.write(500);
  port.write(500);
}
 



arduino code:
Code: [Select]
int ledPin[] = {2, 3, 4, 5, 6, 7, 8, 12, 13}; // the pin that the LED is attached to
int colorArray[] = {9,10,11};

byte onArray[] = {0,0,0,0,0,0,0,0,0};

int recieve[40];     
int message[13];     
int current;
int count;
int program;
boolean recieved = false;

void setup() {
  // initialize serial communication:
  Serial.begin(57600);
  // initialize the LED pin as an output:
  for (int i = 0; i < 9; i++){
  pinMode(ledPin[i], OUTPUT);
  }
   for (int i = 0; i < 9; i++){
  digitalWrite(ledPin[i], HIGH);
  }
 
  for (int i = 0; i < 3; i++){
  pinMode(colorArray[i], OUTPUT);
  }
}

int displayArray(int a){
    for (int i = 0; i < 9; i++){
    if (onArray[i] == 1) digitalWrite(ledPin[i], LOW);
   
    else digitalWrite(ledPin[i], HIGH);
    }
    delay(a);
  }

void loop() {
 
  if (Serial.available() >= 39) {
   
    for (int i=0; i<39; i++) {
        recieve[i] = Serial.read();
      }
      recieved = true;
  }
     
     if (recieved == true){
      count = 0;

         for (int i = 0; i <39; i++){
           current = recieve[i];
           
             if (current != 500){
               message[count] = current;
               count++;
             }
             if (count > 12) break;
         }
         
         for (int i = 0; i < 9; i++){
           if (recieve[i] == 1) onArray[i] = 1;
           if (recieve[i] == 0) onArray[i] = 0;
         }
         analogWrite(colorArray[0], recieve[9]);
         analogWrite(colorArray[1], recieve[10]);
         analogWrite(colorArray[2], recieve[11]);
         recieved = false;
     }
     
   
     displayArray(10);
       
}



The idea is to send a bunch of extra data (500's) from processing to arduino, but only take the wanted data so that it syncs. I don't really have any idea whats going wrong, I'm guessing its a datatype thing or something but I don't know. I'm pretty new to processing so my code may not be as good as it should be. BTW the color is determined by 3 npn transistors and the individual leds are controlled with pnp transistors, hence turning those pins low to turn on the leds.

johnwasser

Serial.read() returns either -1 (no data available) or a value from 0 to 255 (a character).   It's never going to equal 500.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

Kctess5

oh ok I thought it was an int. Any suggestions on how toget around this?

johnwasser

You can send your int values as two bytes OR you can send ASCII characters that spell out the value in decimal and convert back to integers on the other side.  For example:

!500,500,500,500,500,500,500,1,500,0,500,1,500,0,500,1,500,0,500,1,500,0,500,1,500,50,500,35,500,75,500,20,500,500,500,500,500,500,500:

Read bytes till you get to the '!' that indicates the start of a message.  Read decimal values one character at a time till you reach a ',' or ':'.  The ':' indicates the end of the message.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

Go Up