With Processing, you’ll be able to create more controlled drawings, as if physically drawing with a pencil. To do so, you must edit the existing code.

 

STEP 1


Open your Arduino IDE.
Goal: remap buttons for improved drawing control with the Processing sketch.

  1. Open up a new sketch.
  2. Copy or write in “Arduino Sketch: ControllerDefault”.
  3. Next, edit the code in the following way:
    1. Find the section of the code below (about 58 lines down):

    2. Change the code to match this:

    3. Make sure it matches the code as you continue through the rest of the tutorial.
    4. Note: You are mapping the buttons to the correct keys.
  4. Upload code.

 

STEP 2

  1. Open your Processing IDE.
  2. Upload the following code:

    1. This is a line drawing sketch with some extra code that we’ll outline for you below.
    2. Moving the controller around draws lines, just like the previous sketch.

 

STEP 3

Now, let’s start fresh before we add some features.
  1. Clear the screen: You can clear the screen by drawing the whole background one color. Let’s create a clear() function that will draw the background, and call it in setup() and draw():

    1. As you run that function, it erases as quickly as you draw. Why is this? It’s because draw() is called in a loop, so we erase everything after we try to draw it again. To fix this, let’s only erase when we press a button.
  2. Execute the clear() function inside an “if” statement, and it will only erase if the left button is pressed (Remember that we set the ‘a’ key to be mapped to the left button in the Controller’s Sketch?):

  3. Have you noticed that now the controller cursor we’re always drawing. You may want to draw only when the right button is being pressed. Follow the instructions from the previous step, but check the ‘s’ key instead, since this is mapped to the right button in the Controller’s Sketch:

  4. Now, we can change the width and color of the line. Let’s experiment by adding some numbers to set the color with stroke() and the line width with strokeWeight():

  5. You’ll notice that the line is a little thicker and has become red. So, we’ll combine two actions now: Using the top button (‘d’) to cycle through different widths, and the bottom button (‘f’) to cycle through some colors. To keep it simple, we’ll use the code that we already have that’s defined at the top of our Processing sketch. In the “if” statements, we increment (the ‘++’) to increase our index each time the button is pressed, and resetting when we get to the maximum, which is only 2 in this case. Then we’ll use those values when we call stroke() and strokeWeight().

  6. Try it out! Pressing the top button will change the line thickness next time you draw, and pressing the bottom button will change the color. Here’s the final code with everything:

How else can you experiment with this code? Click here to experiment: https://processing.org/tutorials/interactivity/