processing color

edited March 2018 in Arduino

Hello, I wanted to ask you if you know how I can color input inputs (4 inputs, 4 lines with different colors), I tried with stroke (); but I do not come out of it. many thanks in advance

processing code:

// Importiamo la libreria seriale (già inclusa nell'IDE Arduino)
import processing.serial.*;
// Creiamo l'oggetto porta seriale
Serial port; 
// Dato ricevuto dalla porta seriale
int val1;
int val2; 
int val3; 
int val4; 
// Array per contenere i dati
int[] values1;
int[] values2;
int[] values3;
int[] values4;
//*
int lf = 10;    // Linefeed in ASCII
String myString = null;
Serial myPort;  // The serial port
//*

void setup()
{
size(800, 800);
 //*
  // List all the available serial ports
  printArray(Serial.list());
  // Open the port you are using at the rate you want:
  myPort = new Serial(this, Serial.list()[0], 9600);
  //myPort.clear();
  // Throw out the first reading, in case we started reading 
  // in the middle of a string from the sender.
  myString = myPort.readStringUntil(lf);
  myString = null;
 //*
// port = new Serial(this, Serial.list()[0], 9600);
  // Inizializziamo l'array di interi che conterrà i dati
  values1 = new int[width]; values1 = new int[height];
  values2 = new int[width]; values2 = new int[height];
  values3 = new int[width]; values3 = new int[height];
  values4 = new int[width]; values4 = new int[height];
  // Questo comando prepara l'anti-aliasing per la grafica
  smooth();
}

void draw(){
//*
while (myPort.available() >= 3)
{
    myString = myPort.readStringUntil(lf);
    if (myString != null) {
      println(myString);   
    } 
    if (myPort.read() == 0xff) 
    {
 val1 = (myPort.read() << 8) | (myPort.read());
      val2 = (myPort.read() << 8) | (myPort.read());
       val3 = (myPort.read() << 8) | (myPort.read());
      val4 = (myPort.read() << 8) | (myPort.read());
  }
}
 // Per ogni elemento dell'array...
  for (int i=0; i<width-1; i++)
  {
    // ... sposto i valori salvati di una posizione
    values1[i] = values1[i+1];
    values2[i] = values2[i+1];
    values3[i] = values3[i+1];
    values4[i] = values4[i+1];
  }
  // ... e aggiungo il valore appena letto
   values1[width-1] = val1; values1[height-1] = val1; 
   values2[width-1] = val2; values2[height-1] = val2;
    values3[width-1] = val3; values3[height-1] = val3;
  values4[width-1] = val4; values4[height-1] = val4;
  // Imposto il colore di sfondo a nero
  background(255);
  // Imposto il colore del disegno a bianco
  stroke(0);
  // Per ogni elemento dell'array...
  for (int x=1; x<width; x++) 
  {       
    // ... disegno una linea:
    // dalle coordinate del punto precedente...
   line(width-x, height-1-getY1(values1[x-1])-100, width-x-1, height-1-getY1(values1[x])-100);
line(width-x, height-1-getY2(values2[x-1])-100, width-x-1, height-1-getY2(values2[x])-100);
  line(width-x, height-1-getY3(values3[x-1])-100, width-x-1, height-1-getY3(values3[x])-100);
    line(width-x, height-1-getY4(values4[x-1])-100, width-x-1, height-1-getY4(values4[x])-100);
  }
}
int getY1(int val1) {return (int)(val1 / 1023.0f * height) - 1;};
int getY2(int val2) {return (int)(val2 / 1023.0f * height) - 1;};
int getY3(int val3) {return (int)(val3 / 1023.0f * height) - 1;};
int getY4(int val4) {return (int)(val4 / 1023.0f * height) - 1;};

Arduino code:

// Definiamo il pin 0 (RX) che trasmette dati
//#define ANALOG_IN 0

// Setup
void setup() 
{
  // Impostiamo la comunicazione seriale
  Serial.begin(9600); 
}

int value1;

// Loop
void loop() 
{
  // Leggiamo il valore del pin 0 (RX)
  int val = analogRead(ANALOG_IN);
  // Stampiamo un valore di 255: 8 bits impostati a 1 (11111111) 
  Serial.print("|velocita'="); 
  Serial.write( 0xff );
   Serial.print(val, DEC); 
  Serial.write("\r\n"); 
}

Answers

  • Answer ✓

    edit post, highlight code, press ctrl-o to format.

  • Answer ✓

    if you write your code and separate the data coming in from the arduino from the code that processes the data, and supply a sample of that data, then it's easier for people without an arduino to contribute.

  • and please don't post duplicates. keep the discussion here.

Sign In or Register to comment.