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.
Open your Arduino IDE.
Goal: remap buttons for improved drawing control with the Processing sketch.
- Open up a new sketch.
- Copy or write in “Arduino Sketch: ControllerDefault”.
- Next, edit the code in the following way:
- Find the section of the code below (about 58 lines down):
- Change the code to match this:
- Make sure it matches the code as you continue through the rest of the tutorial.
- Note: You are mapping the buttons to the correct keys.
- Upload code.
- Open your Processing IDE.
- Upload the following code:
- This is a line drawing sketch with some extra code that we’ll outline for you below.
- Moving the controller around draws lines, just like the previous sketch.
STEP 3Now, let’s start fresh before we add some features.
- 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():
- 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.
- 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?):
- 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:
- 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():
- 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().
- 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: