How to change the background in the Processing section with Arduino

I am trying to use three potentiometers that are connected to the Arduino to adjust the color processing background. I'm super new at this.

Here is my Arduino code:

int potPin = 0;
int potPinB = 1;
int potPinC = 2;

void setup() 
{
    Serial.begin(9600);
}

void loop()
{
    int val = map(analogRead(potPin), 0, 1023, 0, 255);
    Serial.print(val);
    delay(500);
    Serial.print(",");

    int valB = map(analogRead(potPinB), 0, 1023, 0, 255);
    Serial.print(valB);
    delay(500);
    Serial.print(",");

    int valC = map(analogRead(potPinC), 0, 1023, 0, 255);
    Serial.println(valC);
    delay(500);
}

      

Here is my processing code:
I know this is wrong. My screen just shows black.

import processing.serial.*;

Serial myPort;
float val = 0;

void setup() 
{
    size(500, 500); 
    port = new Serial(this, "/dev/cu.usbmodem1451", 9600);
    //port.bufferUntil('\n');

    if (frame != null); 
    frame.setResizable(true);
}

void draw () 
{
    background(val);
}

void serialEvent (Serial port)
{
    val = float(port.readStringUntil('\n'));
}

      

+3


source to share


1 answer


It looks like you can use Linux, you can check this:

"/dev/cu.usbmodem1451" โ†’ mine โ†’ "/ dev / ttyUSB0"

Here is an example I made from your posted code. It works on my machine, let me know if you have any difficulties.

Arduino sketch (same)



int potPin = 0;
int potPinB = 1;
int potPinC = 2;

void setup() 
{
    Serial.begin(9600);
}

void loop()
{
    int val = map(analogRead(potPin), 0, 1023, 0, 255);
    Serial.print(val);
    delay(500);
    Serial.print(",");

    int valB = map(analogRead(potPinB), 0, 1023, 0, 255);
    Serial.print(valB);
    delay(500);
    Serial.print(",");

    int valC = map(analogRead(potPinC), 0, 1023, 0, 255);
    Serial.println(valC);
    delay(500);
}

      

Sketch processing

The machining sketch has been modified, some code was taken from the Arduino - Graph tutorial .

import processing.serial.*;

Serial myPort;        // The serial port
int xPos = 1;         // horizontal position of the graph

void setup () {
  // set the window size:
  size(400, 300);        

  // List all the available serial ports
  println(Serial.list());
  // Open whatever port is the one you're using.
  myPort = new Serial(this, "/dev/ttyUSB0", 9600);
  // don't generate a serialEvent() unless you get a newline character:
  myPort.bufferUntil('\n');
  // set inital background:
  background(0);
}
void draw () {
  // everything happens in the serialEvent()
}

void serialEvent (Serial myPort) {
  // read first string
  String inStringA = myPort.readStringUntil(',');

  // make sure it not empty
  if (inStringA != null){
    // read following strings
    String inStringB = myPort.readStringUntil(',');
    String inStringC = myPort.readStringUntil('\n');

    // convert to an int and map to the screen height:
    float valA = float(inStringA); 
    float valB = float(inStringB); 
    float valC = float(inStringC); 
    // assign to rgb background
    background(valA,valB,valC);
  }
}

      

+1


source







All Articles